call unmanaged C++ code from C# using pinvoke

后端 未结 3 1317
南旧
南旧 2021-01-05 13:23

I have a unmanaged C++ dll for which I do not have access to code but have all methods declarations for.

Lets for simplicity say that .h looks like this:

<         


        
相关标签:
3条回答
  • 2021-01-05 13:46

    Using PInvoke you can only call global functions exported from Dll. To use exported C++ classes, you need to write C++/CLI wrapper. This is C++/CLI Class Library project, which exposes pure .NET interface, internally it is linked to unmanaged C++ Dll, instantiates a class from this Dll and calls its methods.

    Edit: you can start from this: http://www.codeproject.com/KB/mcpp/quickcppcli.aspx#A8

    0 讨论(0)
  • 2021-01-05 14:04

    To use a class from native C++ from C# you need a C++/CLi wrapper in between, as mentioned by by previous answers. To actually do that, it is not very straight forward. Here is I link that tell you how to do it at a high level: C++/CLI wrapper for native C++ to use as reference in C#.

    If you are quite new to this (like me), you might stumble on 1) -- the linking part. To solve that, you can see how I did here (see my question portion): Link error linking from managed to unmanaged C++ despite linking to .lib file with exported symbols

    0 讨论(0)
  • 2021-01-05 14:12

    If you need to create a wrapper, take a look at swig.org. It will generate one for most high level language like C#.

    I just came across this program a few minutes ago while working the same problem that you are.

    0 讨论(0)
提交回复
热议问题