Click here to Skip to main content
13,704,528 members
Rate this:
 
Please Sign up or sign in to vote.
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 13-Jun-18 0:36am
Updated 13-Jun-18 1:19am

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web06-2016 | 2.8.180920.1 | Last Updated 13 Jun 2018
Copyright © CodeProject, 1999-2018
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100