简体   繁体   中英

Is this possible to write Win32 .dll files by C#.NET?

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?

Thanks.

Yes, it is possible. In fact there is a tool for that. It is a little bit hidden in Rainmeter application.

Rainmeter SDK for C# included an executable named CS_DLL_FOR_C.exe which converted public static methods to external functions callable by native applications. In fact Rainmetere itself is developed in C++ and loads plugins by LoadLibrary

You can download x86 and x64 version of this tool using following links

x86 version

x64 version

PS: i do not know if it is against licence of Rainmeter to distribute this tool.

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.

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.

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.

--

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.

You can expose any .NET assembly that you write as a com object.

See these tutorials (for VS 2003, but still valid).

Of course you can create DLL Files in C#.

All you need is to choose Class Library in your Project Type when you create a new Project in Visual Studio.

Here is a tutorial : http://www.c-sharpcorner.com/uploadfile/mahesh/dll12222005064058am/dll.aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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