2011-12-07 76 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