2011-12-07 63 views
-2

我想從C++這個結構複製到C#:元帥C++結構在C#參考

 typedef struct 
     { 
      int id; 
      char *name; 
     } *ListOfObjects; 

我一直在使用這種嘗試,但它不是在應用程序中正確導入目前正使用該DLL和期待爲特定的簽名。

[StructLayout(LayoutKind.Sequential), Serializable] 
    public struct ListOfObjects { 
     [MarshalAsAttribute(UnmanagedType.ByValArray)] 
     public int id; 

     [MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst = 15)] 
     public string name; 
    } 

    [DllExport("ReadListOfObjects", CallingConvention = CallingConvention.Cdecl)] 
    static ListOfObjects ReadListOfObjects() 
    { 
     ListOfObjects lists = new ListOfObjects(); 
     return lists; 
    } 

我編譯DLL,然後嘗試啓動導入真實這些功能它給這個錯誤的程序之後:

The prodedure entry point ReadListOfObjects could not be located in the dynamic link library thedll.dll. 

任何想法?

+0

的的MarshalAs屬性是嚴重錯誤的。但是你還沒有接近它,它還找不到這個功能。您只顯示結構,而不顯示ReadListOfObjects()函數的C聲明。我們無法猜測它。 –

回答

2

試試這個:

[StructLayout(LayoutKind.Sequential), Serializable] 
public struct ListOfObjects 
{ 
    public int id; 

    [MarshalAs(UnmanagedType.LPStr)] 
    public string name; 
} 
+0

與以前一樣的錯誤。我將通過如何定義使用此數據結構的接口來更新這個問題。 – Dave

+0

@Dave:這個錯誤與'ListOfObjects'無關,它與'TI_ReadListOfLists'在非託管代碼中聲明的方式有關(可能需要C鏈接並且具有C++鏈接)。向我們展示您的非託管功能。 (另外,'DllExport'顯然是荒謬的 - 顯示你的__real__代碼,不是很差的近似值。) – ildjarn

+0

其實這是我的實際代碼。我使用這個:https://sites.google.com/site/robertgiesecke/Home/uploads/unmanagedexports – Dave