2

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.

10
  • Replace PChar type - it is only alias. Depending on Delphi version it would be PAnsiChar pointing to 8-bit before-unicode data or PWideChar pointing to 16-bit UCS-2 data (and which is used by Java and maybe dotnet too), not good to play guessing games in DLLs. 1: Tag your question with actual Delphi version. 2: Change function header for certain pointer type. 2.1: Turn on typed pointers ( {$T+} pragma). Might help to catch such problems, if code is not playing dirty all through. 3: Show declaration of tLoginPacket type. 4: What is "s:IP_bigstr;", is it string or what type is it ? Commented Aug 6, 2012 at 8:41
  • 5: show DLL log content. especially after s:=password; - do log the s value. You can also use Windows API OutputDebugString for logging. In general better would be to declare var s_login, s_password: UnicodeString/AnsiString/WideString; - type depending upon Delphi version and arguments pointer type. Then with very 1st lines do s_login := login; s_password := password; and only use string vars ever after. And - do log and do check values of those vars!!! would they be a single-letter values, only 1st letter of every passed string ? Commented Aug 6, 2012 at 8:45
  • Like about PChar, what type is UnmanagedType.LPStr ? Is it Unicode or MBCS ? which codepage ? or is it random guess, depending on compiler options like _T macro in Visual C++ ? Commented Aug 6, 2012 at 8:49
  • hi, delphi version is, Turbo Delphi Explorer (it's Delphi 2006) Commented Aug 6, 2012 at 8:51
  • edit the question to add dlephi 2006 tag to it please. delphi 2006 was not unicode-based, so PChar probably is PAnsiChar. But to save you on later troubles if Delphi upgraded, and to make code self-documenting, i stil lsuggest you to change them to PAnsiChar. I also suspect you only get 1 first char of new string. Please chack and implement other items from the list. Commented Aug 6, 2012 at 9:29

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

4 Comments

hm thx for reply. I tried [DllImport ("ServerTool", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)], but fails still.
Turbo Delphi Explorer (it's Delphi 2006)
Then use CharSet=CharSet.Ansi,
Can you try removing all code from the dll function except for the initial logging. If the call succeeds and the logging works, the call in itself is fine, and another part of the function is causing the trouble.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.