前进的道路不是一帆风顺的,要随时迎接挑战,敢于战胜困难!

坚持一下,找人聊聊天,释放一些压力!

 

VC dll的调用技巧

Dll2.cpp:

 

int add(int a,int b)
{
 return a+b;
}

int substract(int a,int b)
{
 return a-b;
}

 

Dll2.def:

 

LIBRARY D112

EXPORTS
add
substract

 

正确调用:

 

HMODULE hMod=LoadLibrary("Dll2.dll");
 typedef int (/*_stdcall*/ *MSGBOX)(int a,int b);
 MSGBOX he=(MSGBOX)GetProcAddress(hMod,/*MAKEINTRESOURCE(1)*/"add");
 if (!he)
    {
        // handle the error
        FreeLibrary(hMod);
  int i=GetLastError();
  CString str_err;
  str_err.Format("错误:%d",i);
        AfxMessageBox(str_err);
    }

 CString str;
 //int i=he(2,3);
 str.Format("5+3=%d",he(5,3));
 MessageBox(str);

 

错误调用:

 

HMODULE hMod=LoadLibrary("Dll2.dll");
 typedef int (_stdcall *MSGBOX)(int a,int b);
 MSGBOX he=(MSGBOX)GetProcAddress(hMod,/*MAKEINTRESOURCE(1)*/"add");
 if (!he)
    {
        // handle the error
        FreeLibrary(hMod);
  int i=GetLastError();
  CString str_err;
  str_err.Format("错误:%d",i);
        AfxMessageBox(str_err);
    }

 CString str;
 //int i=he(2,3);
 str.Format("5+3=%d",he(5,3));
 MessageBox(str);

 

posted on 2009-03-14 14:16  山径山精  阅读(391)  评论(0编辑  收藏  举报

导航