我在Delphi 10.1中编写了一个DLL,它使用Devart IBDac组件来允许在安装中备份和恢复Firebird DB(将应用程序从Firebird 1.5升级到3)。使用Ansi Inno Setup 5.5.9一切正常。因为我想避免AnsiString
和PAnsiChar
作为参数(为了更方便地使用Delphi),我安装了Inno Setup Unicode版本5.5.9(u)并更改了AnsiString
/ {的参数{1}}到DLL中的PAnsiChar
以及Inno安装脚本中的String
。我也在Inno Setup脚本中尝试了WideString
,但这根本不起作用。 Inno Setup无法识别PChar
。
奇怪的是脚本部分工作,参数正确传输到DLL但我在与参数无关的情况下遇到GPF:
我认为参数类型是这种邪恶的根源。这是我在DLL和脚本中唯一改变的东西。 AnsiString
版本仍然可以使用。
感谢您分享我在这里做错的任何想法。
更新:请求的示例代码:
从资源运行脚本的Delphi过程在第一个语句中失败。在第一行中MessageBox
我验证了所有参数都正确到达。
procedure runScript(aServer, aDatabase, aUser, aPW, aPort, DLL, script: String); stdcall;
var
ResStream: TResourceStream;
DB: TIBCConnection;
SC: TIBCScript;
{ Muss Ansistring sein wegen Resourcestream }
s: ansistring;
begin
try
DB := TIBCConnection.create(nil);
SC := TIBCScript.create(nil);
SC.Connection := DB;
try
DB.clientlibrary := DLL;
DB.Database := aDatabase;
DB.Server := aServer;
DB.Username := aUser;
DB.Password := aPW;
DB.Port := aPort;
DB.connected := true;
ResStream := TResourceStream.create(hInstance, script, RT_RCDATA);
setlength(s, ResStream.size);
ResStream.ReadBuffer(s[1], ResStream.size);
SC.SQL.Text := String(s);
SC.Execute;
DB.close;
finally
SC.free;
DB.free;
end;
except
on e: Exception do
begin
MessageBox(0, PChar(e.message), PChar('Setup - Fehler in "' + script + '"'), mb_ok);
raise;
end;
end;
end;
Inno Setup(Unicode)中过程的定义。其他必需的DLL在第一个函数定义中声明,因此它们都存在。它适用于AnsiString
/ PAnsiChar
!
procedure runScript(aServer, aDatabase, aUser, aPW, aPort, DLL, script: String);
external 'runScript@files:itcsetupfb.dll stdcall setuponly';
此恢复功能有效,但前提是我不在函数中使用TRIM():
procedure restoreDB(destServer, destDatabase, destSysdbaPW, aBackupfile, aPort, DLL: String; PBRange: integer;
PbHandle, LblHandle, WizHandle: THandle); stdcall;
var
IBCRestoreService1: TIBCRestoreService;
cnt: integer;
s: String;
Msg: TMsg;
begin
IBCRestoreService1 := TIBCRestoreService.create(nil);
SendMessage(PbHandle, PBM_SETRANGE, 0, PBRange shl 16);
cnt := 0;
try
try
with IBCRestoreService1 do
begin
clientlibrary := DLL;
Database.Text := destDatabase;
Server := destServer;
Username := 'sysdba';
Password := destSysdbaPW;
Port := aPort;
Options := [roFixFssMetadata, roNoValidityCheck, roFixFssData, roOneRelationAtATime, roDeactivateIndexes];
FixFssCharset := 'ISO8859_1';
BackupFile.Text := aBackupfile;
PageSize := 16384;
verbose := true;
Attach;
try
ServiceStart;
while IsServiceRunning do
begin
inc(cnt);
if cnt > PBRange then
cnt := 0;
s := GetNextLine + ' ';
{ this was formerly s:=trim(strinGreplace(s,'gbak:','', }
{ [rfReplaceall,rfIgnoreCase])). I Identified the trim to be the }
{ cause of the GPF. Very strange. }
delete(s, 1, 5);
while (length(s) > 0) and (s[1] = #32) do
delete(s, 1, 1);
{ if we have no message file (local fb15) }
if pos('format message', s) > 0 then
s := 'Arbeite ...';
SendMessage(LblHandle, $0C, 0, longint(s));
SendMessage(PbHandle, PBM_SETPOS, cnt, 0);
while PeekMessage(Msg, WizHandle, 0, 0, PM_REMOVE) do
begin
TranslateMessage(Msg);
DispatchMessage(Msg);
end;
UpdateWindow(PbHandle);
end;
finally
detach;
end;
end;
finally
IBCRestoreService1.free;
end;
except
on e: Exception do
begin
MessageBox(0,
PChar('Die Wiederherstellung der EMILpro Datenbank ist leider fehlgeschlagen. Folgender Fehler trat auf: ' +
#13#10#10 + e.message), 'Setup - Fehler bei Wiederherstellung', mb_ok);
raise;
end;
end;
end;
此功能在Inno Setup中声明如下:
procedure restoreDB(destServer, destDatabase, destSysdbaPW, aBackupfile, aPort, DLL: String;
PBRange: integer; PbHandle,LblHandle,WizHandle: THandle);
external 'restoreDB@files:itcsetupfb.dll,fbclient.dll,gds32.dll,icudt52.dll,icudt52l.dat,icuin52.dll,icuuc52.dll,fbintl.dll,firebird.conf,firebird.msg,ib_util.dll,engine12.dll,msvcp100.dll,msvcr100.dll,fbintl.conf stdcall setuponly';
答案 0 :(得分:3)
您不能在Inno Setup的DLL API中使用string
类型,原因至少有两个:
当您将函数参数声明为PChar
时,Inno Setup将始终假设实际参数类型为string
。
string
是“智能”类型,用于执行内部分配和引用计数。这样的类不能传递DLL边界,因为DLL不能释放由应用程序分配的内存,反之亦然,因为每个都有自己的内存管理器。更多内存布局和类的内部逻辑可能因Delphi版本(用于构建Inno Setup和DLL的版本)而异。
在Delphi中将参数实现为PWideChar
(= PChar
),并在Unicode Inno Setup中将参数声明为(wide)string
。我没有看到在Delphi中使用PWideChar
用于“in”参数的任何不便。您可以在任何可以使用string
的地方使用它。
有关类似讨论,请参阅
Exchanging strings (PChar) between a Freepascal compiled DLL and a Delphi compiled EXE
答案 1 :(得分:1)
此答案提供了如何从 DLL 获取字符串的完整示例。我用的是 FPC 而不是 Delphi,但应该差不多。
DLL 的源代码:
{$MODE OBJFPC}
{$H+}
library Sample;
uses
SysUtils;
function GetSampleString(Buffer: PWideChar; NumChars: DWORD): DWORD; stdcall;
var
OutStr: UnicodeString;
begin
OutStr := 'sample output string';
if Assigned(Buffer) and (NumChars >= Length(OutStr)) then
StrPCopy(Buffer, OutStr);
result := Length(OutStr);
end;
exports
GetSampleString;
end.
DLL 函数的参数如下:
Buffer
:指向字符串缓冲区的指针NumChars
:缓冲区所需的字符数,不包括终止空字符与 Windows API 一样,调用该函数两次。第一次调用获取所需的字符数,第二次调用将字符串复制到缓冲区中。该函数返回缓冲区所需的字符数,不包括终止空字符。
Inno 设置代码:
function GetSampleString(Buffer: string; NumChars: DWORD): DWORD;
external 'GetSampleString@Sample.dll stdcall';
function SampleString(): string;
var
NumChars: DWORD;
OutStr: string;
begin
result := '';
NumChars := GetSampleString('', 0); // First call: Get # chars needed
SetLength(OutStr, NumChars); // Allocate string
if GetSampleString(OutStr, NumChars) > 0 then
result := OutStr;
end;