需要 C#/C++动态链接库建议
我已成功让 Dreamweaver 接受我的 DLL 并调用它的方法。 DLL 是用 C++ 编写的,带有 C 插入函数(因此函数名称不会被破坏。)
我想熟悉 C++ 指针和数据类型,但我对 C# 的熟练程度要高得多,所以对于我们的时间线,我认为值得研究直接 C 解决方案的替代方案。
理论上我是否能够用 C# 构建一个从“外部”看到的与 C DLL 相同的 DLL?
并且作为替代方案
我是否能够“包装”一个C# DLL 与工作的 C DLL?
我关心的主要部分是函数的数据类型,因为当 Dreamweaver 调用它们时,它期望 void 和 char 指针作为来回传递数据的格式。
谢谢。
I have successfully gotten Dreamweaver to accept my DLL and call it's methods. The DLL is written in C++ with C interpeted functions (so function names aren't mangled.)
I would like to become comfortable with C++ pointers and datatype, but I have a much stronger proficiency with C# so for our timeline I think it's worthwhile to investigate alternatives to a straight C solution.
Would I theoretically be able to build a DLL in C# that would be seen from the "outside" the same as a C DLL?
and as an alternative
Would I be able to "wrap" a C# DLL with the working C DLL?
The main part of my concern is the datatypes of the functions, as when dreamweaver calls them it is expecting void and char pointers as the format for passing data back and forth.
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
与普遍看法相反,这是可能的。
请参阅此处。
Contrary to popular belief, this is possible.
See here.
是的,C++/CLI 编译器使这一切变得很容易。它会自动生成一个“thunk”,确保当非托管程序调用导出函数时加载 CLR。最好用一个例子来证明。
从 C# 代码开始,从类库项目模板创建一个新项目。您必须编写一个静态方法,这是必要的,因为 C 代码对类一无所知。下面是一个简单的示例:
在“解决方案资源管理器”窗口中右键单击解决方案名称,然后选择“添加 + 新建项目”。选择“Visual C++”、“CLR”、“类库”。右键单击新项目,属性,公共属性,框架和引用,单击添加新引用。在“项目”选项卡中,选择您的 C# 项目。
打开 .cpp 文件并编写如下代码:
Build。这个DLL现在可以被你的非托管代码使用,它可以调用“方法”函数。该 DLL 和您的 C# DLL 都需要复制到非托管程序的目录中。
请注意,调用此方法会产生开销。 C++/CLI 编译器生成的 thunk 做了相当多的工作来确保 CLR 被加载和初始化。调试将变得无趣(使用 Debugger.Break()),并且未捕获的托管异常会导致程序崩溃,并且诊断效果非常差。
Yes, the C++/CLI compiler makes this easy to do. It automatically generates a "thunk" that ensures that the CLR gets loaded when an unmanaged program calls an exported function. Best demonstrated with an example.
Starting with the C# code, create a new project from the Class Library project template. You'll have to write a static method, necessary because C code doesn't know anything about classes. Here's a simple example:
Right-click the solution name in the Solution Explorer window, Add + New Project. Select Visual C++, CLR, Class Library. Right-click the new project, Properties, Common Properties, Framework and References, click Add New Reference. Projects tab, select your C# project.
Open the .cpp file and write code like this:
Build. This DLL can now be used by your unmanaged code, it can call the "method" function. Both this DLL and your C# DLL need to be copied to the directory of the unmanaged program.
Beware that there's overhead in calling this method. The thunk that the C++/CLI compiler generates does a fair amount of work to ensure that the CLR is loaded and initialized. Debugging is going to be unfun (use Debugger.Break()) and uncaught managed exceptions will crash your program with a very poor diagnostic.
您可以创建围绕 .NET DLL 的 COM 包装器。我现在使用它从一些较旧的 C/C++ 代码中调用 .NET 函数。
You can create a COM wrapper around a .NET DLL. I am using this now to call .NET functions from some older C/C++ code.