Question

so I want to use .dll file's function made from delphi.

Here is delphi's code,

procedure Login(login,password:PChar); stdcall;
   var
    LoginPacket:tLoginPacket;
    s:IP_bigstr;
    q,w:integer;
    pb:PByte;
   begin
    LogMessage('Login: '+login+' pwd: '+password);
    fillchar(loginpacket,sizeof(loginpacket),0);
    LoginPacket.code:=10;
    LoginPacket.lr.version:=100;
    LoginPacket.lr.protocol:=ProtocolVersion;
    LoginPacket.lr.login:=login;
    LoginPacket.lr.gameversion:=version;
    s:=password;
    EncryptPwd(s,@loginPacket.lr.pwd);
    Loginpacket.lr.pwdhash:=PasswordHash(password);
    LoginPacket.lr.gameID:=0;
    LoginPacket.lr.regname:='noname';
    LoginPacket.lr.cdkey:=0;
    LoginPacket.lr.cshash:=0;
    loginpacket.lr.sversion:='';
    pb:=@loginpacket; inc(pb);
    for q:=1 to sizeof(tloginpacket)-1 do begin
      pb^:=pb^ xor ((q+10)*(q+10) div 5);
      inc(pb);
    end;
    SendData(@loginpacket,sizeof(loginpacket));
    SimpleRequest(21,1,0);
    SimpleRequest(20,0,0);    
   end;

and here is my C#(unity3d) try,

[DllImport ("ServerTool")]
private static extern void Login([MarshalAs(UnmanagedType.LPStr)]string id, [MarshalAs(UnmanagedType.LPStr)]string pass);

private static extern void Login(string id, string pass);

...
if(stage ==1){
Login("Test", "qwerty");
stage = 2;
}

above both definition to Login method fails,

and when running this part(if(stage==1)...Login), unity crashes and closed.

So I think this Login usage from C# has some problem with communicating delphi dll.

Please help.

Thank in advance.

Was it helpful?

Solution

Don't forget the calling convention in C# as well:

[DllImport ("ServerTool.dll"), CallingConvention=CallingConvention.StdCall)]

You may also need to specify a CharSet, based on which version of Delphi you use.

See http://msdn.microsoft.com/en-us/library/7b93s42f.aspx and http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.callingconvention.aspx

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top