我希望我的inno设置在特定文件夹中循环访问dll(.net和com dlls)并注册每个dll。有没有办法识别每个dll类型(.net / com)并根据dll的类型使用regasm / regsvr。
我有一个代码,它在文件夹中循环遍历.net dll并注册每个。代码可以找到here。在这种情况下,所有dll都是.net类型。如何实现同一文件夹中存在的两个dll。
答案 0 :(得分:1)
以下函数(最初基于this article
中相同的命名方法)可能有助于确定FileName
参数指定的文件是否为.NET程序集。正如其名称所述,如果文件是.NET程序集,则应返回True,否则返回False。此函数应适用于32位和64位库。
请注意,此修改中的代码仅适用于Unicode Inno Setup。它不适用于Inno Setup的ANSI版本。对于ANSI版本,请使用下面发布的辅助函数:
[Code]
function BufferToWord(const Buffer: string): Word;
begin
Result := Ord(Buffer[1]);
end;
function BufferToLongWord(const Buffer: string): LongWord;
begin
Result := (Ord(Buffer[2]) shl 16) + Ord(Buffer[1]);
end;
function ReadFromStream(Stream: TStream; Size: Integer): LongWord;
var
Buffer: string;
begin
SetLength(Buffer, Size div 2);
Stream.ReadBuffer(Buffer, Size);
case Size of
2: Result := BufferToWord(Buffer);
4: Result := BufferToLongWord(Buffer);
else
RaiseException('Unexpected byte size to be read.');
end;
end;
function IsDotNetAssembly(const FileName: string): Boolean;
var
FileStream: TFileStream;
PEOffset: LongWord;
MagicNumber: Word;
ComDescriptor: LongWord;
DataDirOffset: LongWord;
begin
// initialize result
Result := False;
// open the file to be read
FileStream := TFileStream.Create(FileName, fmOpenRead or fmShareDenyNone);
try
// seek to the PE header start offset and read the value of
FileStream.Position := $3C;
PEOffset := ReadFromStream(FileStream, SizeOf(PEOffset));
// seek to the optional header because of the Magic number,
// which will tell us if the image is 32 or 64-bit; read it
FileStream.Position := PEOffset + $18;
MagicNumber := ReadFromStream(FileStream, SizeOf(MagicNumber));
// according to image bitness we set the offset to the data
// directory
case MagicNumber of
$010B: DataDirOffset := $60; // 32-bit image
$020B: DataDirOffset := $70; // 64-bit image
else
RaiseException('Invalid image format.');
end;
// position to RVA 15 of the data directory array
FileStream.Position := PEOffset + $18 + DataDirOffset + $70;
// read the value of the COM descriptor
ComDescriptor := ReadFromStream(FileStream, SizeOf(ComDescriptor));
// if this value is non zero, the file is a CLR assembly
Result := ComDescriptor <> 0;
finally
FileStream.free;
end;
end;
要在Inno Setup的ANSI版本中使用上述代码,请将这些代码替换为上述辅助函数。这样做很重要,因为Inno Setup中的流只能使用字符串作为缓冲区,而且Unicode和ANSI版Inno Setup中字符串的每个字符的字节大小不同:
function BufferToWord(const Buffer: AnsiString): Word;
begin
Result := (Ord(Buffer[2]) shl 8) + Ord(Buffer[1]);
end;
function BufferToLongWord(const Buffer: AnsiString): LongWord;
begin
Result := (Ord(Buffer[4]) shl 24) + (Ord(Buffer[3]) shl 16) +
(Ord(Buffer[2]) shl 8) + Ord(Buffer[1]);
end;
function ReadFromStream(Stream: TStream; Size: Integer): LongWord;
var
Buffer: AnsiString;
begin
SetLength(Buffer, Size);
Stream.ReadBuffer(Buffer, Size);
case Size of
2: Result := BufferToWord(Buffer);
4: Result := BufferToLongWord(Buffer);
else
RaiseException('Unexpected byte size to be read.');
end;
end;
上述功能的使用非常简单:
if IsDotNetAssembly('C:\Wherever\WhateverBinary.dll') then
MsgBox('The binary file is a .NET assembly!', mbInformation, MB_OK)
else
MsgBox('The binary file is not a .NET assembly!', mbInformation, MB_OK);