filmov
tv
C# - How to call C++ code in C# (Calling C++ methods in C# using DLL Importing)

Показать описание
I'll show you how you can call C++ functions in C#. You simply just need to import them using DllImport and thats basically it.
In your C++ project properties/settings, in general, set the Configuration Type to Dynamic Library. This makes it output a DLL. Idk if you can keep it as application, because an application requires a main function. and (i think) putting it as Dynamic Library removes that requirement.
In your C# code you just need to import the DLL using [DllImport]. AFAIK the calling convention needs to be Cdcel. im not sure why you dont need it for other DLLs, like user32... but oh well.
If you get some warning messages about memory management... i only got those when i didn't include set the calling convention to Cdcel. if you do get it, you might have to do some googling as only starting doing this "C++ code called from C#" for 2 days. maybe look into DLL calls on the msdn (microsoft's documentation)
In your C++ project properties/settings, in general, set the Configuration Type to Dynamic Library. This makes it output a DLL. Idk if you can keep it as application, because an application requires a main function. and (i think) putting it as Dynamic Library removes that requirement.
In your C# code you just need to import the DLL using [DllImport]. AFAIK the calling convention needs to be Cdcel. im not sure why you dont need it for other DLLs, like user32... but oh well.
If you get some warning messages about memory management... i only got those when i didn't include set the calling convention to Cdcel. if you do get it, you might have to do some googling as only starting doing this "C++ code called from C#" for 2 days. maybe look into DLL calls on the msdn (microsoft's documentation)
Комментарии