locked
Adding a dll reference to a project RRS feed

  • Question

  • Hi!

    I am trying to call functions in a C project from a C#.

    I compiled and created a dll for the C project however when adding the reference, using add reference in the project menu, it does not load the dll delivering this error message:
    "A reference to the "....dll" could not be added.Please make sure that the file is accessible and that it is a valid assembly or COM component."

    One thing that may help is that when using the P/Invoke Interop Assistant when I try to open the dll it says:
    "Could not load assembly <'...>.dll'.
    Could not load file or assembly '<...>.dll or one of its dependencies. The module was expected to cointain an assembly manifest."

    Any idea?  Thanks"
    Thursday, April 10, 2008 10:52 PM

Answers

  • Unmanaged DLLs are not added as references to managed projects.  References are strictly for managed assemblies (or managed proxies created for COM binaries). 

    To use an unmanaged DLL in managed code you need only use the DllImport attribute on the function extern definition.  You'll have to be sure that the unmanaged DLL is in a path searchable by the managed code (LoadLibrary algorithm).  Here's an example:

     

    [DllImport("Mydll.dll")]

    private static extern void Foo ( int parm );

     

    Michael Taylor - 4/11/08

    http://p3net.mvps.org

     

     

    Friday, April 11, 2008 1:16 PM
    Moderator

All replies

  • Unmanaged DLLs are not added as references to managed projects.  References are strictly for managed assemblies (or managed proxies created for COM binaries). 

    To use an unmanaged DLL in managed code you need only use the DllImport attribute on the function extern definition.  You'll have to be sure that the unmanaged DLL is in a path searchable by the managed code (LoadLibrary algorithm).  Here's an example:

     

    [DllImport("Mydll.dll")]

    private static extern void Foo ( int parm );

     

    Michael Taylor - 4/11/08

    http://p3net.mvps.org

     

     

    Friday, April 11, 2008 1:16 PM
    Moderator
  • Hi,

    I'm getting a big headache seeking around how to reference an unmanaged DLL.
    I have a C# project using an external DLL, but it doesn't find the library at runtime.
    The hard point is how to add the DLL file in order to deploy it with the whole project ? (I'm working with a Smart Device project)

    Thanks.

    Friday, December 31, 2010 9:59 AM
  • Please post new questions in the forums rather than appending to existing, answered questions.  Otherwise you run the risk of the question being missed.

    In answer to your question you cannot add an unmanaged DLL to a managed project via Solution Explorer.  Deployment doesn't rely on Solution Explorer contents anyway (although it might obtain some base information from it).  Instead you normally add the additional files (such as third-party binaries, documentation, images, etc) through an options dialog in the deployment project or wizard.  Since you're asking about smart devices then I recommend that you post this question in the Smart Devices forum as they will have a better understanding of CF deployments.

    Michael Taylor - 1/4/2011
    http://msmvps.com/blogs/p3net

    Tuesday, January 4, 2011 2:37 PM
    Moderator