To the best of my knowledge, there's no way to call a C++ constructor using P/Invoke (i.e. the DllImport attribute).
If the C++ class contains a static 'factory' method which returns a pointer to the created instance, then you might be able to call that first and then use the resulting pointer to call a member function of the class.
To do that, you'd need to pass the pointer (use the IntPtr type) as the first argument to the member function and specify the 'ThisCall' calling convention in the DllImport attribute.
However, the more usual ways to create unmanaged C++ objects from C# are:
1. To convert them to COM objects and then use COM interop to instantiate them; or
2. To create managed 'wrappers' for them using C++/CLI , add a reference to the resulting dll and instantiate them as you would any other managed class.
2 answers
Here is a code snippet from a Windows Impersonation DLL that I had to import. This is from the Program.cs file
[DllImport("advapi32.dll", SetLastError = true)]
Not sure if this helps or not?
answered 2 years ago by:
15
0
Thanx for u r reply... bt i want to create the object of that DLL.
To the best of my knowledge, there's no way to call a C++ constructor using P/Invoke (i.e. the DllImport attribute).
If the C++ class contains a static 'factory' method which returns a pointer to the created instance, then you might be able to call that first and then use the resulting pointer to call a member function of the class.
To do that, you'd need to pass the pointer (use the IntPtr type) as the first argument to the member function and specify the 'ThisCall' calling convention in the DllImport attribute.
However, the more usual ways to create unmanaged C++ objects from C# are:
1. To convert them to COM objects and then use COM interop to instantiate them; or
2. To create managed 'wrappers' for them using C++/CLI , add a reference to the resulting dll and instantiate them as you would any other managed class.
answered 2 years ago by:
17279