本人用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后出错,可以肯定是参数传递错误,但不知怎么解决,请教大家.

解决方案 »

  1.   

    procedure Preview(hWnd : HWND; strID : PChar); external 'd:\a.dll' name 'Preview';stdcall;
      

  2.   

    delphi调用VC时的动态库vc的动态库参数跟DELPHI是相反的,所以你要反过来用,比如VC A(a,b)在delphi中调用时你要A(b,a)加外加上stdcall这个就不会出错了.
      

  3.   

    Preview(edit1.m_hWnd, '1'); '1'? 你贩了c++语法的原始错误 1 传递字符串常量是不对的 即使用变量转一下
                                 2 记得调用约定stdcall
      

  4.   

    问题解决了,感谢各位,方法是要在VC动态库中,函数声明前加入_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)等效吗?希望得到大家指点;
      

  5.   


    extern "C" _declspec(dllexport) void _stdcall Preview(HWND hWnd, LPCTSTR strID)
    {
    //...
    }
    声明的DLL的函数名并非原始的函数名,可以使用VC的dumpbin.exe /exports dllfilename 查看到如果记错应该是在前面加了下划线。
      

  6.   

    _declspec是C语法的,编译后的函数名是_Preveiw,delphi永远访问不到。加上个PASCAL吧,编译后函数名是PREVEIW
      

  7.   

    extern "C" __declspec(dllexport) int __stdcall Add(int x, int y)-> // _Add@8 ;elphi看不到了//
    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. 請盡量用__雙下劃線Σ 非 _單下劃線Σ。。
    了解了麼?