我正在使用一个3.PartySDK,它由.dll、.lib和.h文件组成。我正在使用..dll来反对PInvoke。以及用于查看函数名称和参数的.h文件。(所以我不使用.lib文件)。
SDK相当复杂,因此PInvoke包装器的制作已经证明是一个挑战。所有函数/structs/enum都定义在.h文件中。
我的问题是如何为带有2 **的函数实现pinvoke。
我想是我的C#函数定义错了。当我调用该函数时,它简单地崩溃,没有异常抛出或任何东西。节目就这样停止了。
函数:GetInformatiuon(.)
//C Function: GetInformatiuon(...)
ERROR GetInformatiuon(
Component comp,
struct Information** Info);
//C# Function: GetInformatiuon(...)
[DllImport("externalSDK.dll", EntryPoint = "GetInformatiuon", CallingConvention = CallingConvention.Cdecl)]
public static extern ERROR GetInformatiuon(Component comp, ref Information Info);
);Enum: ERROR
//C Enum: ERROR
typedef enum ERROR_E {
OK = 0, //Everything is ok
E_ARG = 1, //Error in the Arguments
E_DATA = 2 //Data error
//And more...
} ERROR;
//C# Enum: ERROR
public enum ERROR
{
OK = 0, //Everything is ok
E_ARG = 1, //Error in the Arguments
E_DATA = 2 //Data error
//And more...
}结构:组件
//C struct: Component
typedef struct Component_S
{
void* ObjPointer;
unsigned long number;
} Component;
//C# class: Component
[StructLayout(LayoutKind.Sequential)]
public class Component
{
public IntPtr ObjPointer;
public uint number; //uint because usigned long C is 4 bytes (32 bits) and C# ulong is 8 bytes (64 bits), where C# uint is 4 bytes(32 bits)
}结构:信息
//C struct: Information
typedef struct Information_S {
char* language;
unsigned long sampleFrequency;
unsigned long frameShift;
}Information;
//C# struct: Information
[StructLayout(LayoutKind.Sequential)]
public struct Information
{
public string language;
public uint sampleFrequency; //uint because usigned long C is 4 bytes (32 bits) and C# ulong is 8 bytes (64 bits), where C# uint is 4 bytes(32 bits)
public uint frameShift; //uint because usigned long C is 4 bytes (32 bits) and C# ulong is 8 bytes (64 bits), where C# uint is 4 bytes(32 bits)
}发布于 2013-11-21 17:01:12
我找到解决办法了!(又名汉斯·帕桑特评论)
我将该函数更改为:
函数:GetInformatiuon(.)
//C Function: GetInformatiuon(...)
ERROR GetInformatiuon(
Component comp,
struct Information** Info);
//C# Function: GetInformatiuon(...)
[DllImport("externalSDK.dll", EntryPoint = "GetInformatiuon", CallingConvention = CallingConvention.Cdecl)]
public static extern ERROR GetInformatiuon(Component comp, out IntPtr InfoPtr);
);调用函数
IntPtr infoPtr;
lhErr = SDK.GetInformatiuon(component, out infoPtr);
Information info = (Information)Marshal.PtrToStructure(infoPtr, typeof(Information)) ;https://stackoverflow.com/questions/20126647
复制相似问题