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. C# and C++ interop question

C# and C++ interop question

Scheduled Pinned Locked Moved C#
csharpcomquestionc++dotnet
6 Posts 3 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.
  • W Offline
    W Offline
    Werdna
    wrote on last edited by
    #1

    I have an application written in C++ that allows me to write addons. It has a public .h file with some class definitions. I can create .dll that has known entry point and that entry point will receive as one of it's parameters pointer to internal class from the app. Now I have C# library (that I use in other projects) and I want to use it within the application. My first thought was to use regasm and use COM to do all the interop. Might work, but I've never done any COM from C++ and it looks a bit messy. My second approach would be to use managed C++ (which I haven't tried either). So my question is: If I write my .dll with managed c++ and use my library (written in C#), will the application be able to load the library? How will CLR be loaded? This lib is written for .net 2, and I can use the new CLI C++, which looks nicer than 1.0 Also, If I have static objects within my library, will those be loaded upon each call? Thanks.

    B S 2 Replies Last reply
    0
    • W Werdna

      I have an application written in C++ that allows me to write addons. It has a public .h file with some class definitions. I can create .dll that has known entry point and that entry point will receive as one of it's parameters pointer to internal class from the app. Now I have C# library (that I use in other projects) and I want to use it within the application. My first thought was to use regasm and use COM to do all the interop. Might work, but I've never done any COM from C++ and it looks a bit messy. My second approach would be to use managed C++ (which I haven't tried either). So my question is: If I write my .dll with managed c++ and use my library (written in C#), will the application be able to load the library? How will CLR be loaded? This lib is written for .net 2, and I can use the new CLI C++, which looks nicer than 1.0 Also, If I have static objects within my library, will those be loaded upon each call? Thanks.

      B Offline
      B Offline
      BAIJUMAX
      wrote on last edited by
      #2

      Hi, My suggestion is , Create Com (ActiveX Dll),using ATL.It's very simple .And u Don't Have To Break Heads On it. Start -> ATLCOMAPPWIZARD -> Create Simple Object , This Will Your Interface from Outside.Okay, Now, Create Methods Exactly With Same As In Your Class written C++ [ ".cpp And .h"]. Each Methods In The Interface Will Actually wraps The Methods In Your .cpp . For Example :- Your C++ Class Implements add Fuction YourClass.add(long a, long b) you just call this method from YourDll.Add(long a,long b) create the dll. u can add this dll into C# project.By Add reference Method [Note:- U Can Use This In Any Language that Supports COM] Okay.Any doubts feel free to contanct me by baijumax

      W 1 Reply Last reply
      0
      • W Werdna

        I have an application written in C++ that allows me to write addons. It has a public .h file with some class definitions. I can create .dll that has known entry point and that entry point will receive as one of it's parameters pointer to internal class from the app. Now I have C# library (that I use in other projects) and I want to use it within the application. My first thought was to use regasm and use COM to do all the interop. Might work, but I've never done any COM from C++ and it looks a bit messy. My second approach would be to use managed C++ (which I haven't tried either). So my question is: If I write my .dll with managed c++ and use my library (written in C#), will the application be able to load the library? How will CLR be loaded? This lib is written for .net 2, and I can use the new CLI C++, which looks nicer than 1.0 Also, If I have static objects within my library, will those be loaded upon each call? Thanks.

        S Offline
        S Offline
        shaileshkumar
        wrote on last edited by
        #3

        Without using managed c++, also if your apis exposed do not have complex data structures used, you can export the dll and straight away marshal these apis to your own defined api and use it in your code using pinvoke. For e.g. [DllImport("Advapi32.dll", CharSet=CharSet.Auto)] static extern Boolean FileEncryptionStatus(String filename, out UInt32 status); Try some search on pinvoke. - Shailesh

        W 1 Reply Last reply
        0
        • B BAIJUMAX

          Hi, My suggestion is , Create Com (ActiveX Dll),using ATL.It's very simple .And u Don't Have To Break Heads On it. Start -> ATLCOMAPPWIZARD -> Create Simple Object , This Will Your Interface from Outside.Okay, Now, Create Methods Exactly With Same As In Your Class written C++ [ ".cpp And .h"]. Each Methods In The Interface Will Actually wraps The Methods In Your .cpp . For Example :- Your C++ Class Implements add Fuction YourClass.add(long a, long b) you just call this method from YourDll.Add(long a,long b) create the dll. u can add this dll into C# project.By Add reference Method [Note:- U Can Use This In Any Language that Supports COM] Okay.Any doubts feel free to contanct me by baijumax

          W Offline
          W Offline
          Werdna
          wrote on last edited by
          #4

          I think you misunderstood my question. I have .net library that I need to use from within C++. I do not need to expose C++ as COM, I wanted to expose .net as COM and use it from within C++.

          1 Reply Last reply
          0
          • S shaileshkumar

            Without using managed c++, also if your apis exposed do not have complex data structures used, you can export the dll and straight away marshal these apis to your own defined api and use it in your code using pinvoke. For e.g. [DllImport("Advapi32.dll", CharSet=CharSet.Auto)] static extern Boolean FileEncryptionStatus(String filename, out UInt32 status); Try some search on pinvoke. - Shailesh

            W Offline
            W Offline
            Werdna
            wrote on last edited by
            #5

            The app has several quite big C++ abstract classes. My dll will get loaded and passed one of the classes (actually some object derived from this class).

            S 1 Reply Last reply
            0
            • W Werdna

              The app has several quite big C++ abstract classes. My dll will get loaded and passed one of the classes (actually some object derived from this class).

              S Offline
              S Offline
              shaileshkumar
              wrote on last edited by
              #6

              I am not sure i understood you, you want to import the class or class static functions? Could you elaborate a little more? - Shailesh

              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