我可以将用Delphi编写的DLL加载到PowerShell中吗?

cam*_*.rw 3 delphi dll powershell

我可以通过[Reflection.Assembly]::LoadFile()方法直接在Powershell中使用我在Delphi(Delphi 10)中创建的DLL 吗?我正在尝试,但得到错误:

使用"1"参数调用"LoadFile"的异常:"模块应该包含一个程序集清单.

我能够将Delphi DLL包装在我用C#编写的DLL中并以这种方式使用它,但不愿意,因为它意味着为每个更改而不是一个更改编译两个项目.

这是我目前在Delphi DLL中的代码:

library TestDLL;


procedure TestCall(foo: PChar); stdcall;
begin
end;

exports
  TestCall;

begin
end.
Run Code Online (Sandbox Code Playgroud)

Dav*_*nan 6

您的Powershell代码适用于托管程序集.但是您的Delphi库是一个非托管DLL.要直接访问它,请使用pinvoke.一个简单的例子:

Delphi库

library TestDLL;

uses
  SysUtils;

function TestCall(foo: PChar): Integer; stdcall;
begin
  Result := StrLen(foo);
end;

exports
  TestCall;

begin
end.
Run Code Online (Sandbox Code Playgroud)

使用以上库的Powershell脚本

$signature = @'
[DllImport(@"C:\Desktop\TestDLL.DLL", CharSet=CharSet.Unicode)]
public static extern int TestCall(string foo);
'@;

$type = Add-Type -MemberDefinition $signature -Name Win32Utils -Namespace TestDLL -PassThru;

[int] $retval = $type::TestCall("test string");
Write-Host($retval);
Run Code Online (Sandbox Code Playgroud)

现在,我真的不是Powershell专家,所以这可能是草率的.希望它证明了这一点.对于更复杂的参数类型,您需要更高级的Powershell代码,但网上有很多示例.