简体   繁体   中英

P/Invoke c# and native c++

My c# is really bad, and I'll get a book or something to improve it later on. Right now, I have a gui in c# wpf and a dll library in native c++. All the tutorials I see on the internet for p/invoke don't have classes in them.

Here's what my code looks like in c++:

//Dostuff.h

class foo {
__declspec(dllexport) void dostuff();
 }

How would I go about calling the dll from c#? Thanks in advance.

A C++ library without wrappers is often useless outside of the C++ compiler that built it. C# won't make using such a library easy for you...and even if you manage to do it, things may break in all kinds of interesting ways.

You have two relatively safe choices, though:

  • Export a set of extern "C" wrapper functions in your library. You'd take a pointer to the object you want to use, and call a function on that object. If you go that route, it's about as close as you can get to universally compatible; any language that can call C functions will be able to use the library.
  • If you're using Visual C++, you can do something similar specifically for .net. Write a wrapper in C++/CLI that compiles for .net, but forwards calls to the native library.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM