CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Jan 2006
    Location
    Baltimore, Maryland, USA
    Posts
    104

    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 =-

  2. #2
    Join Date
    Jan 2002
    Location
    Scaro, UK
    Posts
    5,940

    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.
    www.pinvoker.com - PInvoker - the .NET PInvoke Interface Exporter for C++ Dlls.

  3. #3
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    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.

  4. #4
    Join Date
    Aug 2008
    Posts
    1

    Exclamation 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.

  5. #5
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    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.

  6. #6
    Join Date
    Jan 2002
    Location
    Scaro, UK
    Posts
    5,940

    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.
    www.pinvoker.com - PInvoker - the .NET PInvoke Interface Exporter for C++ Dlls.

  7. #7
    Join Date
    Feb 2014
    Posts
    3

    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?

  8. #8
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Access my C# dll in C++

    [QUOTE=FredEx42;2147869]
    Quote Originally Posted by darwen View Post
    (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?

  9. #9
    Join Date
    Feb 2014
    Posts
    3

    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.

  10. #10
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    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.

  11. #11
    Join Date
    Feb 2014
    Posts
    3

    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
  •  





Click Here to Expand Forum to Full Width

Featured