How to Make DLL Files Using Command Prompt (Step-by-Step)

Written by

in

Creating a Dynamic Link Library (DLL) in C allows you to package code (functions and data) that can be shared among multiple applications, reducing memory usage and facilitating easier updates. A DLL is a Portable Executable (PE) file that exports functions which can be loaded and executed by other programs at runtime. 1. Create the C Source Code (mydll.c)

Create a file named mydll.c containing the functions you want to export. To ensure the functions are accessible outside the DLL, they must be explicitly exported using the __declspec(dllexport) directive, particularly when using the Microsoft Visual C++ (MSVC) compiler.

#include // Define the export macro #define DLL_EXPORT __declspec(dllexport) // A simple function to be exported DLL_EXPORT int AddNumbers(int a, int b) { return a + b; } // Another function DLL_EXPORT void HelloFromDLL() { printf(“Hello from the C DLL! “); } Use code with caution. 2. Build the DLL

You can build the DLL using command-line tools like MSVC (cl.exe). Command: cl.exe /LD mydll.c

Explanation: The /LD option tells the compiler to create a DLL instead of an executable (.exe). This will generate mydll.dll (the library) and mydll.lib (the import library). 3. Using the DLL

To use the DLL, the consuming application must import the functions. Option A: Load-time Linking (using the .lib file)

#include // Declare the functions as imported __declspec(dllimport) int AddNumbers(int a, int b); declspec(dllimport) void HelloFromDLL(); int main() { HelloFromDLL(); int result = AddNumbers(10, 20); printf(“Result: %d “, result); return 0; } Use code with caution. Compile this with: cl.exe main.c mydll.lib.

Option B: Runtime Linking (using LoadLibrary and GetProcAddress)This method does not require the .lib file, only the .dll.

#include #include int main() { // Load the DLL HINSTANCE hDll = LoadLibrary(“mydll.dll”); if (hDll != NULL) { // Define function pointer type typedef int (*AddFunc)(int, int); // Get function address AddFunc add = (AddFunc)GetProcAddress(hDll, “AddNumbers”); if (add != NULL) { printf(“Result: %d “, add(5, 5)); } FreeLibrary(hDll); } return 0; } Use code with caution. Summary Table: Key Concepts Description Define declspec(dllexport)

Tells the compiler to make the function available in the DLL. Compile /LD (MSVC) Tells the compiler to create a .dll file. Output .dll & .lib The DLL contains the code; the .lib enables easy linking. Consume LoadLibrary Runtime method to load the DLL into a process.

If you are using Visual Studio, you can create a “Dynamic-Link Library (DLL)” project, which automates the configuration of these steps, as shown in this Microsoft guide to Creating and Using a Dynamic-Link Library (C++). If you’d like, I can:

Show you how to set this up in the Visual Studio IDE instead of the command line.

Explain how to create a Module Definition (.def) file for explicit exports.

Provide an example of passing strings or structs between the application and DLL. Let me know which you prefer! Create and Use Your Own Dynamic-Link Library (C++)