How do you create a WPF app that accesses a 32-bit unmanaged c++ DLL?

JN Lifetouch 21 Reputation points
2020-05-15T17:27:51.75+00:00

I have an SDK for a USB-connected camera. This is a 32-bit, unmanaged, c or c++ dll, with no source code. It comes with an import library (.lib file), several c/c++ header files, and virtually no documentation.
I am writing a WPF application that I want to use to call the API methods in the SDK. As I understand it, I need to write a C++/CLI 'wrapper' class library to make the methods in the SDK DLL accessible to the C# application.
I have been working on this for several days, and have read literally hundreds of articles/texts/etc. on StackOverflow, Microsoft documentation pages, and dozens of other sources. I just keep running into one problem after another, and all the resources I've found seem to be missing some critical piece of the puzzle.
I apologize for the way I am presenting this, but here are some of the questions I have:

  1. Do I need to "Copy Always" or "Copy if Newer" the API dll to the $Solution folder?
  2. Do I need a Reference in the Wrapper project to reference the API dll?
  3. In the wrapper, do I need to specify an 'extern' declaration to API methods?
  4. In the wrapper, do I need to specify an 'extern "C"' declaration to API methods?
  5. Do I use PInvoke to call API methods?
  6. Do I use the DllImportAttribute to tell where to find the API method?
  7. Do I need to specify the Calling Convention for the API method?
  8. Do I need to use LoadLibrary to get access to the API dll?
  9. Do I need to link to a static .lib (import library) file from my Wrapper project? 10. Does the Project Platform of my Wrapper need to be x86 (for a 32-bit API dll)? 11. Can the Project Platform for the C# .NET app be "Any CPU"? 12. Will the JIT compiler figure out at runtime what platform to compile to? 13. Is 'Marshaling' required between the C# app and the managed c++/CLI Wrapper? 14. Is 'Marshaling' required between the managed c++/CLI Wrapper and the unmanaged API dll? 15. Is COM required for a c# app to use an unmanaged c/c++ .dll library? 16. Do I need to use System.Runtime.InteropServices? 17. Do I need to set "prefer 32-bit" on the solution or either of the projects?

Thank you!

Not Monitored
Not Monitored
Tag not monitored by Microsoft.
35,518 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Vahid Ghafarpour 16,290 Reputation points
    2023-08-27T06:07:29.19+00:00

    You should set the API DLL to "Copy if Newer" in your C++/CLI wrapper project. This ensures that the DLL gets copied to the output directory of your C# application when you build it.

    0 comments No comments