Click here to Skip to main content
15,888,330 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

I am trying to use a c++ dll in c# winform application

The Dll exports a class (CMyClass) which inherits from another base class.

The export is done by a wrapper which encapsulates the CTor DTor and another function in this manner:

extern "C" {
extern __declspec(dllexport) CMyClass* CreateMyClass();
.DTor..
.function..
}

and CreateMyClass is something like that:


CMyClass* CreateMyClass (){return new CMyClass();}


The call to CreateMyClass in c# is done by declaring
[DllImport("MyDllPath.....")]
static public extern IntPtr CreateMyClass ();

and then calling IntPtr p = CreateMyClass() .

The call fails with Attempted to read or write protected memory"

Is it possible to invoke a c++ class which is a derived class in c#?

Thanks!!

What I have tried:

The dll works fine in an another C++ app.

The C# app. succeeded in calling the CreateMyClass if CMyClass is not a derived class
Posted
Updated 13-Jun-18 1:19am

1 solution

In a nutshell: write a COM wrapper around the class.

You can do this with Marshalling or C++ COM Interop as described by Microsoft. Here is an article How to Marshal a C++ Class which has a demo project included.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900