Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


[DELPHI] Correctly importing a C++ Dll

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
Deine Mutter
Expert Cheater
Reputation: 1

Joined: 05 Apr 2006
Posts: 181

PostPosted: Tue Dec 15, 2009 4:12 pm    Post subject: [DELPHI] Correctly importing a C++ Dll Reply with quote

Hello,
I got this simple function in a dll which is written in C++:
Code:
int WINAPI Testilein(char str[256])
{
   MessageBoxA(NULL, str, "Der Wert der Variablen", MB_OK);

   return 1;
}
When I try to use this function in my Delphi App, it just does not work properly
Code:
program TestpadConsole;

{$APPTYPE CONSOLE}

uses
  SysUtils, Windows;

type
  TStr = array [0..255] of Char;

var hDll: THandle;
    s: TStr;
    Testilein: function(str: TStr): Integer; stdcall;
begin
  try
    hDll := LoadLibrary('C:\...\simplech.dll');
    if hDll <> 0 then begin
      @Testilein := GetProcAddress(hDll, 'Testilein');
      if Assigned(@Testilein) then begin
        s := 'Test';
        Testilein(s);
        Sleep(6000);
      end;
      FreeLibrary(hDll);
    end;
  except
    on E: Exception do begin
      Writeln(E.ClassName, ': ', E.Message);
      Sleep(5000);
    end;
  end;
end.
The dll and the function are correctly loaded (since they return good values) and the messagebox even appears. But the problem is that only the first letter of the String (in this case 'T') is displayed. I do not know how to handle strings in this particular case between Delphi and C++. I would appreciate any help.
_________________
Back to top
View user's profile Send private message
Dark Byte
Site Admin
Reputation: 475

Joined: 09 May 2003
Posts: 25976
Location: The netherlands

PostPosted: Tue Dec 15, 2009 4:47 pm    Post subject: Reply with quote

not sure

anyhow, try this:
replace "s: tstr;" with "s: string;"
do Testilein(@s[1]);

you also might want to change "int WINAPI Testilein(char str[256]) " to "int WINAPI Testilein(char *str) " to get rid of the 256 limit

_________________
Tools give you results. Knowledge gives you control.

Like my help? Join me on Patreon so i can keep helping
Back to top
View user's profile Send private message MSN Messenger
Deine Mutter
Expert Cheater
Reputation: 1

Joined: 05 Apr 2006
Posts: 181

PostPosted: Wed Dec 16, 2009 6:15 am    Post subject: Reply with quote

Replacing the lines like you suggested causes an error, since the parameter of the function is still an char array (TStr). I got rid of the 256 limit in the dll like you said and changed the Parameter in Delphi to PChar:
C++ Dll:
Code:
int WINAPI Testilein(char* str)
{
   MessageBoxA(NULL, str, "Der Wert der Variablen", MB_OK);

   return 1;
}

Delphi:
Code:
program TestpadConsole;

{$APPTYPE CONSOLE}

uses
  SysUtils, Windows;

var hDll: THandle;
    s: String;
    Testilein: function(str: PChar): Integer; stdcall;
begin
  try
    hDll := LoadLibrary('C:\...\simplech.dll');
    if hDll <> 0 then begin
      @Testilein := GetProcAddress(hDll, 'Testilein');
      if Assigned(@Testilein) then begin
        s := 'Test';
        Testilein(@s[1]);
        Sleep(6000);
      end;
      FreeLibrary(hDll);
    end;
  except
    on E: Exception do begin
      Writeln(E.ClassName, ': ', E.Message);
      Sleep(5000);
    end;
  end;
end.

But it is still the same thing, only the first letter gets displayed. That pretty much sucks, because it seemed so easy at first.


[EDIT]
Well, here is the solution: Delphi 2010 sucks, Strings are WideStrings or UnicodeStrings by default, so I had to change it to AnsiString:
Code:
s: AnsiString;


Thank you very much DarkByte Mr. Green
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites