是否可以通过C#.NET编写Win32 .dll文件? [英] Is this possible to write Win32 .dll files by C#.NET?

查看:155
本文介绍了是否可以通过C#.NET编写Win32 .dll文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是由C#.NET编写Win32 .dll文件吗?
如果是,我可以在哪里找到一些教程?
如果没有,那么你的建议如何写Win32 .DLLs?

Is this possible to write Win32 .dll files by C#.NET ? If yes, where i can find some tutorials? If no, so what's your suggestion to write Win32 .DLLs?

谢谢。

推荐答案

这听起来像是试图用Win32风格的出口创建一个经典的Win32 DLL? (Win32风格的出口是DllImport所选择的。)您不能用托管代码(C#或托管C ++)来执行此操作。据我所知,您需要使用非托管C / C ++来执行此操作。

It sounds like you're trying to create a classic Win32 DLL with Win32-style exports? (Win32-style exports are what DllImport picks up.) You can't do that with managed code (C# or managed C++); as far as I know, you'll need to use unmanaged C/C++ to do that.

编程Windows由Charles Petzold或类似于Win32编程的书可能是一个开始的好地方或找到一个hello worldWin32 DLL示例。或者只需在Visual Studio中创建一个Win32 DLL项目,并查看它为您创建的文件,可能应该有一些占位符文本来让您开始。

Programming Windows by Charles Petzold or a similar book on programming Win32 might be a good place to start; or find a 'hello world' Win32 DLL sample. Or just create a Win32 DLL project in Visual Studio and look at the files it creates for you, likely there should be some placeholder text to get you started.

托管代码使用也有.dll作为扩展名的文件,但它们是托管代码的容器,并且没有Win32样式的入口点,因此期望Win32入口点的其他代码无法使用它们。他们有时被称为管理集会而不是dll,以避免混淆。

Managed code does use files that also have .dll as the extension, but they are really containers for managed code, and don't have Win32-style entry points, so other code that's expecting Win32 entry points can't use them. They are sometimes referred to as "managed assemblies" rather than dlls to avoid confusion.

-

更新 - 可能有一个备用选项:您可以创建一个所谓的混合模式DLL,它是一个包含本机代码和托管代码的DLL。然后,该DLL可以具有要导出的托管C#类,以及导出Win32样式API的少量混合C / C ++。虽然这是可能的,但并不一定是一件好事:希望加载一个简单的Win32 DLL的代码可能并不期望得到CLR的加载;如果进程已经具有已加载的CLR的冲突版本,则可能会遇到问题。混合模式DLL往往更有用于允许C#程序集重用C / C ++代码,而不是允许C#/托管代码伪装成Win32 DLL。

Update - there might be an alternate option: you can create what's called a "Mixed mode DLL" which is a DLL that contains both native code and managed code. This DLL could then have the managed C# class that you want to 'export', and a small amount of mixed C/C++ that exports the Win32-style APIs. While this is possible, it's not necessarily a good thing to do: code that's expecting to load a plain Win32 DLL may not be expecting to get the CLR loaded as a consequence; and you could hit problems if the process already has a conflicting version of the CLR already loaded. Mixed-mode DLLs tend to be more useful for allowing a C# assembly to reuse C/C++ code, rather than allowing C#/managed code to masquerade as a Win32 DLL.

这篇关于是否可以通过C#.NET编写Win32 .dll文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆