在VC中DLL的调用规则(转)

在VC6中创建DLL有三种方式:

1.Regular Dll with MFC statically Linked

2.Regular Dll using share MFC Dll

3.MFC Extention Dll(using share MFC)

 

    对于规则的(Regular)DLL调用一般在定义函数时声明 extern "C" _declspec(dllexport)。在导出函数时如果是隐式(implicitly load (and link))调用,需要加载Lib库,#pragma comment( lib, "dllname" ),并且声明 extern "C" _declspec(dllimport)。如果是显式调用就只需要用到两个函数LoadLibrary()和GetProcAddress(),需要注意的是在定义函数指针时,需要声明 typedef UINT (CALLBACK * fn)(UINT),其中(CALLBACK *)(也可以是_stdcall,WINAPI),一定不要加上.不然会有一下错误:

Microsoft Visual C++ Debug Library:
Debug Error:
Program: ...
Module:
File: i386\chkesp.c
Line: 42
The value of ESP was not properly saved across a function call. This is usually a result of calling a function declared with one calling convention with a function pointer declared with a different calling convention.

 

如果要用到MFC类必须在该函数声明明中的第一行输入AFX_MANAGE_STATE(AfxGetStaticModuleState());至于静态和共享的区别是在编译时是静态链接MFC库还是动态连接MFC库。

    对于MFC扩展(MFC Extention) DLL调用只能通过加载Lib的方式调用(隐式).它的好处就是能调用类,在类定义和调用中需要加入宏AFX_EXT_CALL.

 

    还有一种DLL就是WIN32 Dynamic Link Dll, 这种DLL的调用方法类似于常规调用在隐式调用导出函数时可以不加 extern "C"... 但是在显示调用的导出函数中必须增加 extern "C"...在导入函数中需要声明 typedef UINT (CALLBACK * fn)(UINT),其中(CALLBACK *)(也可以是_stdcall,WINAPI),需要加上.不然会有一下错误:

Microsoft Visual C++ Debug Library:
Debug Error:
Program: ...
Module:
File: i386\chkesp.c
Line: 42
The value of ESP was not properly saved across a function call. This is usually a result of calling a function declared with one calling convention with a function pointer declared with a different calling convention.

 

     另外一种就是Win32 Static Library我转载的另一篇文章中有详细介绍。

 

下面是一篇关于通过动态加载DLL的方式调用DLL中类的方式。感觉写的不错。

http://www.codeproject.com/KB/DLL/classesexportedusingLL.aspx

posted on 2014-02-07 17:32  carekee  阅读(654)  评论(0编辑  收藏  举报