Is it possible to call C# code from C++ ?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ricky

    Is it possible to call C# code from C++ ?

    Hi

    I know that we can call C++ code from C# using COM interop, how about do
    the reverse?

    Ricky
  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: Is it possible to call C# code from C++ ?

    Ricky,

    If you want to call a COM component from .NET, you just have to add a
    reference to it in your VS.NET project (go to "Add References").

    If you were working from the command line, you would run the TLBIMP
    utility to generate a Runtime Callable Wrapper, and then set a reference to
    that.

    You can also call C++ code through the P/Invoke layer, if the C++ code
    exposes functions through a DLL.

    Or, finally, you can wrap the C++ code in a managed wrapper (using
    Managed Extensions for C++) and then call it like a normal assembly.

    Hope this helps.


    --
    - Nicholas Paldino [.NET/C# MVP]
    - mvp@spam.guard. caspershouse.co m

    "Ricky" <spam@spam.co m> wrote in message
    news:OwMuyTzlEH A.2864@TK2MSFTN GP14.phx.gbl...[color=blue]
    > Hi
    >
    > I know that we can call C++ code from C# using COM interop, how about do
    > the reverse?
    >
    > Ricky[/color]


    Comment

    • Nicholas Paldino [.NET/C# MVP]

      #3
      Re: Is it possible to call C# code from C++ ?

      Ricky,

      If you want to call a COM component from .NET, you just have to add a
      reference to it in your VS.NET project (go to "Add References").

      If you were working from the command line, you would run the TLBIMP
      utility to generate a Runtime Callable Wrapper, and then set a reference to
      that.

      You can also call C++ code through the P/Invoke layer, if the C++ code
      exposes functions through a DLL.

      Or, finally, you can wrap the C++ code in a managed wrapper (using
      Managed Extensions for C++) and then call it like a normal assembly.

      Hope this helps.


      --
      - Nicholas Paldino [.NET/C# MVP]
      - mvp@spam.guard. caspershouse.co m

      "Ricky" <spam@spam.co m> wrote in message
      news:OwMuyTzlEH A.2864@TK2MSFTN GP14.phx.gbl...[color=blue]
      > Hi
      >
      > I know that we can call C++ code from C# using COM interop, how about do
      > the reverse?
      >
      > Ricky[/color]


      Comment

      • Willy Denoyette [MVP]

        #4
        Re: Is it possible to call C# code from C++ ?

        Yes, you can expose C# classes as a COM classes.
        However, you have to respect some basic rules and preferably set some
        attributes for this to work correctly, you also have to explicitly "register
        the assembly for COM interop" [1].
        For details, check the "COM interop tutorials" in the C# programmers
        reference" on MSDN.

        Willy.

        [1] Windows XP SP2 offers a "registrati on free COM interop" facility.

        "Ricky" <spam@spam.co m> wrote in message
        news:OwMuyTzlEH A.2864@TK2MSFTN GP14.phx.gbl...[color=blue]
        > Hi
        >
        > I know that we can call C++ code from C# using COM interop, how about do
        > the reverse?
        >
        > Ricky[/color]


        Comment

        • Willy Denoyette [MVP]

          #5
          Re: Is it possible to call C# code from C++ ?

          Yes, you can expose C# classes as a COM classes.
          However, you have to respect some basic rules and preferably set some
          attributes for this to work correctly, you also have to explicitly "register
          the assembly for COM interop" [1].
          For details, check the "COM interop tutorials" in the C# programmers
          reference" on MSDN.

          Willy.

          [1] Windows XP SP2 offers a "registrati on free COM interop" facility.

          "Ricky" <spam@spam.co m> wrote in message
          news:OwMuyTzlEH A.2864@TK2MSFTN GP14.phx.gbl...[color=blue]
          > Hi
          >
          > I know that we can call C++ code from C# using COM interop, how about do
          > the reverse?
          >
          > Ricky[/color]


          Comment

          • Ricky

            #6
            Re: Is it possible to call C# code from C++ ?

            Hi

            Sorry, I mean call C# from C++, not C++ from C#.


            Ricky

            Nicholas Paldino [.NET/C# MVP] wrote:[color=blue]
            > Ricky,
            >
            > If you want to call a COM component from .NET, you just have to add a
            > reference to it in your VS.NET project (go to "Add References").
            >
            > If you were working from the command line, you would run the TLBIMP
            > utility to generate a Runtime Callable Wrapper, and then set a reference to
            > that.
            >
            > You can also call C++ code through the P/Invoke layer, if the C++ code
            > exposes functions through a DLL.
            >
            > Or, finally, you can wrap the C++ code in a managed wrapper (using
            > Managed Extensions for C++) and then call it like a normal assembly.
            >
            > Hope this helps.
            >
            >[/color]

            Comment

            • Ricky

              #7
              Re: Is it possible to call C# code from C++ ?

              Hi

              Sorry, I mean call C# from C++, not C++ from C#.


              Ricky

              Nicholas Paldino [.NET/C# MVP] wrote:[color=blue]
              > Ricky,
              >
              > If you want to call a COM component from .NET, you just have to add a
              > reference to it in your VS.NET project (go to "Add References").
              >
              > If you were working from the command line, you would run the TLBIMP
              > utility to generate a Runtime Callable Wrapper, and then set a reference to
              > that.
              >
              > You can also call C++ code through the P/Invoke layer, if the C++ code
              > exposes functions through a DLL.
              >
              > Or, finally, you can wrap the C++ code in a managed wrapper (using
              > Managed Extensions for C++) and then call it like a normal assembly.
              >
              > Hope this helps.
              >
              >[/color]

              Comment

              • Nicholas Paldino [.NET/C# MVP]

                #8
                Re: Is it possible to call C# code from C++ ?

                Ricky,

                Yes, it is. You can export your .NET types to COM using the TLBEXP
                tool, which will generate a type library for your .NET types.

                However, going from .NET to COM is a little tricker than the other way
                around. Before doing this, you should read the section of the .NET
                framework documentation titled "", located at (watch for line wrap):


                "Ricky" <spam@spam.co m> wrote in message
                news:eWLJ7pzlEH A.3712@TK2MSFTN GP15.phx.gbl...[color=blue]
                > Hi
                >
                > Sorry, I mean call C# from C++, not C++ from C#.
                >
                >
                > Ricky
                >
                > Nicholas Paldino [.NET/C# MVP] wrote:[color=green]
                >> Ricky,
                >>
                >> If you want to call a COM component from .NET, you just have to add a
                >> reference to it in your VS.NET project (go to "Add References").
                >>
                >> If you were working from the command line, you would run the TLBIMP
                >> utility to generate a Runtime Callable Wrapper, and then set a reference
                >> to that.
                >>
                >> You can also call C++ code through the P/Invoke layer, if the C++
                >> code exposes functions through a DLL.
                >>
                >> Or, finally, you can wrap the C++ code in a managed wrapper (using
                >> Managed Extensions for C++) and then call it like a normal assembly.
                >>
                >> Hope this helps.
                >>[/color][/color]

                Comment

                • Nicholas Paldino [.NET/C# MVP]

                  #9
                  Re: Is it possible to call C# code from C++ ?

                  Ricky,

                  Yes, it is. You can export your .NET types to COM using the TLBEXP
                  tool, which will generate a type library for your .NET types.

                  However, going from .NET to COM is a little tricker than the other way
                  around. Before doing this, you should read the section of the .NET
                  framework documentation titled "", located at (watch for line wrap):


                  "Ricky" <spam@spam.co m> wrote in message
                  news:eWLJ7pzlEH A.3712@TK2MSFTN GP15.phx.gbl...[color=blue]
                  > Hi
                  >
                  > Sorry, I mean call C# from C++, not C++ from C#.
                  >
                  >
                  > Ricky
                  >
                  > Nicholas Paldino [.NET/C# MVP] wrote:[color=green]
                  >> Ricky,
                  >>
                  >> If you want to call a COM component from .NET, you just have to add a
                  >> reference to it in your VS.NET project (go to "Add References").
                  >>
                  >> If you were working from the command line, you would run the TLBIMP
                  >> utility to generate a Runtime Callable Wrapper, and then set a reference
                  >> to that.
                  >>
                  >> You can also call C++ code through the P/Invoke layer, if the C++
                  >> code exposes functions through a DLL.
                  >>
                  >> Or, finally, you can wrap the C++ code in a managed wrapper (using
                  >> Managed Extensions for C++) and then call it like a normal assembly.
                  >>
                  >> Hope this helps.
                  >>[/color][/color]

                  Comment

                  • Nicholas Paldino [.NET/C# MVP]

                    #10
                    Re: Is it possible to call C# code from C++ ?

                    Oops, it would help if I gave the title and the link:

                    Qualifying .NET Types for Interoperation




                    --
                    - Nicholas Paldino [.NET/C# MVP]
                    - mvp@spam.guard. caspershouse.co m

                    "Ricky" <spam@spam.co m> wrote in message
                    news:eWLJ7pzlEH A.3712@TK2MSFTN GP15.phx.gbl...[color=blue]
                    > Hi
                    >
                    > Sorry, I mean call C# from C++, not C++ from C#.
                    >
                    >
                    > Ricky
                    >
                    > Nicholas Paldino [.NET/C# MVP] wrote:[color=green]
                    >> Ricky,
                    >>
                    >> If you want to call a COM component from .NET, you just have to add a
                    >> reference to it in your VS.NET project (go to "Add References").
                    >>
                    >> If you were working from the command line, you would run the TLBIMP
                    >> utility to generate a Runtime Callable Wrapper, and then set a reference
                    >> to that.
                    >>
                    >> You can also call C++ code through the P/Invoke layer, if the C++
                    >> code exposes functions through a DLL.
                    >>
                    >> Or, finally, you can wrap the C++ code in a managed wrapper (using
                    >> Managed Extensions for C++) and then call it like a normal assembly.
                    >>
                    >> Hope this helps.
                    >>[/color][/color]

                    Comment

                    • Nicholas Paldino [.NET/C# MVP]

                      #11
                      Re: Is it possible to call C# code from C++ ?

                      Oops, it would help if I gave the title and the link:

                      Qualifying .NET Types for Interoperation




                      --
                      - Nicholas Paldino [.NET/C# MVP]
                      - mvp@spam.guard. caspershouse.co m

                      "Ricky" <spam@spam.co m> wrote in message
                      news:eWLJ7pzlEH A.3712@TK2MSFTN GP15.phx.gbl...[color=blue]
                      > Hi
                      >
                      > Sorry, I mean call C# from C++, not C++ from C#.
                      >
                      >
                      > Ricky
                      >
                      > Nicholas Paldino [.NET/C# MVP] wrote:[color=green]
                      >> Ricky,
                      >>
                      >> If you want to call a COM component from .NET, you just have to add a
                      >> reference to it in your VS.NET project (go to "Add References").
                      >>
                      >> If you were working from the command line, you would run the TLBIMP
                      >> utility to generate a Runtime Callable Wrapper, and then set a reference
                      >> to that.
                      >>
                      >> You can also call C++ code through the P/Invoke layer, if the C++
                      >> code exposes functions through a DLL.
                      >>
                      >> Or, finally, you can wrap the C++ code in a managed wrapper (using
                      >> Managed Extensions for C++) and then call it like a normal assembly.
                      >>
                      >> Hope this helps.
                      >>[/color][/color]

                      Comment

                      • Ricky

                        #12
                        Re: Is it possible to call C# code from C++ ?

                        Thanks for your information.

                        Nicholas Paldino [.NET/C# MVP] wrote:[color=blue]
                        > Oops, it would help if I gave the title and the link:
                        >
                        > Qualifying .NET Types for Interoperation
                        >
                        > http://msdn.microsoft.com/library/de...roperation.asp
                        >
                        >[/color]

                        Comment

                        • Ricky

                          #13
                          Re: Is it possible to call C# code from C++ ?

                          Thanks for your information.

                          Nicholas Paldino [.NET/C# MVP] wrote:[color=blue]
                          > Oops, it would help if I gave the title and the link:
                          >
                          > Qualifying .NET Types for Interoperation
                          >
                          > http://msdn.microsoft.com/library/de...roperation.asp
                          >
                          >[/color]

                          Comment

                          Working...