dll - Inno Setup 6 不能使用带字符串参数的 DLL 函数,但它在 Inno Setup 5 中有效

标签 dll inno-setup inno-setup-v6

我们目前使用 Inno Setup 5.5.3 版来构建安装程序。我打算将此版本升级到 6.1.2。
我们使用 Inno Setup 来安装我们的产品。我们随安装程序一起提供许可证代码,用户在安装过程中在字段中输入许可证。
此许可证使用自定义 DLL 进行验证,并且 DLL 为有效许可证返回非否定结果。
此过程在 5.5.3 和 5.6.1 中运行良好,但在版本 6 中失败(使用 6.0.5 和 6.1.2 测试)。
不幸的是,没有生成指出确切问题的日志。
这个自定义 DLL 是 32 位的,是在 10 多年前使用 C++ 构建的。没有计划重做这部分。有没有办法使用相同的 DLL 并解决问题?谢谢你。

[Files]
Source: mydll.dll; Flags: dontcopy
// This function calls the external mydll which parses licenseCode and
// returns an integer
function getFlags( secret, licenseCode : String) : Integer;
external 'getFlags@files:mydll.dll cdecl';
function checkLicense(license : String) : Boolean;
var
  secret : String;
begin
  Result := False;  // default is Incorrect license
  
  license := Trim(license);
  secret := <secret>

  // This line calls the above getFlags function and expects to get an integer
  license_flags := getFlags( secret, license);
  
  if license_flags = -1 then begin
    if not suppressMsgBox then begin
      MsgBoxLog( ‘Incorrect License’)
    end;
    Exit;
  end;

  Result := True;
end;
// This is the c++ function
PS_EXPORT(int) getFlags( const char * secret, const char * license ) {

  // This function is returning -1 for Inno Setup 6
  // but works fine for Inno Setup 5
  if (strlen(license) == 0)
    return -1; 

  ...
}

最佳答案

这不是 5.x 与 6.x 的问题,也不是位数问题。这是 Ansi 与 Unicode 的问题。
在 5.x 中,有两个版本的 Inno Setup,Ansi 版和 Unicode 版。您可能使用的是 Ansi 版本,并且您的代码是为它设计的。在 6.x 中,只有 Unicode 版本。您的代码在 Unicode 版本中不起作用。通过升级到 6.x,您无意中也从 Ansi 升级到了 Unicode。
一个快速而肮脏的解决方案是更改 getFlags 的声明函数将参数正确声明为 Ansi 字符串(AnsiString 类型):

function getFlags( secret, licenseCode : AnsiString) : Integer;
external 'getFlags@files:mydll.dll cdecl';
正确的解决方案是重新实现您的 DLL 以使用 Unicode 字符串(wchar_t 指针):
PS_EXPORT(int) getFlags( const wchar_t * secret, const wchar_t * license )

这是一个类似的问题:Inno Setup Calling DLL with string as parameter .
另见 Upgrading from Ansi to Unicode version of Inno Setup (any disadvantages) .

关于dll - Inno Setup 6 不能使用带字符串参数的 DLL 函数,但它在 Inno Setup 5 中有效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65115997/

相关文章:

inno-setup - 为什么在 Inno Setup 6 中编译的安装程序比 Inno Setup 5 大 1 MB

inno-setup - 如果包含文件在包含后被删除,则 Inno Setup 在编译后显示 "Failed to open included file"错误消息

c - 有没有办法通过名称获取 Windows 中当前进程的函数?

c++ - Qt 应用程序部署不起作用。缺少入口点和 dll

c++ - 尝试从 DLL 加载的派生类实例访问基类 vector 成员时程序崩溃

constants - 在 Inno Setup 安装之前使用 [Code] 更改 AppID

c++ - 错误 C1189 MFC

inno-setup - 从 Inno Setup 检查 Windows 版本的问题

windows - 如何强制应用程序以管理员权限运行