Creating the DLL executable and attaching a process

 

// Reuse the previous DLL project. Project name: moredll, generating moredll.dll, you may create a new DLL project

// Using the Thread Local Storage (TLS) in the Dynamic Link Library program

#include <windows.h>

#include <stdio.h>

 

static DWORD dwTlsIndex; // address of shared memory

// DllMain() is the entry-point function for this DLL.

BOOL DllMain(HINSTANCE hinstDLL, // DLL module handle

DWORD fdwReason, // reason called

LPVOID lpvReserved) // reserved

{

LPVOID lpvData;

BOOL fIgnore;

 

switch (fdwReason)

{

// The DLL is loading due to process initialization or a call to LoadLibrary.

case DLL_PROCESS_ATTACH:

printf("Loading the DLL...\n");

// Allocate a TLS index.

if ((dwTlsIndex = TlsAlloc()) == 0xFFFFFFFF)

return FALSE;

 

// No break: Initialize the index for first thread. The attached process creates a new thread.

case DLL_THREAD_ATTACH:

printf("The attached process creating a new thread...\n");

// Initialize the TLS index for this thread.

lpvData = (LPVOID) LocalAlloc(LPTR, 256);

if (lpvData != NULL)

fIgnore = TlsSetValue(dwTlsIndex, lpvData);

break;

 

// The thread of the attached process terminates.

case DLL_THREAD_DETACH:

printf("The thread of the attached process terminates...\n");

// Release the allocated memory for this thread.

lpvData = TlsGetValue(dwTlsIndex);

if (lpvData != NULL)

LocalFree((HLOCAL) lpvData);

break;

 

// DLL unload due to process termination or FreeLibrary.

case DLL_PROCESS_DETACH:

printf("DLL unloading...\n");

// Release the allocated memory for this thread.

lpvData = TlsGetValue(dwTlsIndex);

if (lpvData != NULL)

LocalFree((HLOCAL) lpvData);

// Release the TLS index.

TlsFree(dwTlsIndex);

break;

 

default:

printf("Reason called not matched, error if any: %d...\n", GetLastError());

break;

}

return TRUE;

UNREFERENCED_PARAMETER(hinstDLL);

UNREFERENCED_PARAMETER(lpvReserved);

}

 

Output example:

 

// Need other program to run, see the other example

 

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: 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: Creating the Windows DLL and then attaching a process

To show: The Windows Dynamic Link Library programming using Win32 functions

 

 

 

C and C++ Programming Resources | C & C++ Code Example Index