How to return a string from a DLL in Inno Setup?

I need to return a string value to the calling inno setup script. The problem is that I cannot find a way to manage the allocated memory. If I highlight on the DLL side, I have nothing to remove from the script side. I cannot use the output parameter because there is no distribution function in the Pascal script. What should I do?

+6
source share
3 answers

Here is a sample code on how to highlight a string that is returned from a DLL:

[code] Function GetClassNameA(hWnd: Integer; lpClassName: PChar; nMaxCount: Integer): Integer; External ' GetClassNameA@User32.dll StdCall'; function GetClassName(hWnd: Integer): string; var ClassName: String; Ret: Integer; begin // allocate enough memory (pascal script will deallocate the string) SetLength(ClassName, 256); // the DLL returns the number of characters copied to the buffer Ret := GetClassNameA(hWnd, PChar(ClassName), 256); // adjust new size Result := Copy(ClassName, 1 , Ret); end; 
+7
source

The only practical way to do this is to select the line in the Inno setting and pass a pointer to it along with the length in the DLL, which then writes it to the length value before returning.

Here is an example of code taken from a newsgroup .

 function GetWindowsDirectoryA(Buffer: AnsiString; Size: Cardinal): Cardinal; external ' GetWindowsDirectoryA@kernel32.dll stdcall'; function GetWindowsDirectoryW(Buffer: String; Size: Cardinal): Cardinal; external ' GetWindowsDirectoryW@kernel32.dll stdcall'; function NextButtonClick(CurPage: Integer): Boolean; var BufferA: AnsiString; BufferW: String; begin SetLength(BufferA, 256); SetLength(BufferA, GetWindowsDirectoryA(BufferA, 256)); MsgBox(BufferA, mbInformation, mb_Ok); SetLength(BufferW, 256); SetLength(BufferW, GetWindowsDirectoryW(BufferW, 256)); MsgBox(BufferW, mbInformation, mb_Ok); end; 

Also see this thread for a more up-to-date discussion.

+2
source

A very simple solution for the case when the DLL function is called only once during the installation process - use the global buffer in your dll for the line.

DLL side:

 char g_myFuncResult[256]; extern "C" __declspec(dllexport) const char* MyFunc() { doSomeStuff(g_myFuncResult); // This part varies depending on myFunc purpose return g_myFuncResult; } 

Inno-Setup Side:

 function MyFunc: PChar; external ' MyFunc@files :mydll.dll cdecl'; 
+2
source

Source: https://habr.com/ru/post/910606/


All Articles