C#要对数据进行封送处理,因为这涉及到托管与非托管代码的交互。
//! 导入C++ DLL内函数
[DllImport("CPlusPlusCSharpMix.dll", EntryPoint = "Init")]
public static extern bool Init();
[DllImport("CPlusPlusCSharpMix.dll")]
public static extern bool Run();
[DllImport("CPlusPlusCSharpMix.dll")]
public static extern bool Clean();
//[DllImport("CPlusPlusCSharpMix.dll")]
//DLL_API bool Init1(std::string str);
[DllImport("CPlusPlusCSharpMix.dll")]
public static extern bool Init2(int a);
[DllImport("CPlusPlusCSharpMix.dll")]
public static extern bool Init3(int a);
[DllImport("CPlusPlusCSharpMix.dll")]
public static extern bool Init4(ref int[] a);
[DllImport("CPlusPlusCSharpMix.dll")]
public static extern bool Init5(ref int a);
const string testDLL = @"CPlusPlusCSharpMix.dll";
[StructLayout(LayoutKind.Sequential)]
public class MyOGRE
{
//该方式是直接调用C++ DLL内的类的成员函数。
[DllImport("CPlusPlusCSharpMix.dll", EntryPoint = @"?MyOGREHello@MyOGRE@@QAEXXZ")]
public static extern void MyOGREHello();
[DllImport("CPlusPlusCSharpMix.dll", EntryPoint = @"?LoadLuaScript@MyOGRE@@QAE_NPAD@Z")]
public static extern bool LoadLuaScript([MarshalAs(UnmanagedType.LPStr)]string filename);
[DllImport("CPlusPlusCSharpMix.dll", EntryPoint = @"?SetNewParent@MyOGRE@@QAEPAUHWND__@@HH@Z")]
public static extern int SetNewParent(int hwnd, IntPtr newparent);
[DllImport(testDLL, EntryPoint = @"?TestEntry@MyOGRE@@QAEXPAUHWND__@@@Z")]
public static extern void TestEntry(IntPtr hwnd);
[DllImport(testDLL, EntryPoint = @"?Render@MyOGRE@@QAEXXZ")]
public static extern void Render();
}
[DllImport("CPlusPlusCSharpMix.dll")]
public static extern MyOGRE GetOGREInst();
}
}