Sunday, July 12, 2009

Can I use the C++ library in C# ,with samples please??

You can't use C++ classes directly, you'll need to wrap them using the


managed extensions to C++.








Try PInvoke( platform invoke). Using platform invoke, .NET Framework


programs can access native code libraries by means of static DLL entry


points. Platform invoke provides the mechanism for finding and invoking


unmanaged functions, as well as marshaling managed arguments to and from


unmanaged code. Hence you can leverage the prior investment in unmanaged


code in .NET applications.





To call a function that's implemented in an Unmanaged DLL





1. Add a using statement for System.Runtime.InteropServices


2. Add a DllImport attribute to a method, specifying the name of the


unmanaged DLL that exports the function to be called


3. Declare a method that's used to call unmanaged code without providing


any implementation for the method.





Here is an example of C# calling the Win32 MessageBox function:





using System;


using System.Runtime.InteropServices;





class MainApp


{


[DllImport("user32.dll", EntryPoint="MessageBox")]


public static extern int MessageBox(int hWnd, String strMessage, String


strCaption, uint uiType);





public static void Main()


{


MessageBox( 0, "Hello, this is PInvoke in operation!", ".NET", 0 );


}


}





For more information, please consult MSDN documentations.

Can I use the C++ library in C# ,with samples please??
Nope two different syntex.. The only way would be if the complier you are using has support for C++.
Reply:I think the better option would be to create a DLL out of the C++ library and add it as a reference(by Visual Studio) in the C# code as COM object.

song titles

No comments:

Post a Comment