没写过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");
  }

解决方案 »

  1.   

    标准的Dll的话,建议不要有类进行参数传递(更不要做为返回值),如上述的CString
      

  2.   

    extern C 和_cdecl 函数调用约定要一致
      

  3.   

    to  oyljerry(【勇敢的心】→ ㊣Warm up,A za!㊣) 
    extern C 和_cdecl 函数调用约定要一致具体的怎么一致?
      

  4.   

    不是
    是我老师给我的
    让我看着做
    -----------------------
    又出现了新状况我加了一个.def
    现在可以调用了
    可是传递不了指针
      

  5.   

    typedef BOOL(__stdcall *Connect)(CString sPath,BYTE* pBmpFile);
      

  6.   

    你这样,到不如用隐式加载的方式处理MFC DLL了。只要加AFX_EXT_CLASS就可以了。