How to use an unmanaged C library from a VC++ CLI project
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: How to use an unmanaged C library from a VC++ CLI project

  1. #1
    Join Date
    Jun 2009
    Posts
    3

    How to use an unmanaged C library from a VC++ CLI project

    I need to access two external APIs: one a native C interface (no classes), the other a C# .NET assembly. I have build a CLI Windows Forms project in VC++ 2008 that can access the .NET assembly.

    When I attempt to link the LIB file for the native DLL, I get the message,

    LNK1313: ijw/native module detected; cannot link with pure modules

    I have tried different compiler and linker settings of /CLI, but without success.

    Tried: Options>Linker>Advanced>CLRImageType = Force IJW (was Default) & rebuild
    Result: fatal error: LINK1309: pure module detected AssemblyInfo.cpp

    Tried: AssemblyInfo.cpp > Options > C/C++>General>Compile with CLR support = /clr (was /clrure)
    Result: command-line option '/clrure' inconsistent with precompiled header
    How do I force the precomipled header to recompile? Build <solution> didn't work.

    Am I going about this the right way?
    Is there some header, assembly, compiler option, #pragma statement or other step that I am missing for mixing managed C++ with an unmanaged C library?

    Thank you,
    Jeff Allan

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

    Re: How to use an unmanaged C library from a VC++ CLI project

    Dont link to the C# .NET assembly via it's .lib file, add the dll as a reference to the project.

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

  3. #3
    Join Date
    Oct 2008
    Location
    Cologne, Germany
    Posts
    756

    Re: How to use an unmanaged C library from a VC++ CLI project

    for a mixed-mode dll in c++/cli you should use only /clr

    /clrure stands for: Pure MSIL is comprised of MSIL instructions, so code containing functions that cannot be expressed in MSIL will prevent compilation. (source: http://msdn.microsoft.com/en-us/libr...53(VS.80).aspx)

    take a look at this http://msdn.microsoft.com/en-us/libr...52(VS.80).aspx
    win7 x86, VS 2008 & 2010, C++/CLI, C#, .NET 3.5 & 4.0, VB.NET, VBA... WPF is comming

    remeber to give feedback you think my response deserves recognition? perhaps you may want to click the Rate this post link/button and add to my reputation

    private lessons are not an option so please don't ask for help in private, I won't replay

    if you use Opera and you'd like to have the tab-button functionality for the texteditor take a look at my Opera Tab-UserScirpt; and if you know how to stop firefox from jumping to the next control when you hit tab let me know

  4. #4
    Join Date
    Jun 2009
    Posts
    3

    Re: How to use an unmanaged C library from a VC++ CLI project

    Quote Originally Posted by darwen View Post
    Dont link to the C# .NET assembly via it's .lib file, add the dll as a reference to the project.

    Darwen.
    Darwen,

    Thanks, that's how I am using the .NET assembly and that is working fine.

    My problem is how to also include (link, reference, whatever) the native C API which is in the form a .LIB and a .DLL. Can I include the .LIB file using Project>Add Existing Iterm? Or do I reference the .DLL using Project>Options>Linker>Input>Additional Dependencies?

    The problem I'm having seems to be with the "/CLR: pure" compile & link options. I will try again making everything /CLR instead.

    Jeff

  5. #5
    Join Date
    Jun 2009
    Posts
    3

    Re: How to use an unmanaged C library from a VC++ CLI project [solution]

    The solution involved 3 steps:
    - setting the linker option to /CRL
    - setting the compiler option for each module to /CRL
    - disabling precompiled headers

    FYI, I included the .LIB file of the native library using Project > Add Existing Item

    Thanks to those who replied.

    Jeff Allan

Tags for this Thread

Posting Permissions

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


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center