Exporting the Windows DLL through the .lib and .dll files
// Project name: mydllpro, Source file name: mysrcdll.cpp, generating mydllpro.dll, mydllpro.lib...
// The mydll() function writes a null-terminated string to the standard output device...
// The export mechanism used here is the __declspec(export) method supported by Microsoft Visual Studio, but any
// other export method supported by your development environment may be substituted.
// For WinXp, don't forget to add Advapi32.lib library if needed...
#define _WIN32_WINNT 0x0501
#include <windows.h>
#include <stdio.h>
#define EOF (-1)
#ifdef __cplusplus // If used by C++ code,
extern "C" { // we need to export the C interface
#endif
__declspec(dllexport) int mydll(LPTSTR lpszMsg)
{
DWORD cchWritten;
HANDLE hStdout;
BOOL fRet;
printf("-This is mydll.dll file lol!-\n");
// Get a handle to the standard output device.
hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
if (INVALID_HANDLE_VALUE == hStdout)
{
// failed to get the handle, give some message, get error code, just exit...
printf("GetStdHandle(), invalid handle, error: GetLastError().\n");
return EOF;
}
else
printf("GetStdHandle(), standard handle is OK.\n");
// Write a null-terminated string to the standard output device.
while (*lpszMsg != '\0')
{
fRet = WriteFile(hStdout, lpszMsg, 1, &cchWritten, NULL);
if ((fRet == FALSE) || (cchWritten != 1))
// If something wrong just exit or provide meaningful message/error code...
return EOF;
// else, write more...
lpszMsg++;
}
printf("\n");
return 1;
}
#ifdef __cplusplus
}
#endif
// The created DLL need other program to run. Before other program can import/run/call mydll.dll, you need to copy
// the mydllpro.lib and mydllpro.dll to appropriate folders. Those lib and dll file should be in the ../debug folder.
// For my Windows XP pro SP2 machine, the mydllpro.lib has been copied to folder: C:\Program Files\Microsoft Visual Studio 8\VC\lib
// and mydllpro.dll has been copied to folder: C:\WINDOWS\system32
// Then we are ready to use the DLL for next example.
Output example:
None
Compiler: Visual C++ Express Edition 2005
Compiled on Platform: Windows Xp Pro SP2
Target platform: none, just for learning and fun
Header file: Standard and Windows
Additional library: Windows Platform SDK
Additional project setting: Set project to be compiled as C.
Project->your_project_name Properties->Configuration Properties->C/C++->Advanced->Compiled As: Compiled as C Code (/TC)
Other info: This is a DLL project. non-CLR or unmanaged.
Need to add Advapi32.lib (Advapi32.dll) to the project. Click the Project menu->Select the your_project_name Properties...sub menu->Expand the Configuration Properties folder on the left pane->Expand the Linker subfolder->Select the Input subfolder->Select the Additional Dependencies field on the right pane->Click the ... at the end of the field->Type in 'Advapi32.lib' in the empty pane->Click the OK button->Click the OK button second time to close the project Properties dialog.
To do: Exporting the Windows DLL using the .lib and .dll files
To show: The Win32 C program example on using the Dynamic Link library