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.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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.