我非常接近我的问题的解决方案,但我需要一些关于最后润色的指导以使一切正常。在过去的一周里,我学到了很多关于
作为参考,我上周就同一主题问了一个类似的问题,但由于我这边的巨大疏忽,我问错了问题。
我正在尝试使用非托管 c++ dll,它是与连接设备通信的 API。我已经成功地创建了包装器和大多数其他函数调用,但最后一个让我发疯了。
对于一些背景信息(可能不需要回答这个问题 - 请记住我当时的基本思维过程有缺陷)在这里:Calling un-managed code with pointer (Updated)
在我最初的问题中,我问的是如何为包含 struct(2) 数组的 struct(1) 创建一个 IntPtr。事实上,struct(1) 根本不包含一个数组,它包含一个指向数组。
这是我试图作为参考实现的 API 的文档:
extern “C” long WINAPI PassThruIoctl
(
unsigned long ChannelID,
unsigned long IoctlID,
void *pInput,
void *pOutput
)
// *pInput Points to the structure SCONFIG_LIST, which is defined as follows:
// *pOutput is not used in this function and is a null pointer
typedef struct
{
unsigned long NumOfParams; /* number of SCONFIG elements */
SCONFIG *ConfigPtr; /* array of SCONFIG */
} SCONFIG_LIST
// Where:
// NumOfParms is an INPUT, which contains the number of SCONFIG elements in the array pointed to by ConfigPtr.
// ConfigPtr is a pointer to an array of SCONFIG structures.
// The structure SCONFIG is defined as follows:
typedef struct
{
unsigned long Parameter; /* name of parameter */
unsigned long Value; /* value of the parameter */
} SCONFIG
这是我目前定义的结构定义
[StructLayout(LayoutKind.Sequential)] // Also tried with Pack=1
public struct SConfig
{
public UInt32 Parameter;
public UInt32 Value;
}
[StructLayout(LayoutKind.Sequential)] // Also tried with Pack=1
public struct SConfig_List
{
public UInt32 NumOfParams;
public IntPtr configPtr;
public SConfig_List(UInt32 nParams, SConfig[] config)
{
this.NumOfParams = nParams;
// I have tried these 2 lines together
IntPtr temp = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(MyNameSpace.SConfig)) * (int)nParams);
this.configPtr = new IntPtr(temp.ToInt32());
// I have tried this by itself
// this.configPtr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(MyNameSpace.SConfig)) * (int)nParams);
// and this line
// this.configPtr = Marshal.AllocHGlobal(sizeof(SConfig)*(int)nParams); // this only complies with unsafe struct
}
}
这是将这些设置为变量并调用与 API 接口的函数的代码片段
SConfig[] arr_sconfig;
arr_sconfig = new SConfig[1];
arr_sconfig[0].Parameter = 0x04;
arr_sconfig[0].Value = 0xF1;
SConfig_List myConfig = new SConfig_List(1, arr_sconfig);
m_status = m_APIBox.SetConfig(m_channelId, ref myConfig);
最后,这里是将这些信息传递给 dll 的函数:
public APIErr SetConfig(int channelId, ref SConfig_List config)
{
unsafe
{
IntPtr output = IntPtr.Zero; // Output not used, just a null pointer for this function
// These 2 lines of code cause API dll to yell about invalid pointer (C# is happy but it doesnt work with dll)
// IntPtr temp = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(J_2534_API.SConfig_List)));
// IntPtr input = new IntPtr(temp.ToInt32());
// The following 2 lines only compile with unsafe - but API dll "likes" the pointer - but I am not getting desired results
// The dll is properly getting the Number of Parameters (NumOfParams), but the data within the array is not being
// referenced correctly
IntPtr input = Marshal.AllocHGlobal(sizeof(SConfig_List)); // Only works with unsafe
Marshal.StructureToPtr(config, input, true);
APIErr returnVal = (APIErr)m_wrapper.Ioctl(channelId, (int)Ioctl.SET_CONFIG, input, output);
return returnVal;
}
}
在我意识到我对基本概念的巨大疏忽之前,我什至无法让 C# 高兴,要么我的语法错误并且代码无法编译,要么它会编译但给出了运行时错误(甚至从不调用外部 dll )
这些问题都在我身后。该代码现在可以正常编译,并且可以在没有任何运行时错误的情况下执行。此外,我使用的 dll 有一个日志功能,所以我可以看到我实际上调用了正确的函数。我什至正确地将一些数据传递给它。该函数正在正确读取 NumOfParams 变量,但结构数组似乎是垃圾数据。
我在这里阅读了一篇非常有用的文章:http: //limbioliong.wordpress.com/2012/02/28/marshaling-a-safearray-of-managed-structures-by-pinvoke-part-1/
而且我一直在阅读 MSDN,但到目前为止,我还没有遇到使这件事起作用的神奇代码组合,所以我再次寻求帮助。
我很确定我的问题是我没有正确设置 IntPtr 变量,并且它们没有指向内存中的正确区域。
我尝试了不安全和安全代码的各种组合。另外,我知道此时我并没有明确释放内存,因此对此的指针也会有所帮助。在我的研究中,这里有一些可能可行的想法,但我似乎无法让它们恰到好处
[MarshalAs(UnmanagedType.LPWStr)]
[MarshalAs(UnmanagedType.ByValArray, SizeConst=...)]
[MarshalAs(UnmanagedType.ByValArray, ArraySubType = UnmanagedType.Struct, SizeConst=100)]
最后一个问题:我假设由于 c++ 声明是无符号长的,那么 UInt32 是 C# 中的正确类型吗?