【发布时间】:2021-10-08 16:49:37
【问题描述】:
这里是c++ dll代码:
#include<stdlib.h>
#include "pch.h"
extern "C" _declspec(dllexport) int Add(int a, int b)
{
return a + b;
}
我在 Visual Studio 中运行的 C# 代码:
[DllImport(@"C:\Dll1.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
public static extern int Add(int port, int speed);
public static void Main()
{
int res = Add(8, 11);
Console.WriteLine(res);
}
没问题,输出:19
PowerShell 代码:
Add-Type -Name MyFunctions -Namespace Cpp @'
[DllImport(@"C:\Dll1.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
public static extern int Add(int port, int speed);
'@
[Cpp.MyFunctions]::Add(8, 11)
输出:
An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)
但是从 user32.dll/kernel32.dll 导入的函数没有任何问题。
如何解决?
【问题讨论】:
标签: c# c++ .net powershell