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

preview_player
Показать описание
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)

Рекомендации по теме
Комментарии
Автор

I normally turn a video straight off if I see text and no mic, but this was good! Thanks.

liamlluls
Автор

This tutorial is pure gold. Nice and easy. Thanks alot

venomousdan
Автор

Wow! amazing demonstration. Really helpful.

shankarghimire
Автор

Amazing sharing and tutorial! Thank you so much.

billtsou
Автор

Cool. Now how do you call classes from C++ in C#?

ghostravenstorm
Автор

Thank you for demonstration, it'll help me in the near future

kakoytochel
Автор

Thanks, I've searched for this for while

sydeweizgt
Автор

If you have a BadImageFormatException, either your address string is wrong or try changing the Platform Target in ( Project Properties -> Build -> Platform Target ) in your C# Project
Also the name of the struct doesn't matter - it's the format that matters.
I changed the struct name and its variable names it still works. It's probably good practice to make it same though.

Manas-cowl
Автор

Great content and nice background music ! :)

ClintonVincen
Автор

nice you help me understand calling C# code in C++ dll code

VirtualMatterTails
Автор

I don't know English, but you helped me insanely! Thank you very much! Like! Subscription! Thanks!

КонстантинРачковский-вю
Автор

i want to make a dll in cpp with the libmodbus library and then use that dll in c# so can you tell how i can do that?

AbhishekBade
Автор

Thanks, the best video in this theme!!! U've helped me so match

roninalone
Автор

I was getting "syntax error: 'int' should be preceded by ';'" until I swapped out the macro for the "_declspec(dllexport)" in front of the functions.

mattarnold
Автор

How do you call C++ exe's from GCC with C#; that is, C++ not created in Visual Studio?

scramjet
Автор

Nowhere did I find instructions on how to do this in .NET 5 or .NET Core 3.1, it seems that it is no longer supported. just like the old man.
When testing, the PInvok call always fails with the exception of

jsrustcsharpcppc
Автор

Is there a way to call functions of a shared object in c#. Calling functions within a .so file

Sj-zewk
Автор

Thanks for video. I tried it with class but result some strange 8 digits numbers. What can I make to fix it? Could you please help?

shahinabdullayev
Автор

Okay, i can use simple void, can do something with int, but cant work with string BTSR its return memory trash or 0, also i dunno how i can get access to events(callback) in Csharp.

DimitryArsenev
Автор

Literally cloned it at November 2024 and changed nothing and it doesn't work O_O let me know if you know how to do it on newer versions, trying to do it for the long time :(

yelhighmusic
visit shbcf.ru