adding dll reference into managed C++ visual studio 2008 project
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: adding dll reference into managed C++ visual studio 2008 project

  1. #1
    Join Date
    Sep 2008
    Posts
    4

    Talking adding dll reference into managed C++ visual studio 2008 project

    adding dll reference into managed C++ visual studio 2008 project

    Hi, guys
    If you open http://support.microsoft.com/kb/310674 link, you will see there are several ways that make using references in a Managed C++ application.
    I tried all of them, but i still cannot see the object of the dll reference project.
    My project is a mixed mode project which contains normal C++ MFC classes and a managed C++ class which communicates with external dll objects.
    From my managed C++ class I am going to add the dll reference which contains many classes and objects from another project.
    I am able to register the dll file and create tlb file but with including those dll or tlb file in my manged C++ class i still cannot seethe external objects.
    Then, i tried to create an interop assembly for a COM object, using the TLBIMP.exe tool, i get the following errors.
    tlbimp externalobj.dll /out:Interop.externalobj.dll
    "TlbImp : error TI0000 : The input file externalobj.dll is not a valid type library."

    Anybody had the same problems and solved it or knows what the problem is, please teach me.
    Thanks.
    - james
    Last edited by arfte; September 13th, 2008 at 11:46 PM. Reason: modifying

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

    Re: adding dll reference into managed C++ visual studio 2008 project

    You're trying to get your native code to interact with your managed dll through COM. You can't just add it as a reference.

    You should use

    Code:
    #import "<dotnet dll tlb file>.tlb"
    in your native code. This will import all the COM symbols from your type library allowing you to access your managed code.

    There are lots of resources on the net to show you how to do this, but here's a brief primer :

    If we have this in a C++/CLI dll (which is standard pattern for exposing to COM - you should use it).

    Code:
    // .h file in MyDll.dll
    
    using namespace System::Runtime::InteropServices;
    
    // this GUID you should generate
    [ComVisible(true)]
    [Guid("341e1c8f-d842-459c-949f-ba83b7966b17")]
    public interface class MyInterface
    {
    public:
        virtual void MyMethod() abstract;
    } ;
    
    // again you should generate this GUID
    [ComVisible(true)]
    [Guid("b4c3cd7d-f57c-40c7-b269-9553f619f2d7")]
    public ref class MyInterfaceImpl : public MyInterface
    {
    public:
        virtual void MyMethod() override
        {
            Console::WriteLine("MyMethod");
        }
    } ;
    After you've built this into a dll (e.g. MyDll.dll) and generated the tlb file using regasm e.g.

    Code:
    regasm /tlb /codebase MyDll.dll
    in your native C++ code you should do :

    Code:
    #import "MyDll.tlb" no_namespace
    
    void ExampleMethod()
    {
        MyInterfacePtr myInterface(__uuidof(MyInterfaceImpl));
        myInterface->MyMethod();
    }
    You HAVE to set the GUIDs otherwise regasm will generate new ones every time you register your objects which will lead to problems.

    I would recommend learning a bit about using COM as well otherwise you'll hit problems with parameter types (strings get turned into BSTRs).

    Darwen.
    Last edited by darwen; September 14th, 2008 at 08:01 AM.
    www.pinvoker.com - PInvoker - the .NET PInvoke Interface Exporter for C++ Dlls.

  3. #3
    Join Date
    Sep 2008
    Posts
    4

    Re: adding dll reference into managed C++ visual studio 2008 project

    Hi, darwen

    Thanks for your reply.
    I think you misunderstood my question.
    I can handle the problem easily if i use com objects as you described in your post.
    My problem is, I have a mixed mode MFC project written in visual studio 2008.
    In the project, the standard C++ classes can reference the managed C++ class, and the managed C++ class should be able to reference the third party objects that come with dll files, so the managed C++ class acts as a wrapper class which creates a bridge between standard C++ class and the external classes in dll. (But the managed C++ class can not reference the external dll.)
    That's why i need to be able to see the contents of the dll by adding the reference of it in managed C++ class and so tried many ways as i mentioned before to solve it.

    - james

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

    Re: adding dll reference into managed C++ visual studio 2008 project

    So are you trying to access a native dll which isn't COM - just an ordinary Win32 dll ?

    If so then you can do one of 2 things.

    (a) If you have a .lib file then you need to add this as an input to the linker. Then just #include any .h files you have to go with the dll.
    (b) If you don't have a .lib file for the dll then you can dynamically load the dll using the LoadLibrary function and access the exported methods using GetProcAddress.
    (c) If you don't have any .lib or .h files for the dll you're stuck.

    If you have the source to the dll and are building it yourself then both the .lib file and .h files will be available to you and you can do (a).

    You'll have to put the built dll in the same folder as the exe/dll which uses it for it to work.

    There's a tutorial on windows dlls here.

    Darwen.
    www.pinvoker.com - PInvoker - the .NET PInvoke Interface Exporter for C++ Dlls.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center