1 /* 2 vfdlib.c 3 4 Virtual Floppy Drive for Windows 5 Driver control library 6 Miscellaneous functions 7 8 Copyright (C) 2003-2008 Ken Kato 9 */ 10 11 #ifdef __cplusplus 12 #pragma message(__FILE__": Compiled as C++ for testing purpose.") 13 #endif // __cplusplus 14 15 #define WIN32_LEAN_AND_MEAN 16 #include <windows.h> 17 #include <stdio.h> 18 19 #ifdef _DEBUG 20 #include <psapi.h> 21 #endif // _DEBUG 22 23 #include "vfdtypes.h" 24 #include "vfdapi.h" 25 #include "vfdlib.h" 26 27 // 28 // DLL Global variables 29 // 30 31 #ifndef __REACTOS__ 32 extern HINSTANCE g_hDllModule = NULL; // Handle to this DLL itself 33 extern UINT g_cDllRefCnt = 0; // Reference count of this DLL 34 extern UINT g_nNotifyMsg = 0; // VFD notification message 35 #else 36 HINSTANCE g_hDllModule = NULL; // Handle to this DLL itself 37 UINT g_cDllRefCnt = 0; // Reference count of this DLL 38 UINT g_nNotifyMsg = 0; // VFD notification message 39 #endif 40 41 // 42 // DllMain 43 // 44 BOOL WINAPI DllMain( 45 HINSTANCE hInstance, 46 DWORD dwReason, 47 LPVOID lpReserved) 48 { 49 #ifdef _DEBUG 50 char name[MAX_PATH]; 51 HMODULE hMod; 52 DWORD size; 53 54 if (EnumProcessModules(GetCurrentProcess(), &hMod, sizeof(hMod), &size)) { 55 GetModuleBaseName(GetCurrentProcess(), hMod, name, sizeof(name)); 56 } 57 else { 58 strcpy(name, "unknown"); 59 } 60 #endif // _DEBUG 61 62 UNREFERENCED_PARAMETER(lpReserved); 63 64 if (dwReason == DLL_PROCESS_ATTACH) { 65 VFDTRACE(0, ("DLL_PROCESS_ATTACH - %s\n", name)); 66 67 // this DLL doesn't need DLL_THREAD_ATTACH and DLL_THREAD_DETACH 68 DisableThreadLibraryCalls(hInstance); 69 70 // store the DLL instance handle 71 g_hDllModule = hInstance; 72 73 // register the VFD notification message 74 g_nNotifyMsg = RegisterWindowMessage(VFD_NOTIFY_MESSAGE); 75 76 } 77 else if (dwReason == DLL_PROCESS_DETACH) { 78 VFDTRACE(0, ("DLL_PROCESS_DETACH - %s\n", name)); 79 } 80 81 return TRUE; 82 } 83 84 // 85 // Check running platform 86 // 87 BOOL WINAPI VfdIsValidPlatform() 88 { 89 BOOL (WINAPI *pfnIsWow64Process)(HANDLE, PBOOL); 90 BOOL wow64; 91 92 if (GetVersion() & 0x80000000) { 93 return FALSE; // doesn't work on Win9x 94 } 95 96 pfnIsWow64Process = (BOOL (WINAPI *)(HANDLE, PBOOL)) 97 GetProcAddress(GetModuleHandle("kernel32.dll"), "IsWow64Process"); 98 99 if (pfnIsWow64Process == NULL) { 100 return TRUE; // NT4 or 2000 -- assured to be 32 bit 101 } 102 103 wow64 = FALSE; 104 105 if (!pfnIsWow64Process(GetCurrentProcess(), &wow64)) { 106 return FALSE; 107 } 108 109 return !wow64; 110 } 111 112 // 113 // Get VFD notification message value 114 // 115 UINT WINAPI VfdGetNotifyMessage() 116 { 117 return g_nNotifyMsg; 118 } 119 120 121 // 122 // Get message text from this DLL module 123 // 124 PSTR ModuleMessage( 125 DWORD nFormat, ...) 126 { 127 PSTR p; 128 va_list args; 129 130 va_start(args, nFormat); 131 132 if (!FormatMessage( 133 FORMAT_MESSAGE_FROM_HMODULE | 134 FORMAT_MESSAGE_ALLOCATE_BUFFER, 135 g_hDllModule, nFormat, 0, (LPTSTR)&p, 0, &args)) { 136 p = NULL; 137 } 138 139 va_end(args); 140 141 return p; 142 } 143 144 // 145 // Get system error message string 146 // 147 PCSTR SystemMessage( 148 DWORD nError) 149 { 150 static CHAR msg[256]; 151 152 if (!FormatMessage( 153 FORMAT_MESSAGE_FROM_SYSTEM | 154 FORMAT_MESSAGE_IGNORE_INSERTS, 155 NULL, nError, 0, msg, sizeof(msg), NULL)) { 156 157 _snprintf(msg, sizeof(msg), 158 "Unknown system error %lu (0x%08x)\n", nError, nError); 159 } 160 161 return msg; 162 } 163 164 #ifdef _DEBUG 165 // 166 // Format and output debug string 167 // 168 void DebugTrace( 169 PCSTR sFormat, ...) 170 { 171 CHAR buf[512]; 172 int len; 173 va_list args; 174 175 len = _snprintf(buf, sizeof(buf), 176 "%s(%lu) : ", TraceFile, TraceLine); 177 178 va_start(args, sFormat); 179 180 _vsnprintf(buf + len, sizeof(buf) - len, sFormat, args); 181 182 OutputDebugString(buf); 183 } 184 #endif // _DEBUG 185