Skip to content

Instantly share code, notes, and snippets.

@justaguywhocodes
Created January 24, 2025 20:34
Show Gist options
  • Save justaguywhocodes/f4c1bc358e98ff72898592cbdd590988 to your computer and use it in GitHub Desktop.
Save justaguywhocodes/f4c1bc358e98ff72898592cbdd590988 to your computer and use it in GitHub Desktop.
#include <windows.h>
#include <stdio.h>
BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) {
switch (ul_reason_for_call) {
case DLL_PROCESS_ATTACH:
case DLL_PROCESS_DETACH: {
HANDLE hFile;
DWORD bytesWritten;
char *filename = "example.txt";
char *content = "This is a simple text file.\n";
// Create or open the file for writing
hFile = CreateFileA(
filename, // name of the file
GENERIC_WRITE, // open for writing
0, // do not share
NULL, // default security
CREATE_ALWAYS, // create new file, always
FILE_ATTRIBUTE_NORMAL, // normal file
NULL); // no attr. template
if (hFile == INVALID_HANDLE_VALUE) {
printf("Error creating file: %d\n", GetLastError());
return FALSE;
}
// Write content to the file
if (!WriteFile(hFile, content, strlen(content), &bytesWritten, NULL)) {
printf("Error writing to file: %d\n", GetLastError());
CloseHandle(hFile);
return FALSE;
}
// Close the handle
CloseHandle(hFile);
if (ul_reason_for_call == DLL_PROCESS_ATTACH) {
printf("File created on DLL load.\n");
} else {
printf("File created on DLL unload.\n");
}
break;
}
}
return TRUE;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment