 |
Cheat Engine The Official Site of Cheat Engine
|
| View previous topic :: View next topic |
| Author |
Message |
Deine Mutter Expert Cheater
Reputation: 1
Joined: 05 Apr 2006 Posts: 181
|
Posted: Tue Dec 15, 2009 4:12 pm Post subject: [DELPHI] Correctly importing a C++ Dll |
|
|
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 |
|
 |
Dark Byte Site Admin
Reputation: 475
Joined: 09 May 2003 Posts: 25976 Location: The netherlands
|
Posted: Tue Dec 15, 2009 4:47 pm Post subject: |
|
|
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 |
|
 |
Deine Mutter Expert Cheater
Reputation: 1
Joined: 05 Apr 2006 Posts: 181
|
Posted: Wed Dec 16, 2009 6:15 am Post subject: |
|
|
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:
Thank you very much DarkByte
|
|
| Back to top |
|
 |
|
|
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
|
|