首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >2 **指针的C# PInvoke

2 **指针的C# PInvoke
EN

Stack Overflow用户
提问于 2013-11-21 16:33:07
回答 1查看 176关注 0票数 1

我正在使用一个3.PartySDK,它由.dll、.lib和.h文件组成。我正在使用..dll来反对PInvoke。以及用于查看函数名称和参数的.h文件。(所以我不使用.lib文件)。

SDK相当复杂,因此PInvoke包装器的制作已经证明是一个挑战。所有函数/structs/enum都定义在.h文件中。

我的问题是如何为带有2 **的函数实现pinvoke。

我想是我的C#函数定义错了。当我调用该函数时,它简单地崩溃,没有异常抛出或任何东西。节目就这样停止了。

函数:GetInformatiuon(.)

代码语言:javascript
复制
//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

代码语言:javascript
复制
//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...
 }

结构:组件

代码语言:javascript
复制
//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)
}

结构:信息

代码语言:javascript
复制
 //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)
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-11-21 17:01:12

我找到解决办法了!(又名汉斯·帕桑特评论)

我将该函数更改为:

函数:GetInformatiuon(.)

代码语言:javascript
复制
//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);
);

调用函数

代码语言:javascript
复制
IntPtr infoPtr;
lhErr = SDK.GetInformatiuon(component, out infoPtr);
Information info = (Information)Marshal.PtrToStructure(infoPtr, typeof(Information)) ;
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20126647

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档