
07-06-2023, 17:44
|
 |
Registered User
|
|
Join Date: May 2023
Location: Sri Lanka
Posts: 25
Thanks: 23
Thanked 32 Times in 10 Posts
|
|
Quote:
Originally Posted by BLACKFIRE69
does anyone know how to convert this following Delphi code into InnoSetup?
alternatively, if you have any other suggestions on how to accomplish this in a similar manner, please feel free to share your thoughts. thanks.
Code:
uses
Winapi.Windows, System.SysUtils;
type
TFuncX = procedure(const Var1: Integer); cdecl;
TFuncY = function(const Count: Longint; var Str: AnsiString): Integer; cdecl;
TFuncZ = procedure(const Str1, Str2: WideString); cdecl;
TFuncCommon = record
case Integer of
0: (FuncX: TFuncX);
1: (FuncY: TFuncY);
2: (FuncZ: TFuncZ);
end;
function MySafeLoadLibFunc(const LibName, FuncName: WideString;
var Func: TFuncCommon; var ErrorStr: WideString): Cardinal;
begin
ErrorStr := '';
Result := LoadLibraryW(PWideChar(LibName));
if Result > 32 then begin
try
case Integer(Func) of // Get the address of the function by name
0: Func.FuncX := GetProcAddress(Result, PWideChar(FuncName));
1: Func.FuncY := GetProcAddress(Result, PWideChar(FuncName));
2: Func.FuncZ := GetProcAddress(Result, PWideChar(FuncName));
end;
if not Assigned(Pointer(Integer(Func))) then begin
ErrorStr := 'Could not find function "' + FuncName + '" in library "' + LibName + '"';
Result := 0;
end;
except
on E: Exception do begin
ErrorStr := 'Error loading function "' + FuncName + '" from library "' + LibName + '": ' + E.Message;
Result := 0;
end;
end;
end else ErrorStr := 'Could not load library "' + LibName + '"';
if ErrorStr = '' then ErrorStr := 'empty';
end;
Code:
USAGE:
procedure Test;
var
MyFunc: TFuncCommon;
ErrorStr: WideString;
LibModule: Cardinal;
TmpStr: AnsiString;
begin
try
LibModule := MySafeLoadLibFunc('MyDll.dll', 'MyFunc3', MyFunc, ErrorStr);
if LibModule > 32 then
begin
//MyFunc.FuncX(69); // Call function FuncX
//MyFunc.FuncY(47, TmpStr); // Call function FuncY
MyFunc.FuncZ('Hello', 'World'); // Call function FuncZ
FreeLibrary(LibModule); // Free MyDll.dll
end
else WriteLn(ErrorStr);
except
on E: Exception do WriteLn(E.ClassName, ': ', E.Message);
end;
end;
Code:
MyDll:
library MyDll;
uses
System.SysUtils, Winapi.Windows;
{$R *.res}
function ShowMsg(const Msg: WideString; Caption: WideString = 'A Msg From MyDll.dll'): Integer; cdecl;
begin
Result := MessageBoxW(0, PWideChar(Msg), PWideChar(Caption), MB_OK);
end;
procedure MyFunc1(const Var1: Integer); cdecl;
begin
ShowMsg('[MyFunc1] : Var1 = ' + IntToStr(Var1));
end;
function MyFunc2(const Count: Longint; var Str: AnsiString): Integer; cdecl;
begin
Str := 'BLACKFIRE';
Result := -10;
ShowMsg('[MyFunc2] : Count = ' + IntToStr(Count));
end;
procedure MyFunc3(const Str1, Str2: WideString); cdecl;
begin
ShowMsg('[MyFunc3] : Str1 = ' + Str1 + ', Str2 = ' + Str2);
end;
exports MyFunc1, MyFunc2, MyFunc3;
begin
end.
|
I recommend you to use ChatGPT or Bing Ai for this!
|