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. reference c++ dll from c#

reference c++ dll from c#

Scheduled Pinned Locked Moved C#
helpcsharpc++comquestion
9 Posts 2 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.
  • D Offline
    D Offline
    donovan solms
    wrote on last edited by
    #1

    i have written a c++ application now i want to override virtual event handlers in the c++ app with c#. also i want to compile the c++ app - or part of it- as a dll that i can reference in c#. but i keep on getting an error saying it's not a com object... problem 1. what type of project do i need to create for c++ to be accesible in c# 2. how do i override an event handler. or how do i call a c# method from c++ (instead of overriding the event handler. i'll call a c# method in a different dll)

    rather have something you don't need, than need something you don't have

    D 1 Reply Last reply
    0
    • D donovan solms

      i have written a c++ application now i want to override virtual event handlers in the c++ app with c#. also i want to compile the c++ app - or part of it- as a dll that i can reference in c#. but i keep on getting an error saying it's not a com object... problem 1. what type of project do i need to create for c++ to be accesible in c# 2. how do i override an event handler. or how do i call a c# method from c++ (instead of overriding the event handler. i'll call a c# method in a different dll)

      rather have something you don't need, than need something you don't have

      D Offline
      D Offline
      donovan solms
      wrote on last edited by
      #2

      ok i compiled the c++ with /clr now c# adds the reference

      rather have something you don't need, than need something you don't have

      D 1 Reply Last reply
      0
      • D donovan solms

        ok i compiled the c++ with /clr now c# adds the reference

        rather have something you don't need, than need something you don't have

        D Offline
        D Offline
        donovan solms
        wrote on last edited by
        #3

        got it working with dllimport but cannot get it to call a c++ method c#code : using System; using System.Collections.Generic; using System.Text; using System.Runtime.InteropServices; namespace ConsoleApplication2 { class Program { static void Main(string[] args) { int mine = tester(); Console.WriteLine(mine); } [DllImport("touchlib.dll", EntryPoint = "tester", ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)] static extern int tester(); } } c++ code int Touchable::tester() { return 5; }

        rather have something you don't need, than need something you don't have

        L 1 Reply Last reply
        0
        • D donovan solms

          got it working with dllimport but cannot get it to call a c++ method c#code : using System; using System.Collections.Generic; using System.Text; using System.Runtime.InteropServices; namespace ConsoleApplication2 { class Program { static void Main(string[] args) { int mine = tester(); Console.WriteLine(mine); } [DllImport("touchlib.dll", EntryPoint = "tester", ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)] static extern int tester(); } } c++ code int Touchable::tester() { return 5; }

          rather have something you don't need, than need something you don't have

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          Hi, DllImport is used to call native code such as Win32 functions. if your C++ dll is .NET code, just call the C++ class as if it were C#. :)

          Luc Pattyn [My Articles]

          D 1 Reply Last reply
          0
          • L Luc Pattyn

            Hi, DllImport is used to call native code such as Win32 functions. if your C++ dll is .NET code, just call the C++ class as if it were C#. :)

            Luc Pattyn [My Articles]

            D Offline
            D Offline
            donovan solms
            wrote on last edited by
            #5

            should i add a using ? and for functions in a class do i need to call Class::function()?

            rather have something you don't need, than need something you don't have

            L 1 Reply Last reply
            0
            • D donovan solms

              should i add a using ? and for functions in a class do i need to call Class::function()?

              rather have something you don't need, than need something you don't have

              L Offline
              L Offline
              Luc Pattyn
              wrote on last edited by
              #6

              donsolms wrote:

              should i add a using ?

              yes

              donsolms wrote:

              for functions in a class do i need to call Class::function()?

              no calling any CLR compliant language obeys the rules of the calling language, hence in C# you need a using statement, and call as class.method(args) or object.method(args). :)

              Luc Pattyn [My Articles]

              D 1 Reply Last reply
              0
              • L Luc Pattyn

                donsolms wrote:

                should i add a using ?

                yes

                donsolms wrote:

                for functions in a class do i need to call Class::function()?

                no calling any CLR compliant language obeys the rules of the calling language, hence in C# you need a using statement, and call as class.method(args) or object.method(args). :)

                Luc Pattyn [My Articles]

                D Offline
                D Offline
                donovan solms
                wrote on last edited by
                #7

                i can't get the using to work. i have the c++ dll in a namespace named touchlib and then a class named Touchable. using touchlib; doesn't work, i have added a reference to the dll. a i missing something, maybe in the compiling of the dll? how can i be sure that the dll is managed c++? what type of vc++ project is managed c++?

                rather have something you don't need, than need something you don't have

                L 1 Reply Last reply
                0
                • D donovan solms

                  i can't get the using to work. i have the c++ dll in a namespace named touchlib and then a class named Touchable. using touchlib; doesn't work, i have added a reference to the dll. a i missing something, maybe in the compiling of the dll? how can i be sure that the dll is managed c++? what type of vc++ project is managed c++?

                  rather have something you don't need, than need something you don't have

                  L Offline
                  L Offline
                  Luc Pattyn
                  wrote on last edited by
                  #8

                  Hi, I dont remember the details for C++, but you did build the C++ dll, didnt you ? so you know it is/isnt managed code. if you were able to add a reference to it in your C# app, it must be managed code. now Intellisense should show the available classes (C# and managed C++). :)

                  Luc Pattyn [My Articles]

                  D 1 Reply Last reply
                  0
                  • L Luc Pattyn

                    Hi, I dont remember the details for C++, but you did build the C++ dll, didnt you ? so you know it is/isnt managed code. if you were able to add a reference to it in your C# app, it must be managed code. now Intellisense should show the available classes (C# and managed C++). :)

                    Luc Pattyn [My Articles]

                    D Offline
                    D Offline
                    donovan solms
                    wrote on last edited by
                    #9

                    thanks a lot, it is working 100% now. i might have referenced an older version of the dll somehow, and after a simple clean solution for the c++ dll and c# project made it work. thanks again

                    rather have something you don't need, than need something you don't have

                    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