c# P/Invoke和包含字节数组的结构数组
本文关键字:数组 字节数 结构 字节 包含 Invoke | 更新日期: 2023-09-27 18:11:00
我需要从c#代码调用本地DLL。由于我对C/c++不太熟悉,我不知道在C中定义的结构应该如何在c#中声明,以便可以调用它。问题是两个参数似乎是一个结构体数组,我不知道如何在c#中声明这个(见最后一个代码块):
c++头文件
typedef enum
{
OK = 0,
//others
} RES
typedef struct
{
unsigned char* pData;
unsigned int length;
} Buffer;
RES SendReceive(uint32 deviceIndex
Buffer* pReq,
Buffer* pResp,
unsigned int* pReceivedLen,
unsigned int* pStatus);
c#声明:enum
{
OK = 0,
//others
} RES
struct Buffer
{
public uint Length;
public ??? Data; // <-- I guess it's byte[]
}
[DllImport("somemodule.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern uint SendReceive(
uint hsmIndex,
uint originatorId,
ushort fmNumber,
??? pReq, // <-- should this be ref Buffer[] ?
uint reserved,
??? pResp, // <-- should this be ref Buffer[] ?
ref uint pReceivedLen,
ref uint pFmStatus);
在一个等效的java客户端中,我发现参数不仅仅是一个Buffer,而是一个Buffer数组。在c#中,它看起来像这样:
var pReq = new Buffer[]
{
new Buffer { Data = new byte[] { 1, 0 }, Length = (uint)2 },
new Buffer {Data = requestStream.ToArray(), Length = (uint)requestStream.ToArray().Length },
//according to the header file, the last item must be {NULL, 0}
new Buffer { Data = null, Length = 0 }
};
var pResp = new Buffer[]
{
new Buffer { Data = new byte[0x1000], Length = 0x1000 },
//according to the header file, the last item must be {NULL, 0}
new Buffer { Data = null, Length = 0x0 }
};
这对我来说似乎很奇怪,因为extern C方法确实有一个指向Buffer结构体(Buffer*)的指针,而不是指向Buffer数组(Buffer[]*)的指针。如何在c#中定义Struct和extern方法的参数类型?
感谢您的帮助。
首先,结构体的参数顺序错误。并且字节数组需要通过手动封送声明为IntPtr
:
struct Buffer
{
public IntPtr Data;
public uint Length;
}
p/调用应该是:
[DllImport("MyNativeDll.dll", CallingConvention=CallingConvention.Cdecl)]
static extern RES SendReceive(
uint deviceIndex,
[In] Buffer[] pReq,
[In, Out] Buffer[] pResp,
out uint pReceivedLen,
out uint pStatus
);
字节数组需要是IntPtr
,这样结构体是可比特的。这是必要的,以便数组参数可以声明为Buffer[]
。
对字节数组进行编组将会有点麻烦。您需要使用GCHandle
来固定托管字节数组,并调用AddrOfPinnedObject()
来获取结构数组中每个结构的固定数组的地址。在编写一些帮助函数以使该任务不那么痛苦时,这将是值得的。
你在c#中的方法签名应该是这样的:
[DllImport("MyNativeDll.dll")]
public static extern RES SendReceive (uint32 deviceIndex, ref Buffer pReq, ref Buffer pResp, ref uint pReceivedLen, ref uint pStatus);
看看这个项目,它可能会帮助你在未来从。net生成原生调用http://clrinterop.codeplex.com/releases/view/14120
基于c++头文件,但没有经过测试,请看下面的代码:
using System;
using System.Runtime.InteropServices;
using System.Text;
namespace WindowsFormsApplication1
{
public class Class1
{
public struct Buffer
{
[MarshalAs(UnmanagedType.LPStr)]
public StringBuilder pData;
public uint length;
}
[DllImport("kernel32.dll", EntryPoint = "LoadLibrary")]
static extern int LoadLibrary(string lpLibFileName);
[DllImport("kernel32.dll", EntryPoint = "GetProcAddress")]
static extern IntPtr GetProcAddress(int hModule, string lpProcName);
[DllImport("kernel32.dll", EntryPoint = "FreeLibrary")]
static extern bool FreeLibrary(int hModule);
[UnmanagedFunctionPointer(CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal delegate IntPtr SendReceive(
uint deviceIndex,
ref Buffer pReq,
ref Buffer pResp,
uint pReceivedLen,
uint pStatus);
public void ExecuteExternalDllFunction()
{
int dll = 0;
try
{
dll = LoadLibrary(@"somemodule.dll");
IntPtr address = GetProcAddress(dll, "SendReceive");
uint deviceIndex = 0;
Buffer pReq = new Buffer() { length = 0, pData = new StringBuilder() };
Buffer pResp = new Buffer() { length = 0, pData = new StringBuilder() };
uint pReceivedLen = 0;
uint pStatus = 0;
if (address != IntPtr.Zero)
{
SendReceive sendReceive = (SendReceive)Marshal.GetDelegateForFunctionPointer(address, typeof(SendReceive));
IntPtr ret = sendReceive(deviceIndex, ref pReq, ref pResp, pReceivedLen, pStatus);
}
}
catch (Exception Ex)
{
//handle exception...
}
finally
{
if (dll > 0)
{
FreeLibrary(dll);
}
}
}
}
}