none
Reference Native DLL from Managed C# project

    Question

  • I am using Visual Studio 2008 and am targeting a Smart Device.

     

    As part of the solution I have 2 projects:-

    1. A native library function (Win32 Smart Device project) that compiles to a DLL

    2. I have a application written in C# that has a managed wrapper class that uses DllImport to make the calls the native library functions.

     

    When I deploy my managed application I want the DLL to be deployed too. How is this done?

     

    I have tried to Add Reference from the Managed app to the DLL but I get "A reference to DLL could not be added".

     

    Thanks,

    Brian

    Wednesday, November 03, 2010 3:51 PM

Answers

  • I would suspect you're trying to add reference to a file which is only possible to do with managed assemblies and some COM files.

    Here's what you should do:

    1. Compile your solution.

    2. Right click on the managed project and select "Add/Existing Item". Do not use "Add Reference".

    3. Navigate to your compiled native DLL and select it (adjust file types as needed).

    4. Click on the down arrow in the "Add" split button and select "Add As Link" (which is what I meant by "adding as reference" - sorry I have not used VS 2008 in a while).

    5. Right click on that freshly added file and select "Properties".

    6. Make sure "Build Action" is "Content" and "Copy To Output Directory" is set to "Copy always" or "Copy if newer".

    7. Right click on the managed project and select "Project Dependencies".

    8. Check your native DLL in the list which would appear.

    You now should be all set.

     


    This posting is provided "AS IS" with no warranties, and confers no rights.
    • Marked as answer by BM_ Friday, November 05, 2010 7:51 AM
    Wednesday, November 03, 2010 11:47 PM
    Moderator
  • Since VS deployment is commonly only used for debugging purposes it is rarely needed.

    However if for some reason you need it then you have few options:

    1. You might be able to change project file manually using standard macros (including debug/release) in a path to the native DLL instead of hard coded path.

    2. In a native project create post build task which would copy either debug or release DLL to a fixed folder and add this file to the managed project.

    3. Change output folder in a native project so both release and debug build point to the same folder.


    This posting is provided "AS IS" with no warranties, and confers no rights.
    • Marked as answer by BM_ Friday, November 05, 2010 7:51 AM
    Thursday, November 04, 2010 4:19 PM
    Moderator

All replies

  • You can add native DLL to your managed project (use 'add as reference' so copy will not be created), configure it as content and set deployment option as needed (e.g. deploy if newer). Now set your native project as dependency of a managed one so native DLL would be rebuild as you deploy.
    This posting is provided "AS IS" with no warranties, and confers no rights.
    Wednesday, November 03, 2010 4:53 PM
    Moderator
  • Ilya,

     

    I have tried to add a reference to the DLL (on Managed project References in solution explorer tree, right click, Add reference ..., Projects Tab, Select Natave DLL). When I do that I get the warning:-

     

    "A reference to abc.DLL could not be added."

     

    What am I doing wrong?

     

    Thanks,

    Brian

    Wednesday, November 03, 2010 5:21 PM
  • I would suspect you're trying to add reference to a file which is only possible to do with managed assemblies and some COM files.

    Here's what you should do:

    1. Compile your solution.

    2. Right click on the managed project and select "Add/Existing Item". Do not use "Add Reference".

    3. Navigate to your compiled native DLL and select it (adjust file types as needed).

    4. Click on the down arrow in the "Add" split button and select "Add As Link" (which is what I meant by "adding as reference" - sorry I have not used VS 2008 in a while).

    5. Right click on that freshly added file and select "Properties".

    6. Make sure "Build Action" is "Content" and "Copy To Output Directory" is set to "Copy always" or "Copy if newer".

    7. Right click on the managed project and select "Project Dependencies".

    8. Check your native DLL in the list which would appear.

    You now should be all set.

     


    This posting is provided "AS IS" with no warranties, and confers no rights.
    • Marked as answer by BM_ Friday, November 05, 2010 7:51 AM
    Wednesday, November 03, 2010 11:47 PM
    Moderator
  • Hi Ilya,

     

    This much clearer and I see were I was confused with the wording "Add reference". I think I am almost there.

     

    There is one thing using this method. When I add the compiled native code (step 3 above). I have an option to choose the Release or Debug version of the DLL. Now that decision is made when I add this DLL. Really what I would like is if the current Managed application is a Debug build then it would take the native Debug DLL, if it is a Release DLL I would like it to build with the native Release DLL.

    Is this possible?

     

    Thanks,

    Brian

    Thursday, November 04, 2010 9:32 AM
  • Since VS deployment is commonly only used for debugging purposes it is rarely needed.

    However if for some reason you need it then you have few options:

    1. You might be able to change project file manually using standard macros (including debug/release) in a path to the native DLL instead of hard coded path.

    2. In a native project create post build task which would copy either debug or release DLL to a fixed folder and add this file to the managed project.

    3. Change output folder in a native project so both release and debug build point to the same folder.


    This posting is provided "AS IS" with no warranties, and confers no rights.
    • Marked as answer by BM_ Friday, November 05, 2010 7:51 AM
    Thursday, November 04, 2010 4:19 PM
    Moderator
  • Ilya,

    Fantastic responses to all my questions!

    We have chosen option 2 and created a post-build script in the native dll build, to copy the a common folder that the Managed project picks up.

     

    The last challange was in the Compact Framework to ensure in  the DLL was loaded by the Application after it was deployed on the device - but that is another story!

     

    Many thanks!

    Brian

    Friday, November 05, 2010 7:51 AM