开发者

Use c++ libraries at C#

I'm working myself on a project, because performance is important i want to write some part of the code on c++ and want to call c++ codes from C#. I will write c++ library and want to write a C# wrapper, how can i do it? Have you an开发者_开发知识库y suggestion where i should begin to study? [any website, any link, any book]

Thanks !


Example:

Windows:

[DllImport("User32.dll")]
public static extern void SetWindowText(int h, String s);



Linux:

[System.Runtime.InteropServices.DllImport("/root/Desktop/q3noclient/libnoclient.so")]
static extern int DisconnectClient (string strServerIP, int iServerPort, string strClientIP, int iClientPort);



C#:

[DllImport("Comdlg32.dll", CharSet=CharSet.Auto, SetLastError=true)]
private static extern bool GetSaveFileName(ref OPENFILENAME lpofn);



VB.NET:

<DllImport("Comdlg32.dll", CharSet:=CharSet.Auto, SetLastError:=True)> _
Private Shared Function GetSaveFileName(ByRef lpofn As OPENFILENAME) As Boolean
End Function


You need to either make C style imports for P/Invoke to use, like

void World_Hello(World* self) { self->Hello(); }

or investigate CallingConvention.ThisCall. Thanks to name mangling, if you want to be portable to other OSes the former is a better choice.


Take a look at C++/CLI. I would recommend C++/CLI in Action by Nishant Sivakumar. Also, check this link out: http://msdn.microsoft.com/en-us/magazine/cc163855.aspx#S7

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜