We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

How to use C++ class with C#

I have a class library written in C++ which I intend to use in a program whose interface is designed in C#. I understand that I need to create an unmanaged DLL and use the DLL in the program. Are there any resources which show how to create a unmanaged C++ DLL in .NET environment without using MFC and how I could use the DLL with C#? MSDN site suggests wrapping the unmanaged DLL function in a managed class but doesnt give any much useful information. Thanks Mukesh

3 Replies

CB Clay Burch Syncfusion Team May 28, 2002 06:17 AM UTC

Here is a Microsoft link explaining how to use P/Invoke with DLLImport to access functions in unmanaged DLLs. http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconconsumingunmanageddllfunctions.asp If you have a active X library (an OCX or ATL DLL)you are trying to use, then you can create the .NET wrapper class using the tool aximp.exe.


MM Mukesh Motwani June 4, 2002 06:36 AM UTC

I created an unmanaged DLL which has a C++ class. PInvoke is able to access global exported DLL functions but it CANNOT import class methods from an unmanged DLL. Thus, I get an error "Object reference not set to an instance of an object" because I am not able to create an object of my class. In VC++ 6 I could use the header file and create an instance. But C# doesnt allow that. In fact, [DllImport("mywin32.dll")] public static extern Cwin32class(); is not compiled because the compiler thinks that method has no return type! But constructors are not supposed to have return types. Thus I cant find any way of using my unmanaged object oriented classes with the C# UI. Can anyone help me please? Thanks Mukesh


MI Michael May 17, 2003 12:10 AM UTC

Mukesh, I have the same problem when I try to use a C++ DLL. Have you found out what the problem could be and how to instantiate the wrapper class or the object? Regards. Michael. > I created an unmanaged DLL which has a C++ class. > PInvoke is able to access global exported DLL functions but it CANNOT import class methods from an unmanged DLL. Thus, I get an error "Object reference not set to an instance of an object" because I am not able to create an object of my class. In VC++ 6 I could use the header file and create an instance. But C# doesnt allow that. In fact, > > [DllImport("mywin32.dll")] > public static extern Cwin32class(); > > is not compiled because the compiler thinks that method has no return type! But constructors are not supposed to have return types. Thus I cant find any way of using my unmanaged object oriented classes with the C# UI. Can anyone help me please? > > Thanks > Mukesh

Loader.
Live Chat Icon For mobile
Up arrow icon