Writing a DLL in C/C++ for .Net interoperability

后端 未结 4 1267
星月不相逢
星月不相逢 2020-12-04 18:26

In my C# application, I would like to write a part of the code in C. I plan to write a DLL witch would be interoperable with .Net. How can I do that?

4条回答
  •  心在旅途
    2020-12-04 19:10

    Below is an example for an application where I had to do just that. In my case, I needed a DLL to wrap calls to functions that were only available in a .lib. The key part is the extern "C" __declspec (dllexport) in the declaration. That's basically all you need. The rest was merely using dllimport in the C# app and getting the marshalling right.

    extern "C" __declspec (dllexport) LONG EstablishContext(DWORD dwScope, 
                                                        LPCVOID pvReserved1, 
                                                        LPCVOID pvReserved2, 
                                                        LPSCARDCONTEXT phContext)
    {
        return SCardEstablishContext(dwScope, pvReserved1, pvReserved2, phContext);
    }
    

提交回复
热议问题