H
H
HellFingers2019-12-08 19:54:39
C++ / C#
HellFingers, 2019-12-08 19:54:39

How to create a DLL correctly?

Good evening. There is a task to transfer data from c # to c ++. I look towards DLL. But off.docs are not very clear. Using VS 2019. I create a dll called Dll2, there are two defined files DLL2.h and DLL2.cpp. There are two questions. First, do I understand correctly that in the header file I should write the names of the methods used below in this way:

#ifdef DLL2_EXPORTS
#define DLL2_API __declspec(dllexport)
#else
#define DLL2_API __declspec(dllimport)
#endif
...
extern "C" DLL2_API int Summ(int first, int second);
...

Then, in the Dll2.cpp file, I have to describe this method:
#include "pch.h"
#include "framework.h"
#include "Dll2.h"
...
DLL2_API int Summ(int first, int second)
{
return first + second;
}

then I collect it all. And the second question is how do I correctly connect the Dll to the c# code. Should I use this code?
[DLLImport("path_to_DLL")]
public static extern void Summ(int first, int second);
static void Main()
{
Console.WriteLn(Summ(2,3));
}

Do not judge strictly for mistakes. Just started learning c++. Thanks in advance.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
H
HellFingers, 2019-12-09
@HellFingers

The problem was the compiler decorating the names. I changed DllImport to this:
In order to find a new name, I used DUMPBIN. Here is the sequence of actions:
1. Open Visual Studio
2. Tools -> Command Prompt -> Developer Command Prompt
3. Enter the following text at the command line: DUMPBIN /EXPORTS "path_to_your_dll"
4. Look for your function in the output and enter the value in the EntryPoint
I hope this will help someone.

A
Alexander Ananiev, 2019-12-08
@SaNNy32

https://docs.microsoft.com/en-us/dotnet/standard/n...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question