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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • eck3ko
    New Member
    • Mar 2009
    • 6

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

    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?
  • vekipeki
    Recognized Expert New Member
    • Nov 2007
    • 229

    #2
    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

    Comment

    • eck3ko
      New Member
      • Mar 2009
      • 6

      #3
      Originally posted by vekipeki
      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
      can i create an instance of that way?

      Comment

      • vekipeki
        Recognized Expert New Member
        • Nov 2007
        • 229

        #4
        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.

        Comment

        • eck3ko
          New Member
          • Mar 2009
          • 6

          #5
          Originally posted by vekipeki
          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.
          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\DMendo za.AMI-GE\My Documents\Visua l Studio 2005\Projects\D LLTest\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\DMendo za.AMI-GE\My Documents\Visua l Studio 2005\Projects\D LLClient2\DLLCl ient2\DLLClient 2.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

          Comment

          • eck3ko
            New Member
            • Mar 2009
            • 6

            #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

            Comment

            Working...