3

Before I ask my question, here are some informations about my development environment:

  • Operating System: Windows 64 bit

  • IDE: Visual Studio 2013

I would like to control a device over a C# project. What I have got from the producer of the device: a .h-file and a static library (.lib-file). In a C++-Console-Application the controlling works fine. But now I have some issues to build a DLL for C#

To build such a .dll-file I created an empty C++ DLL Console-Application, copied the .h-file into the project and referenced the .lib-file to the linker (added an "additional library direction" and an "additional dependency" in the solution settings).

But at this step, the problem occurs. When I build the project, the console prints

========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========

The log files do not show any errors but I have no DLL file in the Debug (or Release) folder.

What I tried: I added a "fake" .cpp-file where I implement one of the functions from the .h-file -> dll is built. So I guess that Visual Studio does not recognize the .lib file.

The next step would be to write a C# "wrapper" with the DllImport commands.

Thank you in advance for all answers and tips.

Blackbriar
  • 477
  • 4
  • 14
  • 1
    This may help. If my understanding is correct. https://stackoverflow.com/questions/772041/using-c-library-in-c-sharp – Some Guy With a PC Oct 01 '18 at 13:47
  • Sadly not. My problem is located in building the dll, not using it. This would be the next step, that I mentioned in my question. – Blackbriar Oct 01 '18 at 13:53
  • Do look in the right folder, unlike C# a C++ project builds to the solution folder, not the project folder. But sure, a static library is not a great start for a DLL. You need a .def file that explicitly lists what specific functions need to be exported. Also keep in mind that you can't pinvoke any C++ class instance methods or any function that uses C++ template types like std::string, that requires a wrapper written in C++/CLI. – Hans Passant Oct 01 '18 at 13:54
  • There is no such thing as "C++ DLL Console-Application". It must be a dll project. – Robert Andrzejuk Oct 01 '18 at 15:44
  • @HansPassant Thank you, but that was not the problem. I am adding the `__declspec(dllexport)` tags to my functions. So I do not need a .def-file, right? And I am not trying to export class instance methods or functions that use things like std::string or something similar. @RobertAndrzejuk New Project -> Visual C++ -> Win32 Console Application -> Application Type: DLL. Hope this helps for understanding. – Blackbriar Oct 02 '18 at 06:46

1 Answers1

2

On Window's, functions are not automatically exported from DLL's, either ones in the builds own source files (c/cpp) or from linked static libraries.

You could export them by adding __declspec(dllexport) to the function declarations, or you can add a "Module-Definition File (.def)" to the project, and list the functions to export. For example here a function foo can be from a static lib included in my DLL.

LIBRARY TEST
EXPORTS  
   foo @1 

For it not creating a file at all, not sure if MS consider it a bug or feature, but it does not create the DLL if the project has no actual source files. You need at least one, it can even be empty.

Fire Lancer
  • 29,364
  • 31
  • 116
  • 182
  • Thank you for the tips. I created an empty .cpp file and the DLL is created now. But now another problem occured, which I will describe in an answer to the question. – Blackbriar Oct 02 '18 at 06:27