Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Import a C++ class to C#

Import a C++ class to C#

Scheduled Pinned Locked Moved C#
csharpc++tutorialquestion
8 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    lune12
    wrote on last edited by
    #1

    Hi, I have a C++ class that is compiled as a DLL. now I would like to import it to my C# program. I know how to import function, but what should I do for importing the class so I can in my C# program create an instance of this class? Thanks,

    C L 2 Replies Last reply
    0
    • L lune12

      Hi, I have a C++ class that is compiled as a DLL. now I would like to import it to my C# program. I know how to import function, but what should I do for importing the class so I can in my C# program create an instance of this class? Thanks,

      C Offline
      C Offline
      Colin Angus Mackay
      wrote on last edited by
      #2

      Unless it is managed C++ you cannot import the class. If it is managed C++ then what you have as an Assembly which can be added by adding a reference to it in the project and by putting a using myNamespace at the top of the C# file you want to use the managed C++ class in.

      *Developer Day Scotland - Free community conference *Colin Angus Mackay's Blog *Latest Scottish Developers Newsletter


      Vogon Building and Loan advise that your planet is at risk if you do not keep up repayments on any mortgage secured upon it. Remember that the force of gravity can go up as well as down.

      L 0 2 Replies Last reply
      0
      • C Colin Angus Mackay

        Unless it is managed C++ you cannot import the class. If it is managed C++ then what you have as an Assembly which can be added by adding a reference to it in the project and by putting a using myNamespace at the top of the C# file you want to use the managed C++ class in.

        *Developer Day Scotland - Free community conference *Colin Angus Mackay's Blog *Latest Scottish Developers Newsletter


        Vogon Building and Loan advise that your planet is at risk if you do not keep up repayments on any mortgage secured upon it. Remember that the force of gravity can go up as well as down.

        L Offline
        L Offline
        lune12
        wrote on last edited by
        #3

        Thanks, but how do I know if it's managed C++. It was written with Visual Studio 2005

        A 1 Reply Last reply
        0
        • L lune12

          Thanks, but how do I know if it's managed C++. It was written with Visual Studio 2005

          A Offline
          A Offline
          Alan N
          wrote on last edited by
          #4

          hi, You could try adding a reference to the dll to your c# project. It will fail if it is not a managed assembly or COM component. Another way would be to try to open the dll with the MSIL disassembler. Again this will fail if there is no CLR header. Alan.

          1 Reply Last reply
          0
          • C Colin Angus Mackay

            Unless it is managed C++ you cannot import the class. If it is managed C++ then what you have as an Assembly which can be added by adding a reference to it in the project and by putting a using myNamespace at the top of the C# file you want to use the managed C++ class in.

            *Developer Day Scotland - Free community conference *Colin Angus Mackay's Blog *Latest Scottish Developers Newsletter


            Vogon Building and Loan advise that your planet is at risk if you do not keep up repayments on any mortgage secured upon it. Remember that the force of gravity can go up as well as down.

            0 Offline
            0 Offline
            0x3c0
            wrote on last edited by
            #5

            Something which just came to mind. Would it be possible to have a function which returns a pointer to an instantiation of the class? So would something like this work? //C++ extern "C" MyClass *GetInstantiation(void); //C# [DllImport("MyFile.dll")] public static extern IntPtr GetInstantiation();

            X 1 Reply Last reply
            0
            • 0 0x3c0

              Something which just came to mind. Would it be possible to have a function which returns a pointer to an instantiation of the class? So would something like this work? //C++ extern "C" MyClass *GetInstantiation(void); //C# [DllImport("MyFile.dll")] public static extern IntPtr GetInstantiation();

              X Offline
              X Offline
              Xmen Real
              wrote on last edited by
              #6

              yes its possible ;)

              TVMU^P[[IGIOQHG^JSH`A#@`RFJ\c^JPL>;"[,*/|+&WLEZGc`AFXc!L %^]*IRXD#@GKCQ`R\^SF_WcHbORY87֦ʻ6ϣN8ȤBcRAV\Z^&SU~%CSWQ@#2 W_AD`EPABIKRDFVS)EVLQK)JKSQXUFYK[M`UKs*$GwU#(QDXBER@CBN% Rs0~53%eYrd8mt^7Z6]iTF+(EWfJ9zaK-i’TV.C\y<pŠjxsg-b$f4ia> -------------------------------------------------------- 128 bit encrypted signature, crack if you can

              0 1 Reply Last reply
              0
              • L lune12

                Hi, I have a C++ class that is compiled as a DLL. now I would like to import it to my C# program. I know how to import function, but what should I do for importing the class so I can in my C# program create an instance of this class? Thanks,

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                I usually create a bridge written in C++ managed which creates one managed class and namespace that could be later imported into C#, but underneath this managed C++ class works with that actual and normal C++ class any way it is expected to work with this class, either I compile them together or I import dll and call this class internally. This bridge serves several good purposes: to test what actually I need in C# from that C++ class. Only those things pass the bridge. Second the bridge is an excellent place to check if parameters are passed correctly and if some of them have to be arranged differently: pointers, arrays... all need different treatment in C# and C++. If you don't have this bridge, but you directly call a C++ procedure from C# then you have to think about this inside C# code every time you call a specific C++ function. And then if you want to use the same C++ class again, the same problem. With bridge you just import it and everything is tested.

                1 Reply Last reply
                0
                • X Xmen Real

                  yes its possible ;)

                  TVMU^P[[IGIOQHG^JSH`A#@`RFJ\c^JPL>;"[,*/|+&WLEZGc`AFXc!L %^]*IRXD#@GKCQ`R\^SF_WcHbORY87֦ʻ6ϣN8ȤBcRAV\Z^&SU~%CSWQ@#2 W_AD`EPABIKRDFVS)EVLQK)JKSQXUFYK[M`UKs*$GwU#(QDXBER@CBN% Rs0~53%eYrd8mt^7Z6]iTF+(EWfJ9zaK-i’TV.C\y<pŠjxsg-b$f4ia> -------------------------------------------------------- 128 bit encrypted signature, crack if you can

                  0 Offline
                  0 Offline
                  0x3c0
                  wrote on last edited by
                  #8

                  Lovely, thanks

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups