簡體   English   中英

powershell調用帶有結構類型參數的c++ dll的導出函數

[英]powershell calling exported function of a c++ dll with structure type parameter

struct foostruct
{
    int B;
    int A;
    int D;
};

int foofunc(foostruct foo)
{
    return foo.A;
}

我已經從test.dll導出了FooFunc現在我試圖從 powershell 調用它

$source =  @"
public struct foostruct {
public int B;
public int A;
public int D;
}
"@
Add-Type -TypeDefinition $Source -IgnoreWarnings
$bar = New-Object foostruct
$bar.B = 1;
$bar.A = 2;
$bar.D = 3;

$MethodDefinition = @'

[DllImport("test.dll", CharSet = CharSet.Unicode, SetLastError = true)]

public static extern int foofunc(foostruct foo);

'@

$dll = Add-Type -MemberDefinition $MethodDefinition -Name 'dll' -PassThru -IgnoreWarnings

這會引發錯誤

The type or namespace name 'foostruct' could not be found (are you missing a using directive or an assembly reference?)

我嘗試將fostruct的定義移動到$MethodDefinition ,如此處所述但是當我使用下面的調用它時,我得到Unable to cast object of type 'System.Object[,]' to type 'System.Object[]'錯誤。

$Result = $dll::foofunc($bar)

The type or namespace name 'foostruct' could not be found (are you missing a using directive or an assembly reference?)兩個單獨的Add-Type s The type or namespace name 'foostruct' could not be found (are you missing a using directive or an assembly reference?) " 程序集不知道“TypeDefinition”程序集,因此無法將foostruct類型解析為實現。

嘗試 1 - 失敗

因此,讓我們根據您鏈接到答案將這兩個聲明移動到一個“MemberDefinition” Add-Type

$source = @"
public struct foostruct
{
    public int B;
    public int A;
    public int D;
}
[DllImport("test\\test\\x64\\Debug\\test.dll", CharSet=CharSet.Unicode, SetLastError=true)]
public static extern int foofunc(foostruct foo);
"@
Add-Type -MemberDefinition $Source -Name "dll";

$bar = New-Object foostruct
$bar.B = 1;
$bar.A = 2;
$bar.D = 3;

$dll::foofunc($bar);

運行foostruct Add-Type工作正常,但嘗試使用foostruct會出現此錯誤:

New-Object : Cannot find type [foostruct]: verify that the assembly containing this type is loaded.
At C:\src\so\pinvoke\test.ps1:25 char:8
+ $bar = New-Object foostruct
+        ~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidType: (:) [New-Object], PSArgumentException
    + FullyQualifiedErrorId : TypeNotFound,Microsoft.PowerShell.Commands.NewObjectCommand

foostruct類型對DllImport可見,但對 PowerShell 不可見:-(

嘗試 2 - 失敗

好的,讓我們嘗試將其作為“TypeDefinition” Add-Type

$source = @"
public struct foostruct
{
    public int B;
    public int A;
    public int D;
}
[DllImport("test\\test\\x64\\Debug\\test.dll", CharSet=CharSet.Unicode, SetLastError=true)]
public static extern int foofunc(foostruct foo);
"@
Add-Type -TypeDefinition $Source;

$bar = New-Object foostruct
$bar.B = 1;
$bar.A = 2;
$bar.D = 3;

由於“浮動” [DllImport]編譯器希望將其包裝在一個類中,這會導致Add-Type出現編譯錯誤:

Add-Type : c:\Users\Mike\AppData\Local\Temp\cchnscq4\cchnscq4.0.cs(14) : Expected class, delegate, enum, interface, or
struct
c:\Users\Mike\AppData\Local\Temp\cchnscq4\cchnscq4.0.cs(13) :     [DllImport("test\\test\\x64\\Debug\\test.dll",
CharSet=CharSet.Unicode, SetLastError=true)]
c:\Users\Mike\AppData\Local\Temp\cchnscq4\cchnscq4.0.cs(14) : >>>     public static extern int foofunc(foostruct foo);
c:\Users\Mike\AppData\Local\Temp\cchnscq4\cchnscq4.0.cs(15) :
At C:\src\so\pinvoke\test.ps1:22 char:1
+ Add-Type -TypeDefinition $Source;
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (Microsoft.Power...peCompilerError:AddTypeCompilerError) [Add-Type], Except
   ion
    + FullyQualifiedErrorId : SOURCE_CODE_ERROR,Microsoft.PowerShell.Commands.AddTypeCommand

嘗試 3 - 成功

我們可以在上面的“TypeDefinition” Add-Type版本中調整$Source字符串以使編譯器滿意:

$source =  @"
using System.Runtime.InteropServices;

public struct foostruct
{
    public int B;
    public int A;
    public int D;
}

public static class NativeMethods
{

    [DllImport("test\\test\\x64\\Debug\\test.dll", CharSet=CharSet.Unicode, SetLastError=true)]
    public static extern int foofunc(foostruct foo);

}
"@
Add-Type -TypeDefinition $Source;

$bar = New-Object foostruct
$bar.B = 1;
$bar.A = 2;
$bar.D = 3;

[NativeMethods]::foofunc($bar);

請注意, foofunc現在是[NativeMethods]類的靜態成員。

這個版本的輸出如下:

2

這基本上是 C++ 函數返回的$bar.A值,所以一切似乎都按要求工作。

筆記

我無法重現您的Unable to cast object of type 'System.Object[,]' to type 'System.Object[]'錯誤,所以我無法真正解釋這一點,但希望上面的答案可以幫助您解決問題。 ..

暫無
暫無

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

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