DLLMYDLL.CPP
// MyDll.cpp : Defines the entry point for the DLL application.
//#include "stdafx.h"BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}int DLLMax(int a, int b)
{
if(a>=b)return a;
else
return b;
}
int DLLMin(int a, int b)
{
if(a>=b)return b;
else
return a;
} int DLLRet(int a)
{
return a;
}MyDll.defLIBRARY MyDll
EXPORTS
DLLMax
DLLMin
DLLRet在PB中声明function long DLLRet(ref int a) library 'MyDll.dll'程序中调用int a,li_retli_ret = DLLRet(a)
系统提示 Spectified argument type differs from required argument type
at runtime in DLL function dllret(invalid stack pointer on return from
function call)
// MyDll.cpp : Defines the entry point for the DLL application.
//#include "stdafx.h"BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}int DLLMax(int a, int b)
{
if(a>=b)return a;
else
return b;
}
int DLLMin(int a, int b)
{
if(a>=b)return b;
else
return a;
} int DLLRet(int a)
{
return a;
}MyDll.defLIBRARY MyDll
EXPORTS
DLLMax
DLLMin
DLLRet在PB中声明function long DLLRet(ref int a) library 'MyDll.dll'程序中调用int a,li_retli_ret = DLLRet(a)
系统提示 Spectified argument type differs from required argument type
at runtime in DLL function dllret(invalid stack pointer on return from
function call)
function int DLLRet(ref int a) library 'MyDll.dll'仍是原来的错误提示
还有注意调用约定,_stdcall
{
...
}改为
extern -c int _stdcall DLLMax(int a, int b)
{
...
}其他的相应变化
{
...
}改为
extern "C" int _stdcall DLLMax(int a, int b)
{
...
}
为int DLLRet(int* a)
改为extern "C" int _stdcall DLLMax(int a, int b)
可以正常调用了
但原来那样写是因为看到下面这片文章,def不能够代替extern "C" int _stdcall的声明吗?
还请赐教1)使用导出函数关键字_declspec(dllexport)创建MyDll.dll,该动态链接库中有两个函数,分别用来实现得到两个数的最大和最小数。在MyDll.h和MyDLL.cpp文件中分别输入如下原代码:
//MyDLL.h
extern "C" _declspec(dllexport) int Max(int a, int b);
extern "C" _declspec(dllexport) int Min(int a, int b);
//MyDll.cpp
#include
#include"MyDll.h"
int Max(int a, int b)
{
if(a>=b)return a;
else
return b;
}
int Min(int a, int b)
{
if(a>=b)return b;
else
return a;
} 该动态链接库编译成功后,打开MyDll工程中的debug目录,可以看到MyDll.dll、MyDll.lib两个文件。LIB文件中包含DLL文件名和DLL文件中的函数名等,该LIB文件只是对应该DLL文件的"映像文件",与DLL文件中,LIB文件的长度要小的多,在进行隐式链接DLL时要用到它。读者可能已经注意到在MyDll.h中有关键字"extern C",它可以使其他编程语言访问你编写的DLL中的函数。 2)用.def文件创建工程MyDll 为了用.def文件创建DLL,请先删除上个例子创建的工程中的MyDll.h文件,保留MyDll.cpp并在该文件头删除#include MyDll.h语句,同时往该工程中加入一个文本文件,命名为MyDll.def,再在该文件中加入如下代码:LIBRARY MyDll
EXPORTS
Max
Min 其中LIBRARY语句说明该def文件是属于相应DLL的,EXPORTS语句下列出要导出的函数名称。我们可以在.def文件中的导出函数后加@n,如Max@1,Min@2,表示要导出的函数顺序号,在进行显式连时可以用到它。该DLL编译成功后,打开工程中的Debug目录,同样也会看到MyDll.dll和MyDll.lib文件。