A
A
askova2018-04-19 11:42:55
.NET
askova, 2018-04-19 11:42:55

Can any data types be compiled into a DLL (C++) for use in a C# project?

Good day, colleagues!
Need expert advice!
There is a C ++ project of the lib-library and a project of demonstrating work with the library (C ++) - Visual Studio 2017.
The library has various structures, classes, constants, enumerations, unions (unions), etc. data types necessary for work libraries. The main goal of the library is to establish a connection with the device and receive information in real time (parsing bit information).
You need to somehow use the sources of the C ++ lib library, so that you can use these developments in a C # project. Using the example: https://msdn.microsoft.com/en-us/library/ms235636.aspxmanaged to compile the dll, but it only works in the C++ project as in the example, in the C# project it is not possible to use it with DLLImport - entry point errors.
Direct on the right path or help with a specific solution to the current problem.
All projects can be sent as needed.
For example, how to access a variable in a library or class?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
anikavoi, 2018-04-19
@anikavoi

There, calls are made through ... well, you understand.
[DllImport("../../../Release/RContDll1.dll", EntryPoint = "math_add", CallingConvention = CallingConvention.StdCall)]
static extern int Add(int a, int b);
And the library should have this:
extern "C"
{
__declspec(dllexport) int __stdcall math_add(int a, int b)
{
return a + b;
}
}
IMHO, it's easier to write in C a "shim" that C# will call, and it, in turn, will call a real dll function.
Data is transferred normally - at the address of the structure, and only then in C # the circus begins with [MarshalAs(UnmanagedType.ByValTStr, SizeConst = ....)]

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question