首页 > 解决方案 > 如何取消引用 Inno Setup Pascal 脚本中的指针?

问题描述

我从 Inno Setup Script 中的 DLL 文件调用一个函数,它的返回类型是PAnsiChar. 为了获得整个字符串,我需要取消引用指针,但标准的帕斯卡语法在这里不起作用。甚至有可能做到这一点吗?

function SQLDLL : PAnsiChar;
external 'GetSQLServerInstances@files:IsStartServer.dll stdcall setuponly';

function NextButtonClick(CurPage: Integer): Boolean;
var
  hWnd: Integer;
  Str : AnsiString;
begin
  if CurPage = wpWelcome then begin
    hWnd := StrToInt(ExpandConstant('{wizardhwnd}'));

    MessageBox(hWnd, 'Hello from Windows API function', 'MessageBoxA', MB_OK or MB_ICONINFORMATION);

    MyDllFuncSetup(hWnd, 'Hello from custom DLL function', 'MyDllFunc', MB_OK or MB_ICONINFORMATION);

    Str := SQLDLL;
    try
      { if this DLL does not exist (it shouldn't), an exception will be raised }
      DelayLoadedFunc(hWnd, 'Hello from delay loaded function', 'DllFunc', MB_OK or MB_ICONINFORMATION);
    except
      { handle missing dll here }
    end;
  end;
  Result := True;
end;

我只有 DLL 文件。原始语言是Delphi。

我更新到最新版本的 Inno Setup 6.0.3 并在我的家用 Windows 10 Pro 机器上测试了这段代码:

[Setup]
AppName=My Program
AppVersion=1.5
WizardStyle=modern
DefaultDirName={autopf}\My Program
DisableProgramGroupPage=yes
DisableWelcomePage=no
UninstallDisplayIcon={app}\MyProg.exe
OutputDir=userdocs:Inno Setup Examples Output

[Files]
Source: "MyProg.exe"; DestDir: "{app}"
Source: "MyProg.chm"; DestDir: "{app}"
Source: "Readme.txt"; DestDir: "{app}"; Flags: isreadme
Source: "IsStartServer.dll"; Flags: dontcopy

[Code]
function SQLDLL : PAnsiChar;
external 'GetSQLServerInstances@files:IsStartServer.dll stdcall';

function NextButtonClick(CurPage: Integer): Boolean;
var
  Str : PAnsiChar;
begin
  Str := SQLDLL;
  Result := True;
end; 

现在我遇到了这种错误: 在此处输入图像描述

我不明白为什么它必须查看我的“临时”目录?我还听说这个问题可能与 Windows 10 UAC 中的组策略有关,但我不确定我应该在这里做什么来摆脱这个错误。

标签: pointersinno-setupdereferencepascalscript

解决方案


如果我理解正确,你自己SQLDLL管理一些内存缓冲区并返回一个指向Unicode字符串的指针(不是 ANSI,这就是为什么你尝试时只有一个字符PAnsiChar,根据你的评论)。

Inno Setup 不直接支持这一点,甚至没有PWideChar类型。但是,我们可以自己处理。我们只需要分配一个大小合适的 Inno 字符串并手动复制数据。

这是一个如何做到这一点的工作示例。它GetCommandLineW用作返回 a 的示例函数PWideChar,但您可以对SQLDLL函数执行相同的操作。

  • 从外部函数中获取指针并将其存储在一个变量中(a Cardinal- 在我的示例中,我PWideChar为它创建了一个 typedef)。
  • 使用 获取字符串长度lstrlenW
  • 创建一个空的常规String,但使用 将其设置为正确的长度SetLength。这将保留足够的容量,以便我们可以在下一步中将实际内容写入其中。
  • 用于lstrcpyW将指针引用的字符串复制到常规String变量。
    • (如果您使用 ANSI 版本的 Inno Setup:请WideCharToMultiByte改用,请参阅本文末尾的更新。)

诀窍是以这样一种方式导入lstrcpyW,即目标指针被声明为String但源指针被声明为Cardinal(或我的 typedefPWideChar这里)。

type
  PWideChar = Cardinal; { Inno doesn't have a pointer type, so we use a Cardinal instead }

{ Example of a function that returns a PWideChar }
function GetCommandLineW(): PWideChar;
external 'GetCommandLineW@kernel32.dll stdcall';

{ This function allows us to get us the length of a string from a PWideChar }
function lstrlenW(lpString: PWideChar): Cardinal;
external 'lstrlenW@kernel32.dll stdcall';

{ This function copies a string - we declare it in such a way that we can pass a pointer
  to an Inno string as destination
  This works because Inno will actually pass a PWideChar that points to the start of the
  string contents in memory, and internally the string is still null-terminated
  We just have to make sure that the string already has the right size beforehand! }
function lstrcpyW_ToInnoString(lpStringDest: String; lpStringSrc: PWideChar): Integer;
external 'lstrcpyW@kernel32.dll stdcall';

function InitializeSetup(): Boolean;
var
  returnedPointer: PWideChar; { This is what we get from the external function }
  stringLength: Cardinal; { Length of the string we got }
  innoString: String; { This is where we'll copy the string into }
begin
  { Let's get the PWideChar from the external function }
  returnedPointer := GetCommandLineW();

  { The pointer is actually just a renamed Cardinal at this point: }
  Log('String pointer = ' + IntToStr(returnedPointer));

  { Now we have to manually allocate a new Inno string with the right length and
    copy the data into it }

  { Start by getting the string length }
  stringLength := lstrlenW(returnedPointer);
  Log('String length = ' + IntToStr(stringLength));

  { Create a string with the right size }
  innoString := '';
  SetLength(innoString, stringLength);

  { This check is necessary because an empty Inno string would translate to a NULL pointer
    and not a pointer to an empty string, and lstrcpyW cannot handle that. }
  if StringLength > 0 then begin
    { Copy string contents from the external buffer to the Inno string }
    lstrcpyW_ToInnoString(innoString, returnedPointer);
  end;

  { Now we have the value stored in a proper string variable! }
  Log('String value = ' + innoString);

  Result := False;
end;

如果将其放入安装程序并运行它,您会看到如下输出:

[15:10:55,551]   String pointer = 9057226
[15:10:55,560]   String length = 106
[15:10:55,574]   String value = "R:\Temp\is-9EJQ6.tmp\testsetup.tmp" /SL5="$212AC6,121344,121344,Z:\Temp\testsetup.exe" /DEBUGWND=$222722 

正如你所看到的,命令行字符串(我们得到的PWideChar)被正确地复制到一个常规的字符串变量中,并且可以在最后正常访问。


更新:如果您使用的是 ANSI 版本的 Inno Setup 而不是 Unicode,则仅此代码将不起作用。所需的更改是:lstrcpyW您将使用 ,而不是使用WideCharToMultiByte

function WideCharToMultiByte_ToInnoString(CodePage: Cardinal; dwFlags: Cardinal; lpWideCharStr: PWideChar; cchWideChar: Cardinal; lpMultiByteStr: String; cbMultiByte: Cardinal; lpDefaultChar: Cardinal; lpUsedDefaultChar: Cardinal): Integer;
external 'WideCharToMultiByte@kernel32.dll stdcall';

{ Later on: Instead of calling lstrcpyW_ToInnoString, use this:
  Note: The first parameter 0 stands for CP_ACP (current ANSI code page), and the
  string lengths are increased by 1 to include the null terminator }
WideCharToMultiByte_ToInnoString(0, 0, returnedPointer, stringLength + 1, innoString, stringLength + 1, 0, 0);

推荐阅读