1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/*
	vfdlib.c

	Virtual Floppy Drive for Windows
	Driver control library
	Miscellaneous functions

	Copyright (C) 2003-2008 Ken Kato
*/

#ifdef __cplusplus
#pragma message(__FILE__": Compiled as C++ for testing purpose.")
#endif	// __cplusplus

#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <stdio.h>

#ifdef _DEBUG
#include <psapi.h>
#endif	// _DEBUG

#include "vfdtypes.h"
#include "vfdapi.h"
#include "vfdlib.h"

//
// DLL Global variables
//

#ifndef __REACTOS__
extern HINSTANCE	g_hDllModule = NULL;	// Handle to this DLL itself
extern UINT			g_cDllRefCnt = 0;		// Reference count of this DLL
extern UINT			g_nNotifyMsg = 0;		// VFD notification message
#else
HINSTANCE	g_hDllModule = NULL;	// Handle to this DLL itself
UINT			g_cDllRefCnt = 0;		// Reference count of this DLL
UINT			g_nNotifyMsg = 0;		// VFD notification message
#endif

//
// DllMain
//
BOOL WINAPI DllMain(
	HINSTANCE		hInstance,
	DWORD			dwReason,
	LPVOID			lpReserved)
{
#ifdef _DEBUG
	char			name[MAX_PATH];
	HMODULE			hMod;
	DWORD			size;

	if (EnumProcessModules(GetCurrentProcess(), &hMod, sizeof(hMod), &size)) {
		GetModuleBaseName(GetCurrentProcess(), hMod, name, sizeof(name));
	}
	else {
		strcpy(name, "unknown");
	}
#endif	// _DEBUG

	UNREFERENCED_PARAMETER(lpReserved);

	if (dwReason == DLL_PROCESS_ATTACH) {
		VFDTRACE(0, ("DLL_PROCESS_ATTACH - %s\n", name));

		//	this DLL doesn't need DLL_THREAD_ATTACH and DLL_THREAD_DETACH
		DisableThreadLibraryCalls(hInstance);

		//	store the DLL instance handle
		g_hDllModule = hInstance;

		//	register the VFD notification message
		g_nNotifyMsg = RegisterWindowMessage(VFD_NOTIFY_MESSAGE);

	}
	else if (dwReason == DLL_PROCESS_DETACH) {
		VFDTRACE(0, ("DLL_PROCESS_DETACH - %s\n", name));
	}

	return TRUE;
}

//
//	Check running platform
//
BOOL WINAPI VfdIsValidPlatform()
{
	BOOL (WINAPI *pfnIsWow64Process)(HANDLE, PBOOL);
	BOOL wow64;

	if (GetVersion() & 0x80000000) {
		return FALSE;		// doesn't work on Win9x
	}

	pfnIsWow64Process = (BOOL (WINAPI *)(HANDLE, PBOOL))
		GetProcAddress(GetModuleHandle("kernel32.dll"), "IsWow64Process");

	if (pfnIsWow64Process == NULL) {
		return TRUE;		// NT4 or 2000 -- assured to be 32 bit
	}

	wow64 = FALSE;

	if (!pfnIsWow64Process(GetCurrentProcess(), &wow64)) {
		return FALSE;
	}

	return !wow64;
}

//
//	Get VFD notification message value
//
UINT WINAPI VfdGetNotifyMessage()
{
	return g_nNotifyMsg;
}


//
//	Get message text from this DLL module
//
PSTR ModuleMessage(
	DWORD			nFormat, ...)
{
	PSTR	p;
	va_list	args;

	va_start(args, nFormat);

	if (!FormatMessage(
		FORMAT_MESSAGE_FROM_HMODULE |
		FORMAT_MESSAGE_ALLOCATE_BUFFER,
		g_hDllModule, nFormat, 0, (LPTSTR)&p, 0, &args)) {
		p = NULL;
	}

	va_end(args);

	return p;
}

//
//	Get system error message string
//
PCSTR SystemMessage(
	DWORD			nError)
{
	static CHAR		msg[256];

	if (!FormatMessage(
		FORMAT_MESSAGE_FROM_SYSTEM |
		FORMAT_MESSAGE_IGNORE_INSERTS,
		NULL, nError, 0, msg, sizeof(msg), NULL)) {

		_snprintf(msg, sizeof(msg),
			"Unknown system error %lu (0x%08x)\n", nError, nError);
	}

	return msg;
}

#ifdef _DEBUG
//
//	Format and output debug string
//
void DebugTrace(
	PCSTR			sFormat, ...)
{
	CHAR			buf[512];
	int				len;
	va_list			args;

	len = _snprintf(buf, sizeof(buf),
		"%s(%lu) : ", TraceFile, TraceLine);

	va_start(args, sFormat);

	_vsnprintf(buf + len, sizeof(buf) - len, sFormat, args);

	OutputDebugString(buf);
}<--- va_list 'args' was opened but not closed by va_end().
#endif	// _DEBUG