在delphi中调用C++ DLL文件,程序进程都启动不起来,单步调试的话按f7无数次还能起来,估计是参数传递的问题,delphi中的变量类型和C++中不兼容吧,源码:
delphi中建立一个单元,静态引入dll文件:
//--------------------------
interface
 uses Sysutils;
  
  function Login(username :PChar; password:PChar) : longint; stdcall; 
implementation
   function Login;external 'GLSMAPI.dll' name 'Login';
 
end.
//-----------------------------------
c++中dll文件定义:
/*
**功能描述: 向服务器提交用户登录请求
**返回值 : long
**参 数  : LPCTSTR lpszUser            用户名
**参 数  : LPCTSTR lpszPassword 用户口令
*/
long Login(LPCTSTR lpszUser,LPCTSTR lpszPassword);LPCTSTR 就是c++中的char*类型,对应delphi中Pchar类型
//-------------------------------
在delphi中使用login函数:
procedure TFormMain.Button1Click(Sender: TObject);
var
 strBuf : array[0..25]of char;
  P1,p2:PChar;
str1,str2:  string;
begin
  str1 := 'alex';
  str2 := 'temp';  Login(PChar(str1),PChar(str2),);//
end;
以上文件编译能通过,但是进程起不来,如果注销掉Login函数,就能起来了
多谢!

解决方案 »

  1.   

    在STDCALL后面加上CE……的那句,怎么拼忘了,呵呵,就没有问题了。
      

  2.   

    ai,说清楚点嘛,老哥!!!
    function Login(username :PChar; password:PChar) : longint; stdcall;
    是不是应该改成
    function Login( var username :PChar; var password:PChar) : longint; stdcall;
    但是还是不行!!!
      

  3.   

    VC里改成
    long WINAPI Login(LPCTSTR lpszUser,LPCTSTR lpszPassword);
      

  4.   

    function Login(username, password: PChar) : longint; cdecl;在 VC 中加
    extern "C"
    {
       long __cdecl Login(LPCTSTR lpszUser,LPCTSTR lpszPassword);
    }或者
    function Login(username, password: PChar) : longint; stdcall;在 VC 中加
    extern "C"
    {
       long __stdcall Login(LPCTSTR lpszUser,LPCTSTR lpszPassword);
    }
      

  5.   

    解决了,C++中函数输出定义问题,
    类似如下就可以了:
    函数原型:extern "C"
       int FAR PASCAL EXPORT Add(int a,int b);
    谢谢大家!每日进步啊!