简体   繁体   English

如何为 C# DLL 提供 C 兼容接口?

[英]How to give a C# DLL a C-compliant interface?

A third-party app I have can call extension DLLs if they have C-compliant interfaces as described below.我拥有的第三方应用程序可以调用扩展 DLL,如果它们具有如下所述的 C 兼容接口。

I would like the third-party app to call my C# DLL, since I would rather write in C# than C.我希望第三方应用程序调用我的 C# DLL,因为我宁愿写 C# 而不是 Z0D61708470CAD14D412F5 (Maybe my other choice would be to write a C DLL wrapper to call my C# DLL, which is an extra step that I might not get right). (Maybe my other choice would be to write a C DLL wrapper to call my C# DLL, which is an extra step that I might not get right).

I've searched the net and SO but haven't found (or recognized) a good match to my question.我已经搜索了网络和 SO,但没有找到(或识别出)与我的问题很好的匹配。

The best suggestion seemed to be this special marshalling declarations in the C# code here: Calling a C# DLL from C;最好的建议似乎是 C# 代码中的这种特殊编组声明: Calling a C# DLL from Z01261F884DFE43D; linking problems 链接问题

Is it possible to write a C# DLL that has a C-compliant interface like the one below?是否可以编写一个具有如下 C 兼容接口的 C# DLL? If so, could someone tell me what I have to do or point me to some documentation?如果是这样,有人可以告诉我我必须做什么或指向我一些文件吗?

The third-party app documentation says, "The DLL function must be a PASCAL function that takes a single LPCSTR (long pointer to a constant string) argument. C or C++ DLL functions called must have prototypes equivalent to: The third-party app documentation says, "The DLL function must be a PASCAL function that takes a single LPCSTR (long pointer to a constant string) argument. C or C++ DLL functions called must have prototypes equivalent to:

extern "C" __declspec(dllexport) void __stdcall fn(LPCTSTR szParam );

Take a look at this article: https://www.codeproject.com/Articles/12512/Using-the-CDECL-calling-convention-in-C-changing看看这篇文章: https://www.codeproject.com/Articles/12512/Using-the-CDECL-calling-convention-in-C-chang

It discusses a different problem (opposite to yours) but also explains the linkage of C# functions - by default C# uses __stdcall (and it cannot be overridden).它讨论了一个不同的问题(与您的问题相反),但也解释了 C# 函数的链接 - 默认情况下 C# 使用 __stdcall (并且它不能被覆盖)。 This means your third party app should be able to call the C# function as it stands.这意味着您的第三方应用程序应该能够按原样调用 C# function。 Have you tried it?你试过了吗? Did you get any errors?你有没有得到任何错误?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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