How do I write a DLL file in C?
I was looking online, but I always get tutorials for C++, not C. I want to write my first DLL file in C. How can I do that? What would a short example be?
Type "regsvr32 /u DLL NAME" into the command prompt, substituting the actual DLL name without the brackets for "DLL NAME." Press enter. The DLL file is now no longer running on your system.
If you need to use your DLL from . NET languages - write it in C#, it won't be a windows dll, just an assembly. Very easy to use. If you need to use your DLL from ONLY C++ and ONLY from applications written by the same compiler, write in C++.
Let's get you started on your first DLL:
Templates
, select Win32 Project
.Application Type
(In the Application Settings
tab).Empty Project
and press Finish
.You need to attach an empty source file to the blank project:
Source Files
, Add -> Add New Item and then select C++ File
and give the name to it.Open
.In the opened window, enter the following code:
#include <stdio.h> extern "C" { __declspec(dllexport) void DisplayHelloFromMyDLL() { printf ("Hello DLL.\n"); } }
__declspec(dllexport)
is an obligatory prefix which makes DLL functions available from an external application.
extern “C”
(with braces, for scoping) shows that all code within brackets is available from “outside” the file. Although code will compile even without this statement, during runtime, you will get an error. (I leave this as an experiment for you).
Build this application and your DLL file is ready.
Refer to Walkthrough: Creating and Using a Dynamic Link Library for more information on how to do addition and stuff.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With