有没有办法从 C# 应用程序调用非托管(非 COM)dll?

发布于 2024-07-12 06:41:48 字数 220 浏览 3 评论 0原文

有没有办法在我的 C# 应用程序中使用(引用)以非托管 C++(不是 COM 库)编写的 DLL?

当我尝试从 Visual Studio 中引用它时,收到“不是 COM 对象”错误消息。

也许有某种转换器\路由器可以 COMify 我的 DLL 引用? 我不知道 COM 和 COM 互操作是如何工作的,因为当我开始编程时,这对我来说已经没有必要了。

谢谢。

Is there a way to use (reference) a DLL written in an unmanaged C++ (not a COM library) in my C# application?

When I try to reference it from within Visual Studio, I get 'not a COM object' error message.

Maybe there is some kind of translator\router that would COMify my DLL reference?
I have no clue how COM and COM interop work, since I started programming when this was already unnecessary for me.

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

拒绝两难 2024-07-19 06:41:48

您需要使用 DllImport 属性。 下面是 Win32 PostMessage 函数的示例:

[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool PostMessage(IntPtr handle, int message, IntPtr wparam, IntPtr lparam);

You need to use the DllImport attribute. Here's an example for the Win32 PostMessage function:

[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool PostMessage(IntPtr handle, int message, IntPtr wparam, IntPtr lparam);
倾城°AllureLove 2024-07-19 06:41:48

请参阅 MSDN 上的“使用非托管 DLL 函数”主题:

http://msdn.microsoft.com/en-us/library/26thfadc.aspx

无需添加任何COM代理,.NET可以使用[DllImport] 属性。 您还可以通过指定其他属性来完全控制 .NET 和非托管 DLL 之间的编组。

See "Consuming unmanaged DLL functions" topic on MSDN:

http://msdn.microsoft.com/en-us/library/26thfadc.aspx

There is no need to add any COM proxy, .NET can consume DLLs directly using the [DllImport] attribute. You also have full control over the marshalling between .NET and the unmanaged DLL by specifying additional attributes.

入画浅相思 2024-07-19 06:41:48

Joe 已经回答了这个问题,所以我要补充一下 - 为了节省自己一些时间,而不必挖掘和破坏函数签名,P/Invoke 有一个相当完整的 Win32 签名库,以 wiki 的形式还有一个 Visual Studio 插件!

请访问 P/Invoke 查看

Joe already answered this, so I'm going to tack this on - to save yourself some time and not having to dig up and mangle function signatures, P/Invoke has a pretty complete library of Win32 signatures, in the form of both a wiki AND a Visual Studio plugin!

Check it out at P/Invoke

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文