Re: Access my C# dll in C++
(1) You need to add a [Guid(...)] attribute to your IMessage and MyTest classes. You need to generate unique GUIDs for both. See here. Then re-run regasm on your dll.
(2) It should be
Code:
IMessagePtr pIMessage(__uuidof(MyTest));
// example of use
pIMessage->Testing();
Notice the Ptr on the end of IMessage ? This makes it a smart pointer.
And interfaces in COM are abstract classes.
Darwen.
Re: Access my C# dll in C++
One last little detail. You'll need to scope the smart pointer so it goes out of scope before calling CoUninitialize.
Code:
#import "....\aName.tlb" raw_interfaces_only
using namespace aName;
int _tmain(int argc, _TCHAR* argv[])
{
// Initialize COM.
HRESULT hr = CoInitialize(NULL);
{ // Scoping block
// Create the interface pointer.
IMessagePtr spIMessage(__uuidof(MyTest));
spIMessage->Testing( );
}
// Uninitialize COM.
CoUninitialize();
return 0;
}
Re: Access my C# dll in C++
Now If I use same EXE at some other machine the application crases.
The problem is I guess simple that it dosen't register the class / Interface at that machine.
Could any one help me that how we make it portable so that the application should be machine independent.
Re: Access my C# dll in C++
It's not about portability, it's about ensuring that you have any dependencies available and the COM registration has taken place on the target machine.
Re: Access my C# dll in C++
To register your dll do
Code:
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\regasm /tlb /codebase <dotnetdllname.dll>
Darwen.
Re: Access my C# dll in C++
[QUOTE=darwen;1746816](1) You need to add a [Guid(...)] attribute to your IMessage and MyTest classes. You need to generate unique GUIDs for both. See here. Then re-run regasm on your dll.
I tried this and I still got the same error. I tried with bout VisualStudio 2010 and2013. Any clues?
Re: Access my C# dll in C++
[QUOTE=FredEx42;2147869]
Quote:
Originally Posted by
darwen
(1) You need to add a [Guid(...)] attribute to your IMessage and MyTest classes. You need to generate unique GUIDs for both. See
here. Then re-run regasm on your dll.
I tried this and I still got the same error. I tried with bout VisualStudio 2010 and2013. Any clues?
What is the same error? What error did you get?
Re: Access my C# dll in C++
I get the same errors as reported at the start of the thread, starting with:
1>CppTest.cpp(16): error C2259: 'DoIt::IDoIt' : cannot instantiate abstract class
1> due to following members:
1> 'HRESULT IUnknown::QueryInterface(const IID &,void **)' : is abstract
My code is essentially the same as the code at the start of this thread. It seems like adding the [Guid(...)] attributes fixed capitalc's problem, but it didn't help me.
Re: Access my C# dll in C++
Did you regasm the C# COM dll? Does it appear as registered on your machine? (use oleview.exe in the Windows SDK or manually verified it got registered in the registry by searching for the csharpcom.dll).
Also, you need to set the [assembly: ComVisible(true)] in the AssemblyInfo.cs file.
Check out http://msdn.microsoft.com/en-us/libr...(v=vs.71).aspx
Re: Access my C# dll in C++
I found the problem -- I wasn't appending "Ptr" to the interface name in the .cpp file. Thanks for replying.