我在C ++中有一个带有如下函数的DLL:
LesMes.hpp
bool Sing_For_Me(CInstructment accompaniedBy, string song, string singer);
同时在我的WPF应用程序中,我将使用互操作层:
[ DllImport( "musicals.dll", EntryPoint="Sing_For_Me", CharSet=CharSet.Ansi )]
public static extern bool SingForMe(???????, string note, string singer);
如何在WPF代码中引用 CInstrument ?
似乎有很多字符串被使用的例子,你只能使用某些预先定义的数据类型吗?
答案 0 :(得分:1)
如何定义CInstructment? 如果它是一个结构,你可以很容易编组。创建一个新的struct应用StructLayout(Sequential)作为Attribute(请参阅System.Runtime.InteropServices),然后将其传递给您的interop。为什么你有bool的返回类型IntPtr?
您还可以在此处找到更多信息 http://www.developerfusion.com/article/84519/mastering-structs-in-c/
答案 1 :(得分:1)
对于C ++类,没有任何直接的P / Invoke方式,但这就是制作C ++ \ CLI的原因。
您应该创建一个C ++ \ CLI类项目,然后可以将CInstrument
包装到.NET类中,然后在WPF中使用它。
请参阅此codeproject article for more info
原生C ++代码只适用于C ++ \ CLI,有一种称为IJW的东西(它只是起作用)。另请参阅this article for more。
您需要添加新项目。中选择:
其他语言> Visual C ++> CLR>班级图书馆
您的代码如下所示:
//native C++ CInstrument
//cinstrument.cpp
class CInstrument
{
..
}
//this is a CLR class
//Compatible with C#/VB.NET
//cinstrumentwrapper.cpp
ref class CInstumentWrapper
{
private:
CInstrument* _instrument;//delegate to the native C++ class
}