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

夙愿已清 提交于 2019-12-04 07:11:49

Here is a sample code of how to allocate a string that returns 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;

A very simple solution for the case where the DLL function is called only once in the installation - use a global buffer in your dll for the string.

DLL side:

char g_myFuncResult[256];

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

Inno-Setup side:

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

The only practical way to do this is to allocate a string in Inno setup, and pass a pointer to that along with the length to your DLL that then writes to it up to the length value before returning.

Here's some example code taken from the 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 more up to date discussion.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!