C#调用返回结构的C ++ DLL函数

时间:2013-06-10 09:14:34

标签: c# c++ dll call

我有一个C ++ dll,它定义了一个struct和一个dll调用:

typedef const char* FString;

typedef struct {
    FString version;
    FString build_no;
    FString build_type;
    FString build_date;
    FString build_info;
    FString comment;
} FVersionInfo;

extern "C" FAPI_EXPORT FVersionInfo CALLINGCONV fGetVersion(void);

在c#侧我使用动态加载:

    [DllImport("kernel32.dll", EntryPoint = "LoadLibrary")]
    static extern int LoadLibrary(
        [MarshalAs(UnmanagedType.LPStr)] string lpLibFileName);

    [DllImport("kernel32.dll", EntryPoint = "GetProcAddress")]
    static extern IntPtr GetProcAddress(int hModule,
        [MarshalAs(UnmanagedType.LPStr)] string lpProcName);

    [DllImport("kernel32.dll", EntryPoint = "FreeLibrary")]
    static extern bool FreeLibrary(int hModule);

    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
    public struct FVersionInfo
    {
        public string Version;
        public string Build_No;
        public string Build_Type;
        public string Build_Date;
        public string Build_Info;
        public string Comment;
    }

    [UnmanagedFunctionPointer(CallingConvention.StdCall, CharSet = CharSet.Ansi)]
    public delegate FVersionInfo fGetVersion();

    public fGetVersion GetVersion;

    FHandle = LoadLibrary(@pName);
    IntPtr intPtr;
    intPtr = GetProcAddress(FHandle, "fGetVersion");
    GetVersion = (fGetVersion)Marshal.GetDelegateForFunctionPointer(intPtr, typeof(fGetVersion));

调用代码应为:

FVersionInfo version = new FVersionInfo();
version = GetVersion();

我的第一个问题是,在c#loading部分中调用Marshal.GetDelegateForFunctionPointer时,我变成了“System.Runtime.InteropServices.MarshalDirectiveException”。

然后我使用IntPtr测试结构返回参数,如:

[UnmanagedFunctionPointer(CallingConvention.StdCall, CharSet = CharSet.Ansi)]
public delegate IntPtr fGetVersion();

所以我让Marshal.GetDelegateForFunctionPointer工作,但后来我遇到了编组问题:

IntPtr DllValue = new IntPtr();
FVersionInfo version = new FVersionInfo();
DllValue = fGetVersion();
Marshal.PtrToStructure(DllValue, FVersionInfo);

此处它在fGetVersion()调用时遇到“托管调试助手'PInvokeStackImbalance'”崩溃。我认为这意味着堆栈已损坏(不平衡)。

我已经测试了许多结构定义的变体,但没有结果。

欢迎任何想法或建议!

2 个答案:

答案 0 :(得分:1)

感谢您的指导,但我找到了一个有效的解决方案:

  1. 我将结构的声明更改为
  2. [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
    public struct FVersionInfo
    {
        public IntPtr Version;
        public IntPtr Build_No;
        public IntPtr Build_Type;
        public IntPtr Build_Date;
        public IntPtr Build_Info;
        public IntPtr Comment;
    }
    
    1. 所以我毫无问题地通过了Marshal.GetDelegateForFunctionPointer

    2. 我将使用代码更改为:

    3. GF.FVersionInfo vi = new GF.FVersionInfo();
      vi = gf.GetVersion();
      
      1. 之后,我可以使用
      2. 访问字符串
          

        string MyVersion = Marshal.PtrToStringAnsi(VersionInfos.Version);

答案 1 :(得分:0)

将结构体与const char *成员一起使用时的示例。在这种情况下,您将拥有自己的C ++ DLL结构来回传递。

// CPP Source code
// ----------------------------------

// Struct definition
struct MyStruct_t {
    const char * sStr;
    size_t       iInt;
};
// ----------------------------------


// C# Source code
// ----------------------------------

// Struct definition
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]       
public struct MyStruct_t {
    public string sStr;
    public int    iInt;
};

// DLL prototype
[DllImport("Cpp.dll", CallingConvention = CallingConvention.StdCall)]
public static extern void MyFunctionCall(IntPtr MyStruct_t);

// Prepare a struct pointer to pass to DLL
MyStruct_t pMyStruct_t = new MyStruct_t();
IntPtr pMyStructPTR    = Marshal.AllocHGlobal(Marshal.SizeOf(pMyStruct_t));
Marshal.StructureToPtr(pMyStruct_t, pMyStructPTR, false);

// Call C++ DLL
MyFunctionCall(pMyStructPTR);

// The DLL updated struct, get the struct
MyStruct_t pMyStruct = (MyStruct_t)Marshal.PtrToStructure(pMyStructPTR, typeof(MyStruct_t));

// Show data
MessageBox.Show(pMyStruct.sStr);

// clean up
if (pMyStructPTR != IntPtr.Zero) { Marshal.FreeHGlobal(pMyStructPTR); pMyStructPTR = IntPtr.Zero; }
// ----------------------------------