簡體   English   中英

我可以將用Delphi編寫的DLL加載到PowerShell中嗎?

[英]Can I load a DLL written in Delphi into 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.

您的Powershell代碼適用於托管程序集。 但是您的Delphi庫是一個非托管DLL。 要直接訪問它,請使用pinvoke。 一個簡單的例子:

Delphi庫

library TestDLL;

uses
  SysUtils;

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

exports
  TestCall;

begin
end.

使用以上庫的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);

現在,我真的不是Powershell專家,所以這可能是草率的。 希望它證明了這一點。 對於更復雜的參數類型,您需要更高級的Powershell代碼,但網上有很多示例。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM