Using C# in C++
-
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? -
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?I'm afraid you cannot do it: There's no trace of the static
Subtract
method in thetlh
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] -
I'm afraid you cannot do it: There's no trace of the static
Subtract
method in thetlh
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]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
-
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
You could create a singleton object in C# project, that could provide access to the static methods.
-
You could create a singleton object in C# project, that could provide access to the static methods.
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] -
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?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" - Mixturecheers, Alok Gupta VC Forum Q&A :- I/IV Support CRY- Child Relief and You
-
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?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" - Mixturecheers, Alok Gupta VC Forum Q&A :- I/IV Support CRY- Child Relief and You