-
July 29th, 2008, 10:56 AM
#1
Access my C# dll in C++
here's my C# dll:
Code:
public interface IMessage
{
void Testing();
}
public class MyTest : IMessage
{
public void Testing()
{
Console.WriteLine("Testing dll");
}
}
here's my cpp code:
Code:
#import "....\aName.tlb" raw_interfaces_only
using namespace aName;
int _tmain(int argc, _TCHAR* argv[])
{
// Initialize COM.
HRESULT hr = CoInitialize(NULL);
// Create the interface pointer.
IMessage pIMessage(__uuidof(MyTest));
// Uninitialize COM.
CoUninitialize();
return 0;
}
..and here's my error:
Code:
1>c:\.....cpp(22) : error C2259: 'aName::IMessage' : cannot instantiate abstract class
1> due to following members:
1> 'HRESULT IUnknown::QueryInterface(const IID &,void **)' : is abstract
1> c:\program files\microsoft visual studio 8\vc\platformsdk\include\unknwn.h(113) : see declaration of 'IUnknown::QueryInterface'
1> 'ULONG IUnknown::AddRef(void)' : is abstract
1> c:\program files\microsoft visual studio 8\vc\platformsdk\include\unknwn.h(117) : see declaration of 'IUnknown::AddRef'
1> 'ULONG IUnknown::Release(void)' : is abstract
1> c:\program files\microsoft visual studio 8\vc\platformsdk\include\unknwn.h(119) : see declaration of 'IUnknown::Release'
1> 'HRESULT IDispatch::GetTypeInfoCount(UINT *)' : is abstract
1> c:\program files\microsoft visual studio 8\vc\platformsdk\include\oaidl.h(2712) : see declaration of 'IDispatch::GetTypeInfoCount'
1> 'HRESULT IDispatch::GetTypeInfo(UINT,LCID,ITypeInfo **)' : is abstract
1> c:\program files\microsoft visual studio 8\vc\platformsdk\include\oaidl.h(2715) : see declaration of 'IDispatch::GetTypeInfo'
1> 'HRESULT IDispatch::GetIDsOfNames(const IID &,LPOLESTR *,UINT,LCID,DISPID *)' : is abstract
1> c:\program files\microsoft visual studio 8\vc\platformsdk\include\oaidl.h(2720) : see declaration of 'IDispatch::GetIDsOfNames'
1> 'HRESULT IDispatch::Invoke(DISPID,const IID &,LCID,WORD,DISPPARAMS *,VARIANT *,EXCEPINFO *,UINT *)' : is abstract
1> c:\program files\microsoft visual studio 8\vc\platformsdk\include\oaidl.h(2727) : see declaration of 'IDispatch::Invoke'
1> 'HRESULT aName::IMessage::Testing(void)' : is abstract
1> c:\...\aName.tlh(72) : see declaration of 'aName::IMessage::Testing'
1>c:\......cpp(22) : error C2664: 'aName::IMessage::IMessage(const aName::IMessage &)' : cannot convert parameter 1 from 'const _GUID' to 'const aName::IMessage &'
1> Reason: cannot convert from 'const _GUID' to 'const aName::IMessage'
1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
I created my dll, created a MyKeyFile, and I used the command to run regasm creating my .tlb file. All that seems fine. The C++ code I used came from a quick example on a microsoft support page. My issue is the error saying I have an astract class when I didn't declare an astract class. Maybe it has something to do with those methods that seems to be coming from HRESULT. I don't know if I'm making a mistake with the C++ code accessing the dll, or if I made a mistake within my dll. I just want to access a method in the dll. Any thoughts?
-= the best is yet to come =-
-
July 29th, 2008, 02:53 PM
#2
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.
-
August 1st, 2008, 06:17 PM
#3
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;
}
Last edited by Arjay; August 1st, 2008 at 07:48 PM.
-
August 18th, 2008, 06:25 AM
#4
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.
-
August 18th, 2008, 10:56 AM
#5
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.
-
August 18th, 2008, 02:25 PM
#6
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.
-
February 12th, 2014, 04:54 PM
#7
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?
-
February 12th, 2014, 05:18 PM
#8
Re: Access my C# dll in C++
[QUOTE=FredEx42;2147869]
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?
-
February 12th, 2014, 05:30 PM
#9
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.
-
February 12th, 2014, 10:41 PM
#10
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
Last edited by Arjay; February 12th, 2014 at 10:44 PM.
-
February 13th, 2014, 03:54 PM
#11
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|