简体   繁体   中英

Automatically perform C# wrapping of a C++ library

I've done a bit of research around this before posting but I couldn't find a direct answer specific to my environment.

I have a third party library written in C++. I have access to the .dll , .lib and .h headers of the library.

I'd like to wrap the library for use in C# using p/invoke. I'd like to know what options I have in terms of doing this and maintaining it. My hope here is that with the .h headers I can utilize some application to automatically provide the wrapper code.

I'm interested in all kinds of solutions including commercial software solutions and any pitfalls of trying to automatically manage the wrapper code.

You could try SWIG which is free. You may be able to script it to run automatically as part of a build process.

Personally I'd just write some wrappers using [DllImport] . Do you really need to access everything in the C++ library from C#? Often I've found you really only need to call a few functions, and the simplicity of dllimport is great

My hope here is that with the .h headers I can utilize some application to automatically provide the wrapper code.

It would be lovely if this were possible, but it is not. It is not possible because the header file does not contain enough information to determine how to marshal parameters. For instance, consider this declaration:

void foo(int *x);

What is x ? Its type is int* , pointer to int . But is it a pointer to a single int value, or is it an array of int ? You simply cannot tell from the header file. You need to read the documentation.

And what about data flow. Is the information flowing into the native code, or out of it, or in both directions? Again, that detail cannot be expressed a header file.

Now, there are various annotation conventions that can help with this. Essentially these are macros that evaluate to nothing that can be read by a tool and so help with conversion. You see Win32 API functions annotated with _In_ , _In_Opt_ , etc. These can help, but only the very simplest of libraries can be automatically translated into p/invoke declarations.

Now, if you are prepared to add extra annotation to the raw header files then you might have a chance. It is certainly possible that existing tools exist that will do a good job so long as you express the missing information in comments or macros. SWIG is certainly worth a look, and there are other tools.

The fundamental point of my answer is to try to get across the idea that such translations are not as automatic as you might hope. Personally, I always end up writing my p/invoke declarations by hand. This allows me to get them exactly the way I want, and maintenance is not really a big deal because DLL interfaces typically don't change. They don't change because you don't want to break binary compatibility.

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