没写过dll,照葫芦画瓢写了一个
能编译
不过就是用不成
大家给看看有啥毛病没,谢谢这个是.h#if !defined(AFX_MYISM_H__5BFD6F06_84D9_4163_BE7A_169389502846__INCLUDED_)
#define AFX_MYISM_H__5BFD6F06_84D9_4163_BE7A_169389502846__INCLUDED_#define BMP_API __declspec(dllexport) #ifdef _cplusplus
#ifndef __AFXWIN_H__
#error include 'stdafx.h' before including this file for PCH
#endif
extern "C" {
#endif
BMP_API BOOL LoadBmp(CString sPath,BYTE* pBmpFile);
BMP_API BOOL ShowBmp(CDC* pDC,BYTE* pBmpFile);
BMP_API int PixeltoOffset(CPoint point,int widthbit,int nHeight);
BMP_API BOOL DrawLine(CPoint a,CPoint b,BYTE* pBmpFile);
BMP_API BOOL SaveBmp(CString sPath,BYTE* pBmpFile);#ifdef _cplusplus
}
#endif#endif
这个是.cpp
// MyBmpDll.cpp : Defines the entry point for the DLL application.
//#include "stdafx.h"
#include "MyBmpDll.h"#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
BMP_API BOOL LoadBmp(CString sPath,BYTE* pBmpFile)
{
CFile BmpFile;
if(!BmpFile.Open(sPath,CFile::modeRead,NULL))
{
AfxMessageBox("can't open the file");
return false;
}
int nsize;
nsize=BmpFile.GetLength();
pBmpFile=new BYTE[nsize];
BmpFile.Seek(0,CFile::begin);
BmpFile.Read(pBmpFile,nsize);
BmpFile.Close();
return true;
}BMP_API BOOL ShowBmp(CDC* pDC,BYTE* pBmpFile)
{
if(pBmpFile==NULL)
return false;
BYTE* pBmp=&pBmpFile[54];
int nWidth,nHeight;
BITMAPINFOHEADER *bmpinfo=(BITMAPINFOHEADER*)&pBmpFile[14];
int bitcount=(int)bmpinfo->biBitCount;
if(bitcount!=24)
{
AfxMessageBox("it can only open 24 bit bmp");
return false;
}
nWidth=bmpinfo->biWidth;
nHeight=bmpinfo->biHeight;
BYTE* pBuf,* ptmp;
pBuf=pBmp;
int widthbit=(4-nWidth*3%4)+nWidth*3;
for(int i=0;i<nHeight;i++)
{
pBuf=pBmp+widthbit*i;
for(int j=0;j<nWidth*3;j+=3)
{
ptmp=&pBuf[j];
CPoint point;
point.x=j/3;
point.y=nWidth-i;
pDC->SetPixel(point,RGB(ptmp[2],ptmp[1],ptmp[0]));
}
}
return true;
}BMP_API int PixeltoOffset(CPoint point,int widthbit,int nHeight)
{
int x;
x=point.x*3+widthbit*(nHeight-point.y);
return x;
}BMP_API BOOL DrawLine(CPoint a,CPoint b,BYTE* pBmpFile)
{
if(pBmpFile==NULL)
return false;
BYTE* pBmp=&pBmpFile[54];
int nWidth,nHeight;
BITMAPINFOHEADER *bmpinfo=(BITMAPINFOHEADER*)&pBmpFile[14];
nWidth=bmpinfo->biWidth;
nHeight=bmpinfo->biHeight;
int widthbit=(4-nWidth*3%4)+nWidth*3;
int Minx,Maxy;
if(b.x>a.x)
Minx=a.x;
else Minx=b.x;
if(b.y>a.y)
Maxy=b.y;
else Maxy=a.y;
float tg=float(b.x-a.x)/float(b.y-a.y);
int x,y;
for(int i=0;i<abs(b.x-a.x);i++)
{
x=Minx+i;
y=abs(x/tg);
CPoint point;
point.x=x;
point.y=y;
int j=PixeltoOffset(point,widthbit,nHeight);
pBmpFile[j]=0;
pBmpFile[j+1]=0;
pBmpFile[j+2]=0;
}
return true;
}BMP_API BOOL SaveBmp(CString sPath,BYTE* pBmpFile)
{
CFile BmpFile(sPath,CFile::modeWrite);
BITMAPFILEHEADER* bf=(BITMAPFILEHEADER*) pBmpFile;
int nsize=bf->bfSize;
BmpFile.Write(pBmpFile,nsize);
BmpFile.Flush();
BmpFile.Close();
return true;
}
函数里面应该没什么问题
调用的时候我是
typedef BOOL(_cdecl *Connect)(CString sPath,BYTE* pBmpFile);
HINSTANCE hinstDLL=NULL;
hinstDLL=LoadLibrary("MyBmpDll.dll");
if (hinstDLL)
{
Connect Proc;
Proc = (Connect)GetProcAddress (hinstDLL,"LoadBmp");
if(Proc==NULL)
{
AfxMessageBox("can't find proc");
return;
}
if(!Proc(sPath,pBmpFile))
return;
FreeLibrary(hinstDLL); }
else
{
AfxMessageBox("can't find dll");
}
能编译
不过就是用不成
大家给看看有啥毛病没,谢谢这个是.h#if !defined(AFX_MYISM_H__5BFD6F06_84D9_4163_BE7A_169389502846__INCLUDED_)
#define AFX_MYISM_H__5BFD6F06_84D9_4163_BE7A_169389502846__INCLUDED_#define BMP_API __declspec(dllexport) #ifdef _cplusplus
#ifndef __AFXWIN_H__
#error include 'stdafx.h' before including this file for PCH
#endif
extern "C" {
#endif
BMP_API BOOL LoadBmp(CString sPath,BYTE* pBmpFile);
BMP_API BOOL ShowBmp(CDC* pDC,BYTE* pBmpFile);
BMP_API int PixeltoOffset(CPoint point,int widthbit,int nHeight);
BMP_API BOOL DrawLine(CPoint a,CPoint b,BYTE* pBmpFile);
BMP_API BOOL SaveBmp(CString sPath,BYTE* pBmpFile);#ifdef _cplusplus
}
#endif#endif
这个是.cpp
// MyBmpDll.cpp : Defines the entry point for the DLL application.
//#include "stdafx.h"
#include "MyBmpDll.h"#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
BMP_API BOOL LoadBmp(CString sPath,BYTE* pBmpFile)
{
CFile BmpFile;
if(!BmpFile.Open(sPath,CFile::modeRead,NULL))
{
AfxMessageBox("can't open the file");
return false;
}
int nsize;
nsize=BmpFile.GetLength();
pBmpFile=new BYTE[nsize];
BmpFile.Seek(0,CFile::begin);
BmpFile.Read(pBmpFile,nsize);
BmpFile.Close();
return true;
}BMP_API BOOL ShowBmp(CDC* pDC,BYTE* pBmpFile)
{
if(pBmpFile==NULL)
return false;
BYTE* pBmp=&pBmpFile[54];
int nWidth,nHeight;
BITMAPINFOHEADER *bmpinfo=(BITMAPINFOHEADER*)&pBmpFile[14];
int bitcount=(int)bmpinfo->biBitCount;
if(bitcount!=24)
{
AfxMessageBox("it can only open 24 bit bmp");
return false;
}
nWidth=bmpinfo->biWidth;
nHeight=bmpinfo->biHeight;
BYTE* pBuf,* ptmp;
pBuf=pBmp;
int widthbit=(4-nWidth*3%4)+nWidth*3;
for(int i=0;i<nHeight;i++)
{
pBuf=pBmp+widthbit*i;
for(int j=0;j<nWidth*3;j+=3)
{
ptmp=&pBuf[j];
CPoint point;
point.x=j/3;
point.y=nWidth-i;
pDC->SetPixel(point,RGB(ptmp[2],ptmp[1],ptmp[0]));
}
}
return true;
}BMP_API int PixeltoOffset(CPoint point,int widthbit,int nHeight)
{
int x;
x=point.x*3+widthbit*(nHeight-point.y);
return x;
}BMP_API BOOL DrawLine(CPoint a,CPoint b,BYTE* pBmpFile)
{
if(pBmpFile==NULL)
return false;
BYTE* pBmp=&pBmpFile[54];
int nWidth,nHeight;
BITMAPINFOHEADER *bmpinfo=(BITMAPINFOHEADER*)&pBmpFile[14];
nWidth=bmpinfo->biWidth;
nHeight=bmpinfo->biHeight;
int widthbit=(4-nWidth*3%4)+nWidth*3;
int Minx,Maxy;
if(b.x>a.x)
Minx=a.x;
else Minx=b.x;
if(b.y>a.y)
Maxy=b.y;
else Maxy=a.y;
float tg=float(b.x-a.x)/float(b.y-a.y);
int x,y;
for(int i=0;i<abs(b.x-a.x);i++)
{
x=Minx+i;
y=abs(x/tg);
CPoint point;
point.x=x;
point.y=y;
int j=PixeltoOffset(point,widthbit,nHeight);
pBmpFile[j]=0;
pBmpFile[j+1]=0;
pBmpFile[j+2]=0;
}
return true;
}BMP_API BOOL SaveBmp(CString sPath,BYTE* pBmpFile)
{
CFile BmpFile(sPath,CFile::modeWrite);
BITMAPFILEHEADER* bf=(BITMAPFILEHEADER*) pBmpFile;
int nsize=bf->bfSize;
BmpFile.Write(pBmpFile,nsize);
BmpFile.Flush();
BmpFile.Close();
return true;
}
函数里面应该没什么问题
调用的时候我是
typedef BOOL(_cdecl *Connect)(CString sPath,BYTE* pBmpFile);
HINSTANCE hinstDLL=NULL;
hinstDLL=LoadLibrary("MyBmpDll.dll");
if (hinstDLL)
{
Connect Proc;
Proc = (Connect)GetProcAddress (hinstDLL,"LoadBmp");
if(Proc==NULL)
{
AfxMessageBox("can't find proc");
return;
}
if(!Proc(sPath,pBmpFile))
return;
FreeLibrary(hinstDLL); }
else
{
AfxMessageBox("can't find dll");
}
解决方案 »
- 菜鸟请问大侠们一下简单的聊天工具socket的实现的一个问题
- 图像旋转错误
- 关于用线程往文本里写数据,并可查看本文更新过程的问题,各位大侠进来指点指点!
- 请问如何通过文件的绝对PIDL获取其文件的创建时间、修改时间、文件大小等信息?
- InstallShield问题,如何在Uninstall时卸载之前安装的DLL?解决马上给分!!
- list控件的一个问题
- LOWORD这个宏是在什么情况下会用到。
- CALLBACK函数的问题。。。请大家帮助!!
- 寻《windows程序设计(中文版)》下载地址,知道的请帮个忙
- 自绘的BUTTON,单击可重绘,单击的快一点,双击的话,只响应一次。画也只画一次。
- 项目做完了,但会出现这样的错误,好郁闷啊,而且是偶尔才出现的,差不多三天几只出现一次,有经验的大哥进来帮看看,谢谢!
- 如何得到选择的odbc数据源?(急!!)
extern C 和_cdecl 函数调用约定要一致具体的怎么一致?
是我老师给我的
让我看着做
-----------------------
又出现了新状况我加了一个.def
现在可以调用了
可是传递不了指针