By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,853 Members | 2,144 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,853 IT Pros & Developers. It's quick & easy.

Create an instance of a class inside a c++ dll in a c# project

P: 6
Hi everybody i really need your help im completely lost :S this is what i need:

I have a c++ dll that uses MFC.
I need to create an instance of a class inside that dll in a c# project
I need to end with another dll to add it as a reference to other projects.

I have found many "solutions" but i just get confused :S
Pinvoke (just call functions but cant create an instance...)
create a kind of proxy, wrappers or whatever (not well explained)

please orient me, wich is the best approach to do this... any url address to read?
Mar 3 '09 #1
Share this Question
Share on Google+
5 Replies


vekipeki
Expert 100+
P: 229
You need to use P/Invoke to import native class members. The trickiest part here is to get the class members' names right - there is a dumpbin tool which comes with VS that will show you their names (after which you will need to "undecorate" the names using undname.exe.

You need correct native function names to specify the EntryPoint in DllImport attribute, as you can see from this example: http://blogs.msdn.com/vcblog/archive...lass-in-c.aspx
Mar 4 '09 #2

P: 6
@vekipeki
can i create an instance of that way?
Mar 4 '09 #3

vekipeki
Expert 100+
P: 229
Did you check the example? You have to P/Invoke every method from a native class, and then create a C# class which wraps these methods. If you have created a C# class, then of course you can instantiate it.

Check this example also: http://www.codeproject.com/KB/cs/marshalCPPclass.aspx.
Mar 5 '09 #4

P: 6
@vekipeki
What i wanted is what is explained in the last url :D thanks a lot =), just i have a few more problems.... i just try to reproduce the same creating new projects. and when compiling the CLR c++ dll project the next error:

Error 1 fatal error C1189: #error : "include 'stdafx.h' before including this file for PCH" c:\Documents and Settings\DMendoza.AMI-GE\My Documents\Visual Studio 2005\Projects\DLLTest\DLLTest\DLLTest.h 7

This happen only when the next code is in this order:

#include "stdafx.h"
#include "DLLTest.h"
#include <STDIO.H>


#include "DLLClient2.h"

this code is in DLLClient2.cpp this project is a c++ clr dll project that will act as a proxy it creates an instance of the class that is inside the other dll (the unmanaged one). if i change the above code like this a loooot of errors shows:

#include "DLLTest.h"
#include "stdafx.h"
#include <STDIO.H>


#include "DLLClient2.h"

Error 1 error C2512: 'CDLLTestApp' : no appropriate default constructor available c:\Documents and Settings\DMendoza.AMI-GE\My Documents\Visual Studio 2005\Projects\DLLClient2\DLLClient2\DLLClient2.cpp 16

and more similar errors nothing is recognized as if the .h file doesnt exist. any ideas? ...

i already copy the unmanaged dll in the same folder
Mar 5 '09 #5

P: 6
hi the last problem was solved when using the correct project type, but now i cant compile for this error:

Error 1 error PRJ0050: Failed to register output. Please ensure you have the appropriate permissions to modify the registry. DLLClientTest

any ideas? im logged as administrator
Mar 9 '09 #6

Post your reply

Sign in to post your reply or Sign up for a free account.