如何从DLL返回字符串到Inno Setup?

问题描述 投票:7回答:3

我需要将字符串值返回到调用inno设置脚本。问题是我找不到管理分配的内存的方法。如果我在DLL方面进行分配,则在脚本方面没有任何可取消分配的内容。我不能使用输出参数,因为Pascal脚本中也没有分配函数。我该怎么办?

c++ dll inno-setup pascalscript
3个回答
7
投票

这里是如何分配从DLL返回的字符串的示例代码:

[Code]
Function GetClassNameA(hWnd: Integer; lpClassName: PChar; nMaxCount: Integer): Integer; 
External '[email protected] 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;

4
投票

非常简单的解决方法对于仅一次调用DLL函数的情况-在您的dll中使用全局缓冲区作为字符串。

DLL侧:

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端:

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

3
投票

唯一可行的方法是在Inno Setup中分配一个字符串,并将指向该字符串的指针及其长度传递给您的DLL,然后在返回之前将其写入长度值的DLL。

这里是一些示例代码taken from the newsgroup

function GetWindowsDirectoryA(Buffer: AnsiString; Size: Cardinal): Cardinal;
external '[email protected] stdcall';
function GetWindowsDirectoryW(Buffer: String; Size: Cardinal): Cardinal;
external '[email protected] 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;

也请参阅this thread以获取最新的讨论。

© www.soinside.com 2019 - 2024. All rights reserved.