Howto use (and debug) C# code in/from C++ code

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Letharion
    New Member
    • Oct 2008
    • 3

    Howto use (and debug) C# code in/from C++ code

    Hi :)

    I have
    (A) An application whose functionality I'm gonna extend with a dll
    (B) A bunch of C# code that I wish to use in the dll.
    (C) A how-to-interact-with-this-app-specification that requires what has been refered to only as "regular DLL entry points", something that seems to require use of C++. (This is where my understanding of the subject gets a bit fuzzy)

    All of this is going to be a rather big project, so I'm trying to find the most "efficient" solution, longterm.

    Now, from a barebones C++ project that this application accepts as a valid extension, how do I call functions on my C# code?

    I have both the C++ and C# source available, and can make any changes to it necessary. I could even go to C++ altogether instead, but I'm more comfortable in C#.

    I'm using Visual Studio Professional 2008, and I'm guessing that's it's not really difficult to call code over the project limits, but I could be wrong.

    Also, when I do get this working, how will debugging work? Is there anything "extra" I should take into consideration? "Normally" I just run a my code line by line if I wanna debug something, but maybe that's not possible either because it's a dll, or because I use 2 languages.

    I'd appreciate any and all assistance :)
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    Originally posted by Letharion
    Hi :)

    (C) A how-to-interact-with-this-app-specification that requires what has been refered to only as "regular DLL entry points", something that seems to require use of C++. (This is where my understanding of the subject gets a bit fuzzy)
    I don't know what you are talking about here...are you creating a "how to" readme file for the DLL?

    Originally posted by Letharion
    Now, from a barebones C++ project that this application accepts as a valid extension, how do I call functions on my C# code?
    Include a reference to your C++ in your project. This will create an Interop object that lets the C# code use the C++ unamaged code.

    Originally posted by Letharion
    I have both the C++ and C# source available, and can make any changes to it necessary. I could even go to C++ altogether instead, but I'm more comfortable in C#.
    I would stick with what your comfortable with. Why would you choose to write code in two different languages if you didn't have to? I personally find managed code a lot easier to work with than unmanaged code....especia lly with concerns to memory clean up.

    If you use C# only you can automatically generate XML documentation for all of your public functions/members. This XML doc can then be used to create help documents from. Check out Sandcastle and DocProject for components that make this easier than doing it by hand.

    Originally posted by Letharion
    Also, when I do get this working, how will debugging work?
    In Visual Studio you will be able to Debug your C# code....you'll have to use something else to debug your C++ code.

    Originally posted by Letharion
    Is there anything "extra" I should take into consideration?
    When calling unmanaged code in managed C# code an Interop Object is created so that the C# code can use the unmanaged code. This Object does not always work 100% and you may have to Marshal your C++ code yourself. It would be best to pick either Managed or Unmanaged code for your project....
    Like I said earlier I'm much more fond of managed.

    I would strongly recommend researching Interop and what is involved when you have to Marshal before you get started so that you know the problems that can happen when using unmanaged and managed code together.

    Originally posted by Letharion
    "Normally" I just run a my code line by line if I wanna debug something, but maybe that's not possible either because it's a dll, or because I use 2 languages.
    I'd create another application, a Console app which includes the DLL that can be used for testing.


    -Frinny

    Comment

    • Letharion
      New Member
      • Oct 2008
      • 3

      #3
      Hey Frinny, thanks for all you help :)

      Originally posted by Frinavale
      Include a reference to your C++ in your project. This will create an Interop object that lets the C# code use the C++ unamaged code.
      About this, I have to ask, did you turn it around? Or do I completely missunderstand what your saying?
      I want my C++ code to call C# functions, not the other way around. :)

      Taking a rather trivial example with some pseudo-code.

      If I take the C++-dll, and write
      [code=cpp]
      unsigned int Identify() {
      return 2;
      }
      [/code]
      This is "correct" and "works". (As is being recognized as a valid extension to Discipulus, as the program is called)

      Now, I add a C# project to the same solution, and do
      [code=cpp]
      namespace MySpace {
      public class DllClass {
      public void Identify {
      return 2;
      }
      }
      }
      [/code]
      And now I wish the C++ version of identify to look like this.
      [code=cpp]
      unsigned int Identify() {
      return C#->MySpace->DllClass->Identify();
      }
      [/code]
      What I need to do is somehow expose the namespace MySpace to the C++ code, right? :)
      Last edited by Frinavale; Oct 27 '08, 06:37 PM. Reason: added [code] tags

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        Originally posted by Letharion
        Hey Frinny, thanks for all you help :)



        About this, I have to ask, did you turn it around? Or do I completely missunderstand what your saying?
        I want my C++ code to call C# functions, not the other way around. :)
        I did get it turned around. I'm not sure why I thought you were calling your C++ code from your C# code...maybe it's because you said you prefer C#...

        Anyways, what you need to do is make an Interface so that you can expose the C# members to the C++. You are may to have to write some Marshaling code so that your C++ can use your C# for things like Strings etc. (see the MarshalAs Attribute

        Once you have that, you compile your C# code.
        After that you will have to register the DLL using regasm (this will create a COM Callable Wrapper (CCW) so that your C++ code knows how to call and use your C# code).

        Check out this article ...it has details on how to call C# code from C++ and how to call C++ code from C#.

        -Frinny

        Comment

        • Letharion
          New Member
          • Oct 2008
          • 3

          #5
          Sry for taking so long to reply, but thanks for the help. The link looks like it can help me out :)

          Comment

          • Frinavale
            Recognized Expert Expert
            • Oct 2006
            • 9749

            #6
            On more link just in case: Exposing .NET Framework Components to COM

            :)

            -Frinny

            Comment

            Working...