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 / C++ / MFC
  4. Using C# in C++

Using C# in C++

Scheduled Pinned Locked Moved C / C++ / MFC
questioncomcsharpc++
7 Posts 4 Posters 1 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.
  • C Offline
    C Offline
    Chris_Green
    wrote on last edited by
    #1

    Hi, I have a sample project where I am using a C# library in a C++ test program. I'm doing this by COM, using the tlb and project reference. C# [ComVisible(true)] [ClassInterface(ClassInterfaceType.AutoDual)] public class Math { public int Add(int x, int y) { return x + y; } public static int Subtract(int x, int y) { return x - y; } } C++ #include "stdafx.h" #include <iostream> #import "..\\CSharpUsefulLibrary.tlb" raw_interfaces_only int _tmain(int argc, _TCHAR* argv[]) { CoInitialize(0); CSharpUsefulLibrary::_MathPtr math(__uuidof(CSharpUsefulLibrary::Math)); long value; math->Add(1, 2, &value); std::wcout << "The value is " << value; CoUninitialize(); return 0; } So this works fine, I'm able to call the add method in my C# library from C++. The question is, how do I call the static subtract method?

    CPalliniC T 3 Replies Last reply
    0
    • C Chris_Green

      Hi, I have a sample project where I am using a C# library in a C++ test program. I'm doing this by COM, using the tlb and project reference. C# [ComVisible(true)] [ClassInterface(ClassInterfaceType.AutoDual)] public class Math { public int Add(int x, int y) { return x + y; } public static int Subtract(int x, int y) { return x - y; } } C++ #include "stdafx.h" #include <iostream> #import "..\\CSharpUsefulLibrary.tlb" raw_interfaces_only int _tmain(int argc, _TCHAR* argv[]) { CoInitialize(0); CSharpUsefulLibrary::_MathPtr math(__uuidof(CSharpUsefulLibrary::Math)); long value; math->Add(1, 2, &value); std::wcout << "The value is " << value; CoUninitialize(); return 0; } So this works fine, I'm able to call the add method in my C# library from C++. The question is, how do I call the static subtract method?

      CPalliniC Offline
      CPalliniC Offline
      CPallini
      wrote on last edited by
      #2

      I'm afraid you cannot do it: There's no trace of the static Subtract method in the tlh generated file.

      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
      This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
      [My articles]

      In testa che avete, signor di Ceprano?

      C 1 Reply Last reply
      0
      • CPalliniC CPallini

        I'm afraid you cannot do it: There's no trace of the static Subtract method in the tlh generated file.

        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
        This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
        [My articles]

        C Offline
        C Offline
        Chris_Green
        wrote on last edited by
        #3

        Yeah, in VS I did File->Open on the tlb and indeed couldn't find it. Looks like we'll need a wrapper to expose static funcitons. Thanks

        R 1 Reply Last reply
        0
        • C Chris_Green

          Yeah, in VS I did File->Open on the tlb and indeed couldn't find it. Looks like we'll need a wrapper to expose static funcitons. Thanks

          R Offline
          R Offline
          Rolf Kristensen
          wrote on last edited by
          #4

          You could create a singleton object in C# project, that could provide access to the static methods.

          CPalliniC 1 Reply Last reply
          0
          • R Rolf Kristensen

            You could create a singleton object in C# project, that could provide access to the static methods.

            CPalliniC Offline
            CPalliniC Offline
            CPallini
            wrote on last edited by
            #5

            There's no need, I suppose, since he already has a class. He has simply to wrap the static method with an instance one. :)

            If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
            This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
            [My articles]

            In testa che avete, signor di Ceprano?

            1 Reply Last reply
            0
            • C Chris_Green

              Hi, I have a sample project where I am using a C# library in a C++ test program. I'm doing this by COM, using the tlb and project reference. C# [ComVisible(true)] [ClassInterface(ClassInterfaceType.AutoDual)] public class Math { public int Add(int x, int y) { return x + y; } public static int Subtract(int x, int y) { return x - y; } } C++ #include "stdafx.h" #include <iostream> #import "..\\CSharpUsefulLibrary.tlb" raw_interfaces_only int _tmain(int argc, _TCHAR* argv[]) { CoInitialize(0); CSharpUsefulLibrary::_MathPtr math(__uuidof(CSharpUsefulLibrary::Math)); long value; math->Add(1, 2, &value); std::wcout << "The value is " << value; CoUninitialize(); return 0; } So this works fine, I'm able to call the add method in my C# library from C++. The question is, how do I call the static subtract method?

              T Offline
              T Offline
              ThatsAlok
              wrote on last edited by
              #6

              newkie wrote:

              The question is, how do I call the static subtract method?

              afaik, and my weak knowledge in COM, i haven't seen STATIC method being exported by COM model

              "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
              Never mind - my own stupidity is the source of every "problem" - Mixture

              cheers, Alok Gupta VC Forum Q&A :- I/IV Support CRY- Child Relief and You

              1 Reply Last reply
              0
              • C Chris_Green

                Hi, I have a sample project where I am using a C# library in a C++ test program. I'm doing this by COM, using the tlb and project reference. C# [ComVisible(true)] [ClassInterface(ClassInterfaceType.AutoDual)] public class Math { public int Add(int x, int y) { return x + y; } public static int Subtract(int x, int y) { return x - y; } } C++ #include "stdafx.h" #include <iostream> #import "..\\CSharpUsefulLibrary.tlb" raw_interfaces_only int _tmain(int argc, _TCHAR* argv[]) { CoInitialize(0); CSharpUsefulLibrary::_MathPtr math(__uuidof(CSharpUsefulLibrary::Math)); long value; math->Add(1, 2, &value); std::wcout << "The value is " << value; CoUninitialize(); return 0; } So this works fine, I'm able to call the add method in my C# library from C++. The question is, how do I call the static subtract method?

                T Offline
                T Offline
                ThatsAlok
                wrote on last edited by
                #7

                just a FAQ on COM :- http://com-faq.blogspot.com/[^]

                "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
                Never mind - my own stupidity is the source of every "problem" - Mixture

                cheers, Alok Gupta VC Forum Q&A :- I/IV Support CRY- Child Relief and You

                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