Home   Notes   Contact Me

C++ Microsoft


Make code run before main

See this article: "Running Code Before and After Main" http://www.codeguru.com/cpp/misc/misc/threadsprocesses/article.php/c6945__1/

/* prepostdemo.c */ #include <windows.h> int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) { MessageBox(NULL, TEXT("Hello, Main Program!"), TEXT("Hello"), 0); return 0; } int PreMain1(void) { MessageBox(NULL, TEXT("First Pre-Main Function"), TEXT("PreMain1"), 0); return 0; } int PreMain2(void) { MessageBox(NULL, TEXT("Second Pre-Main Function"), TEXT("PreMain2"), 0); return 0; } int PostMain1(void) { MessageBox(NULL, TEXT("First Post-Main Function"), TEXT("PostMain1"), 0); return 0; } int PostMain2(void) { MessageBox(NULL, TEXT("Second Post-Main function"), TEXT("PostMain2"), 0); return 0; } typedef int cb(void); #pragma data_seg(".CRT$XIU") static cb *autostart[] = { PreMain1, PreMain2 }; #pragma data_seg(".CRT$XPU") static cb *autoexit[] = { PostMain1, PostMain2 }; #pragma data_seg() /* reset data-segment */

How not to need .def files

Here are sample .h and .cpp files:


Find Files


HANDLE findHandle;
WIN32_FIND_DATA fd;

findHandle = ::FindFirstFile( "*.h", &fd);
if ( findHandle == INVALID_HANDLE_VALUE) {
	return -1;
}
if ( fd.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY) {
}

::FindNextFile( findHandle, &fd));