本人用VC写了一个动态链接库,格式如下:
extern "C"
_declspec(dllexport)
void Preview(HWND hWnd, LPCTSTR strID)
{
//...
}
用Delphi写了一个调用界面,格式如下:
procedure Preview(hWnd : HWND; strID : PChar); external 'd:\a.dll' name 'Preview';
procedure TForm1.a;
begin
Preview(edit1.m_hWnd, '1');
end;运行程序后,在调完动态库函数Preview后出错,可以肯定是参数传递错误,但不知怎么解决,请教大家.
extern "C"
_declspec(dllexport)
void Preview(HWND hWnd, LPCTSTR strID)
{
//...
}
用Delphi写了一个调用界面,格式如下:
procedure Preview(hWnd : HWND; strID : PChar); external 'd:\a.dll' name 'Preview';
procedure TForm1.a;
begin
Preview(edit1.m_hWnd, '1');
end;运行程序后,在调完动态库函数Preview后出错,可以肯定是参数传递错误,但不知怎么解决,请教大家.
2 记得调用约定stdcall
如果这样声明:
extern "C" _declspec(dllexport) void _stdcall Preview(HWND hWnd, LPCTSTR strID)
{
//...
}
则在Delphi调用中出错,显示找不到函数Preview;
如果改成这样:
extern "C" void _stdcall Preview(HWND hWnd, LPCTSTR strID)
{
//...
}
在.def文件中加入导出函数名,则运行正常,为什么是这样呢? .def文件不是和_declspec(dllexport)等效吗?希望得到大家指点;
extern "C" _declspec(dllexport) void _stdcall Preview(HWND hWnd, LPCTSTR strID)
{
//...
}
声明的DLL的函数名并非原始的函数名,可以使用VC的dumpbin.exe /exports dllfilename 查看到如果记错应该是在前面加了下划线。
extern "C" __declspec(dllexport) int __cdecl Add(int x, int y)-> Add it's OKextern "C" 聲明以C方式修飾函數名
可__stdcall 確是如此Σ修飾 所以函數名就不是以C方式修飾了
->
Name-decoration convention An underscore (_) is prefixed to the name. The name is followed by the at sign (@) followed by the number of bytes (in decimal) in the argument list. Therefore, the function declared as int func( int a, double b ) is decorated as follows: _func@12 而 __cdeclName-decoration convention Underscore character (_) is prefixed to names ps. 請盡量用__雙下劃線Σ 非 _單下劃線Σ。。
了解了麼?