简体   繁体   中英

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 any 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM