#import no longer works

Hi there

I have been using a COM DLL in VS .NET 2003 and it has worked fine. I create a reference in the solution explorer and add a #import statement at the top of the form.h file, add a var which is a pointer to the "command interface" of the DLL and it all just works. I've been struggling to do the same under VC++ 2005 and it just seems to be completely different. Using a #import statement generates the following error:

#import is not supported with /clr:pure and /clr:safe

Can someone please point me at some doco which explains the procedure for including a COM DLL in a C++/CLR app in VS .net 2005.

Thanks
Dave



Answer this question

#import no longer works

  • Michael_SQL

    I have 4 references to import from a vendor and this method worked well for 3 of the 4 references. When I try to import the 4th one I get a message that the application has to close. I have sent Microsoft the error report but need to know how to get around this issue to continue. Any ideas what would cause this issue
  • gswartz

    Hi Ron

    The problem is the file that isn't compiling is created by the compiler. The top of the file says:

    // Created by Microsoft (R) C/C++ Compiler Version 14.00.50215.44 (6792e71e).
    //
    // d:\my documents\visual studio 2005\projects\testvizwindow\testvizwindow\debug\pwsactivex.tlh
    //
    // C++ source equivalent of Win32 type library pwsActivex.dll
    // compiler-generated file created 07/09/05 at 10:35:46 - DO NOT EDIT!

    Since I have no control over that, what is the method in 2005 of importing an ActiveX DLL, since making the DLL a reference and putting a #import statement at the top of my code, causes the compiler to generate a file it can't compile

    Thanks
    Dave


  • spicoli7

    Hi Ronald

    Thanks for that, but I now have a few more errors.  Actually, it gets to 100, then the compiler dies.

    Here are the first and last few:

    d:\my documents\visual studio 2005\projects\testvizwindow\testvizwindow\debug\pwsactivex.tlh(32) : error C3149: 'PWSACTIVEXLib::IEmptyControl' : cannot use this type here without a top-level '^'
    d:\my documents\visual studio 2005\projects\testvizwindow\testvizwindow\debug\pwsactivex.tlh(33) : error C3149: 'PWSACTIVEXLib::IvizCommunication' : cannot use this type here without a top-level '^'
    d:\my documents\visual studio 2005\projects\testvizwindow\testvizwindow\debug\pwsactivex.tlh(44) : error C2011: 'PWSACTIVEXLib::IEmptyControl' : 'interface' type redefinition
            d:\my documents\visual studio 2005\projects\testvizwindow\testvizwindow\interop\interop.pwsactivexlib.1.0.dll : see declaration of 'PWSACTIVEXLib::IEmptyControl'

    ...

    2005\projects\testvizwindow\testvizwindow\debug\pwsactivex.tli(173) : fatal error C1003: error count exceeds 100; stopping compilation


    This file being compiled has been generated from the activeX DLL. Is this another switch, or is there something really wrong This all works fine under VS2003.

    Thanks
    Dave


  • Al33327

    In VC++ 2005, we are introducing new language extensions to target .NET called C++/CLI.  If your program is targeting .NET and works fine with VC++ 2003 it is probably using the old language extensions for targeting .NET ("Managed Extensions").  The Managed Extensions are deprecated starting with VC++ 2005 but are still available using the /clr:oldSyntax switch.

    Thanks,

    -Ron Pihlgren
     VC++ Testing

  • NitinVed

    Hi Dave,

    Sorry for taking so long to get back on this.

    >>Since I have no control over that, what is the method in 2005 of importing an ActiveX DLL, since making the DLL a reference and putting a #import statement at the top of my code, causes the compiler to generate a file it can't compile

    If it were me, I wouldn't use #import at all.  To use a COM DLL from managed code you can do this:

    1. Add a reference to the COM object (Select the project in solution explorer and on the main menu, select Project\References...  Under Common Properties, select References.  Click the Add New Reference... button and then click on the COM tab (if the COM DLL is registered) or Browse (If it isn't) and select your COM DLL.  Visual Studio will create a managed wrapper around your COM DLL.

    2. Do a #using <(the name of the managed wrapper that was generated).DLL> at the top of your source file, the same as if you were including a managed assembly.

    3. You can now create objects and use them as if they are managed classes.  To see what the generated wrapper looks like, you can use the Object Browser.

    Let me know if this isn't what you're looking for

    Thanks,

    -Ron Pihlgren
     VC++ Testing



  • bchadraa MSFT

    Don't use the /clr:safe or /clr:pure switches. Just use /clr.

    Ronald Laeremans
    Visual C++ team

  • Jordy Boom

    Ron, your suggestion works fine with pure COM dlls, but what about dotnet dlls with dual interface

    I needed to make a call from unmanaged c++ to managed code. I know it can be done, but it's not a trivial task, so instead I made my managed code to support COM interface and was planning to make unmanaged c++ to work with my managed c# dll over the COM interface.

    I followed this article - http://support.microsoft.com/kb/828736 - using VS.NET 2005 and also found that #import does not work anymore. I get the following warning and the errors from compiling the auto-generated tlh file:

    warning C4099: : type name first seen using 'class' now seen using 'struct'

    error C3149: cannot use this type here without a top-level '*'
    error C2011: interface' type redefinition

    Since I have my c# dll also registered as COM dll, I tried your suggestion to just reference it using the IDE, but I got the following error in the project's property page:

    Creating command line ""...\tlbimp.exe" /out:"...\Interop\Interop.csharplib.1.0.dll" /transform: dispret /namespace:csharplib "...\csharplib.tlb""
    Microsoft (R) .NET Framework Type Library to Assembly Converter 2.0.50727.42
    Copyright (C) Microsoft Corporation. All rights reserved.
    TlbImp : error TI0000 : System.Runtime.InteropServices.COMException - Type library 'csharplib' was exported from a CLR assembly and cannot be re-imported as a CLR assembly.

    So, I can't reference my dll using #import and I cannot reference it using Add Reference ... how do I reference it then

  • #import no longer works