Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

517 lines
19 KiB

  1. //************************************************************************
  2. // Generic Win 3.1 fax printer driver support. Helper functions which are
  3. // called in WOWFAXUI.C
  4. //
  5. // History:
  6. // 14-mar-95 reedb Created. Many of the functions were moved from
  7. // FAXCOMM.C, since they're no longer called
  8. // from WOWFAX.DLL.
  9. // 16-aug-95 reedb Move to kernel mode. Debug output and validate
  10. // functions moved from FAXCOMM.C.
  11. //
  12. //************************************************************************
  13. #include "wowfaxui.h"
  14. #include "dde.h"
  15. extern LPCRITICAL_SECTION lpCriticalSection;
  16. extern HINSTANCE ghInst;
  17. BOOL InSetupMode(void);
  18. #if DBG
  19. INT iFaxLogLevel = 20;
  20. INT iReqFaxLogLevel = 0;
  21. typedef PVOID HANDLE;
  22. //************************************************************************
  23. // faxlogprintf - Two different implementations. One for client side
  24. // debugging the other for server side.
  25. //
  26. //************************************************************************
  27. // For Debug logging.
  28. #define MAX_DISPLAY_LINE 256 // 128 characters.
  29. TCHAR szFaxLogFile[] = L"C:\\FAXLOG.LOG";
  30. HANDLE hfFaxLog = NULL;
  31. // Defines for iFaxLogMode
  32. #define NO_LOGGING 0
  33. #define LOG_TO_FILE 1
  34. #define OPEN_LOG_FILE 2
  35. #define CLOSE_LOG_FILE 3
  36. INT iFaxLogMode = NO_LOGGING;
  37. VOID faxlogprintf(LPTSTR pszFmt, ...)
  38. {
  39. DWORD lpBytesWritten;
  40. int len;
  41. TCHAR szText[1024];
  42. va_list arglist;
  43. va_start(arglist, pszFmt);
  44. len = wvsprintf(szText, pszFmt, arglist);
  45. if (iFaxLogMode > LOG_TO_FILE) {
  46. if (iFaxLogMode == OPEN_LOG_FILE) {
  47. if((hfFaxLog = CreateFile(szFaxLogFile,
  48. GENERIC_WRITE,
  49. FILE_SHARE_WRITE,
  50. NULL,
  51. CREATE_ALWAYS,
  52. FILE_ATTRIBUTE_NORMAL,
  53. NULL)) != INVALID_HANDLE_VALUE) {
  54. iFaxLogMode = LOG_TO_FILE;
  55. }
  56. else {
  57. hfFaxLog = NULL;
  58. iFaxLogMode = NO_LOGGING;
  59. OutputDebugString(L"Couldn't open fax log file!\n");
  60. }
  61. }
  62. else {
  63. FlushFileBuffers(hfFaxLog);
  64. CloseHandle(hfFaxLog);
  65. hfFaxLog = NULL;
  66. iFaxLogMode = NO_LOGGING;
  67. }
  68. }
  69. if (iFaxLogLevel >= iReqFaxLogLevel) {
  70. if (iFaxLogMode) {
  71. WriteFile(hfFaxLog, szText, len, &lpBytesWritten, NULL);
  72. }
  73. else {
  74. OutputDebugString(szText);
  75. }
  76. }
  77. }
  78. VOID LogWowFaxInfo(LPWOWFAXINFO lpWowFaxInfo)
  79. {
  80. faxlogprintf(L"\tlpWowFaxInfo (lpMap): %X\n", lpWowFaxInfo);
  81. faxlogprintf(L"\t\thwnd: %X\n", lpWowFaxInfo->hwnd);
  82. faxlogprintf(L"\t\ttid: %X\n", lpWowFaxInfo->tid);
  83. faxlogprintf(L"\t\tproc16: %X\n", lpWowFaxInfo->proc16);
  84. faxlogprintf(L"\t\tlpinfo16: %X\n", lpWowFaxInfo->lpinfo16);
  85. faxlogprintf(L"\t\tmsg: %X\n", lpWowFaxInfo->msg);
  86. faxlogprintf(L"\t\thdc: %X\n", lpWowFaxInfo->hdc);
  87. faxlogprintf(L"\t\twCmd: %X\n", lpWowFaxInfo->wCmd);
  88. faxlogprintf(L"\t\tcData: %X\n", lpWowFaxInfo->cData);
  89. faxlogprintf(L"\t\thwndui: %X\n", lpWowFaxInfo->hwndui);
  90. faxlogprintf(L"\t\tretvalue: %X\n", lpWowFaxInfo->retvalue);
  91. faxlogprintf(L"\t\tstatus: %X\n", lpWowFaxInfo->status);
  92. if (lpWowFaxInfo->lpDevice) {
  93. faxlogprintf(L"\t\tlpDevice: %s\n", lpWowFaxInfo->lpDevice);
  94. }
  95. else {
  96. faxlogprintf(L"\t\tlpDevice: %X\n", lpWowFaxInfo->lpDevice);
  97. }
  98. if (lpWowFaxInfo->lpDriverName) {
  99. faxlogprintf(L"\t\tlpDriverName: %s\n", lpWowFaxInfo->lpDriverName);
  100. }
  101. else {
  102. faxlogprintf(L"\t\tlpDriverName: %X\n", lpWowFaxInfo->lpDriverName);
  103. }
  104. if (lpWowFaxInfo->lpPortName) {
  105. faxlogprintf(L"\t\tlpPortName: %s\n", lpWowFaxInfo->lpPortName);
  106. }
  107. else {
  108. faxlogprintf(L"\t\tlpPortName: %X\n", lpWowFaxInfo->lpPortName);
  109. }
  110. faxlogprintf(L"\t\tlpIn: %X\n", lpWowFaxInfo->lpIn);
  111. faxlogprintf(L"\t\tlpOut: %X\n", lpWowFaxInfo->lpOut);
  112. if (lpWowFaxInfo->szDeviceName) {
  113. faxlogprintf(L"\t\tszDeviceName: %s\n", lpWowFaxInfo->szDeviceName);
  114. }
  115. else {
  116. faxlogprintf(L"\t\tszDeviceName: %X\n", lpWowFaxInfo->szDeviceName);
  117. }
  118. faxlogprintf(L"\t\tbmPixPerByte: %X\n", lpWowFaxInfo->bmPixPerByte);
  119. faxlogprintf(L"\t\tbmWidthBytes: %X\n", lpWowFaxInfo->bmWidthBytes);
  120. faxlogprintf(L"\t\tbmHeight: %X\n", lpWowFaxInfo->bmHeight);
  121. faxlogprintf(L"\t\tlpbits: %X\n", lpWowFaxInfo->lpbits);
  122. }
  123. VOID LogFaxDev(LPTSTR pszTitle, LPFAXDEV lpFaxDev)
  124. {
  125. DWORD dwTmp;
  126. CHAR cTmp0, cTmp1, cTmp2, cTmp3;
  127. faxlogprintf(L"WOWFAXUI!%s: %X\n", pszTitle, lpFaxDev);
  128. dwTmp = lpFaxDev->id;
  129. cTmp3 = (CHAR) dwTmp & 0xFF;
  130. cTmp2 = (CHAR) (dwTmp >> 8) & 0xFF;
  131. cTmp1 = (CHAR) (dwTmp >> 16) & 0xFF;
  132. cTmp0 = (CHAR) (dwTmp >> 24) & 0xFF;
  133. faxlogprintf(L"\tid: %c%c%c%c\n", cTmp3, cTmp2, cTmp1, cTmp0);
  134. faxlogprintf(L"\tlpNext: %X\n", lpFaxDev->lpNext);
  135. faxlogprintf(L"\tlpClient: %X\n", lpFaxDev->lpClient);
  136. faxlogprintf(L"\thdev: %X\n", lpFaxDev->hdev);
  137. faxlogprintf(L"\tidMap: %X\n", lpFaxDev->idMap);
  138. faxlogprintf(L"\tcbMapLow: %X\n", lpFaxDev->cbMapLow);
  139. faxlogprintf(L"\thMap: %X\n", lpFaxDev->hMap);
  140. faxlogprintf(L"\tszMap: %s\n", lpFaxDev->szMap);
  141. if (lpFaxDev->lpMap) {
  142. LogWowFaxInfo(lpFaxDev->lpMap);
  143. }
  144. else {
  145. faxlogprintf(L"\tlpMap: %X\n", lpFaxDev->lpMap);
  146. }
  147. faxlogprintf(L"\toffbits: %X\n", lpFaxDev->offbits);
  148. faxlogprintf(L"\thbm: %X\n", lpFaxDev->hbm);
  149. faxlogprintf(L"\tcPixPerByte: %X\n", lpFaxDev->cPixPerByte);
  150. faxlogprintf(L"\tbmFormat: %X\n", lpFaxDev->bmFormat);
  151. faxlogprintf(L"\tbmWidthBytes: %X\n", lpFaxDev->bmWidthBytes);
  152. faxlogprintf(L"\thbmSurf: %X\n", lpFaxDev->hbmSurf);
  153. faxlogprintf(L"\thwnd: %X\n", lpFaxDev->hwnd);
  154. faxlogprintf(L"\ttid: %X\n", lpFaxDev->tid);
  155. faxlogprintf(L"\tlpinfo16: %X\n", lpFaxDev->lpinfo16);
  156. faxlogprintf(L"\thDriver: %X\n", lpFaxDev->hDriver);
  157. faxlogprintf(L"\tStart of gdiinfo: %X\n", (DWORD)&(lpFaxDev->gdiinfo));
  158. faxlogprintf(L"\tStart of devinfo: %X\n", (DWORD)&(lpFaxDev->devinfo));
  159. faxlogprintf(L"\tpdevmode: %X\n", lpFaxDev->pdevmode);
  160. }
  161. #endif
  162. //************************************************************************
  163. // ValidateFaxDev - Validates the FAXDEV structure by checking the DWORD
  164. // signature, which is a known fixed value.
  165. //
  166. //************************************************************************
  167. BOOL ValidateFaxDev(LPFAXDEV lpFaxDev)
  168. {
  169. if (lpFaxDev) {
  170. if (lpFaxDev->id == FAXDEV_ID) {
  171. return TRUE;
  172. }
  173. LOGDEBUG(0, (L"ValidateFaxDev failed, bad id, lpFaxDev: %X\n", lpFaxDev));
  174. }
  175. else {
  176. LOGDEBUG(0, (L"ValidateFaxDev failed, lpFaxDev: NULL\n"));
  177. }
  178. return FALSE;
  179. }
  180. //***************************************************************************
  181. // WFLocalAlloc - Debug version of LocalAlloc.
  182. //***************************************************************************
  183. LPVOID WFLocalAlloc(DWORD dwBytes, LPWSTR lpszWhoCalled)
  184. {
  185. LPVOID lpTmp;
  186. lpTmp = LocalAlloc(LPTR, dwBytes);
  187. if (lpTmp == NULL){
  188. LOGDEBUG(0, (L"WOWFAXUI!%s, failed on memory allocation of %d bytes\n", lpszWhoCalled, dwBytes));
  189. }
  190. return(lpTmp);
  191. }
  192. //***************************************************************************
  193. // FindWowFaxWindow - Put up a message box if you can't.
  194. //***************************************************************************
  195. HWND FindWowFaxWindow(void)
  196. {
  197. HWND hwnd;
  198. PROCESS_INFORMATION ProcessInformation;
  199. STARTUPINFO StartupInfo;
  200. DWORD WaitStatus;
  201. TCHAR szMsg[WOWFAX_MAX_USER_MSG_LEN];
  202. TCHAR szTitle[WOWFAX_MAX_USER_MSG_LEN];
  203. WCHAR szWowExec[] = L"WOWEXEC";
  204. if ((hwnd = FindWindow(WOWFAX_CLASS, NULL)) == NULL) {
  205. // You can't find the WowFaxWindow, try to start WOW.
  206. RtlZeroMemory((PVOID)&StartupInfo, (DWORD)sizeof(StartupInfo));
  207. StartupInfo.cb = sizeof(StartupInfo);
  208. StartupInfo.dwFlags = STARTF_USESHOWWINDOW;
  209. StartupInfo.wShowWindow = SW_NORMAL;
  210. if (CreateProcess(NULL,
  211. szWowExec,
  212. NULL, // security
  213. NULL, // security
  214. FALSE, // inherit handles
  215. CREATE_NEW_CONSOLE | CREATE_DEFAULT_ERROR_MODE,
  216. NULL, // environment strings
  217. NULL, // current directory
  218. &StartupInfo,
  219. &ProcessInformation)) {
  220. WaitForInputIdle(ProcessInformation.hProcess, 120 * 1000);
  221. if ((hwnd = FindWindow(WOWFAX_CLASS, NULL)) != NULL) {
  222. return(hwnd);
  223. }
  224. }
  225. // If WOW failed to start -- let user know.
  226. // -- except during setup mode -- the spooler calls into every printer
  227. // driver to update the registry printer settings if needed.
  228. if(!InSetupMode()) {
  229. if (LoadString(ghInst, WOWFAX_NAME_STR, szTitle, WOWFAX_MAX_USER_MSG_LEN)) {
  230. if (LoadString(ghInst, WOWFAX_NOWOW_STR, szMsg, WOWFAX_MAX_USER_MSG_LEN)) {
  231. MessageBox(hwnd, szMsg, szTitle, MB_OK);
  232. }
  233. }
  234. }
  235. }
  236. return(hwnd);
  237. }
  238. //************************************************************************
  239. // DupTokenW - Helper for Get16BitDriverInfoFromRegistry. Allocate and
  240. // copy a token, wide format. Allocates storage for duplicate.
  241. // wcsdup is not present in the run-times we link to.
  242. //************************************************************************
  243. LPTSTR DupTokenW(LPTSTR lpTok)
  244. {
  245. LPTSTR lpRetVal = NULL;
  246. if (lpTok != NULL) {
  247. lpRetVal = WFLOCALALLOC((wcslen(lpTok) + 1) * sizeof(TCHAR), L"DupTokenW");
  248. if (lpRetVal) {
  249. wcscpy(lpRetVal, lpTok);
  250. }
  251. }
  252. return(lpRetVal);
  253. }
  254. //************************************************************************
  255. // Get16BitDriverInfoFromRegistry - Get the 16-bit driver info (name, port)
  256. // from the registry where it is written by the 16-bit fax driver
  257. // install program using an intercepted WriteProfileString. Storage is
  258. // allocated for the returned info, and must be freed by the caller
  259. // using Free16BitDriverInfo. See also Set16BitDriverInfoToRegistry
  260. // in WOW32FAX.C
  261. //************************************************************************
  262. LPREGFAXDRVINFO16 Get16BitDriverInfoFromRegistry(PWSTR pDeviceName)
  263. {
  264. TCHAR szRetBuf[MAX_PATH];
  265. HKEY hKey = 0;
  266. DWORD dwType, cbBufSize = MAX_PATH;
  267. LPTSTR lpTok, lpDevName;
  268. LPREGFAXDRVINFO16 lpRetVal = WFLOCALALLOC(sizeof(REGFAXDRVINFO16), L"Get16BitDriverInfoFromRegistry");
  269. if ((pDeviceName != NULL) && (lpRetVal != NULL)) {
  270. // Extract the 16-bit device name from pDeviceName.
  271. wcsncpy(szRetBuf, pDeviceName, sizeof(szRetBuf)/sizeof(TCHAR));
  272. szRetBuf[(sizeof(szRetBuf)/sizeof(TCHAR))-1] = '\0';
  273. lpDevName = szRetBuf;
  274. while (lpTok = wcschr(lpDevName, '\\')) {
  275. lpDevName = ++lpTok;
  276. }
  277. if (lpTok = wcschr(lpDevName, ',')) {
  278. *lpTok = '\0';
  279. }
  280. if (lpRetVal->lpDeviceName = DupTokenW(lpDevName)) {
  281. if (RegOpenKeyEx(HKEY_LOCAL_MACHINE,
  282. L"Software\\Microsoft\\Windows NT\\CurrentVersion\\WOW\\WowFax\\devices",
  283. 0, KEY_READ, &hKey ) == ERROR_SUCCESS) {
  284. if (RegQueryValueEx(hKey, lpDevName, 0, &dwType, (LPBYTE)szRetBuf,
  285. &cbBufSize) == ERROR_SUCCESS) {
  286. // Parse registry data into driver name and port name.
  287. // Make wcstok multi-thread safe, it stores state.
  288. EnterCriticalSection(lpCriticalSection);
  289. lpTok = wcstok(szRetBuf, L",");
  290. lpRetVal->lpDriverName = DupTokenW(lpTok);
  291. lpTok = wcstok(NULL, L",");
  292. lpRetVal->lpPortName = DupTokenW(lpTok);
  293. LeaveCriticalSection(lpCriticalSection);
  294. }
  295. RegCloseKey(hKey);
  296. }
  297. }
  298. }
  299. if (lpRetVal && lpRetVal->lpDeviceName &&
  300. lpRetVal->lpDriverName && lpRetVal->lpPortName) {
  301. LOGDEBUG(1, (L"WOWFAXUI!Get16BitDriverInfoFromRegistry, Name: %s, Driver: %s, Port: %s\n", pDeviceName, lpRetVal->lpDriverName, lpRetVal->lpPortName));
  302. return(lpRetVal);
  303. }
  304. LOGDEBUG(0, (L"WOWFAXUI!Get16BitDriverInfoFromRegistry, failed\n"));
  305. Free16BitDriverInfo(lpRetVal);
  306. return(NULL);
  307. }
  308. //************************************************************************
  309. // Free16BitDriverInfo - Free the 16-bit driver info allocated by
  310. // Get16BitDriverInfoFromRegistry.
  311. //************************************************************************
  312. VOID Free16BitDriverInfo(LPREGFAXDRVINFO16 lpRegFaxDrvInfo16)
  313. {
  314. if (lpRegFaxDrvInfo16) {
  315. if (lpRegFaxDrvInfo16->lpDeviceName) {
  316. LocalFree(lpRegFaxDrvInfo16->lpDeviceName);
  317. }
  318. if (lpRegFaxDrvInfo16->lpDriverName) {
  319. LocalFree(lpRegFaxDrvInfo16->lpDriverName);
  320. }
  321. if (lpRegFaxDrvInfo16->lpPortName) {
  322. LocalFree(lpRegFaxDrvInfo16->lpPortName);
  323. }
  324. LocalFree(lpRegFaxDrvInfo16);
  325. return;
  326. }
  327. }
  328. //***************************************************************************
  329. // InterProcCommHandler - Handles inter-process communication between
  330. // WOWFAXUI-WOW32 and WOWFAX-WOW32.
  331. //***************************************************************************
  332. BOOL InterProcCommHandler(LPFAXDEV lpdev, UINT iAction)
  333. {
  334. LPWOWFAXINFO lpT = lpdev->lpMap;
  335. HANDLE hMap = 0;
  336. WNDPROC wndproc;
  337. MSG msg;
  338. switch (iAction)
  339. {
  340. case DRVFAX_SETMAPDATA:
  341. if (lpdev->lpMap) {
  342. // init map struct
  343. lpdev->lpMap->status = FALSE;
  344. lpdev->lpMap->retvalue = (DWORD)-1;
  345. lpdev->hwnd = FindWowFaxWindow();
  346. lpdev->lpMap->hwnd = lpdev->hwnd;
  347. lpdev->lpMap->msg = 0;
  348. lpdev->lpMap->hdc = (WPARAM)lpdev->idMap;
  349. (DWORD)lpdev->lpMap->lpinfo16 = lpdev->lpinfo16;
  350. }
  351. break;
  352. case DRVFAX_SENDTOWOW:
  353. if (lpdev->lpMap->hwnd) {
  354. SendMessage(lpdev->lpMap->hwnd, lpdev->lpMap->msg, (WPARAM)lpdev->idMap, 0);
  355. }
  356. else {
  357. LOGDEBUG(0, (L"WOWFAXUI!InterProcCommHandler, No hwnd to send to.\n"));
  358. }
  359. break;
  360. case DRVFAX_CALLWOW:
  361. if (lpdev->lpMap->hwnd) {
  362. wndproc = (WNDPROC) GetWindowLongA(lpdev->lpMap->hwnd, GWL_WNDPROC);
  363. CallWindowProc(wndproc, lpdev->lpMap->hwnd, lpdev->lpMap->msg, (WPARAM)lpdev->idMap, 0);
  364. }
  365. else {
  366. LOGDEBUG(0, (L"WOWFAXUI!InterProcCommHandler, No hwnd to call to.\n"));
  367. }
  368. break;
  369. case DRVFAX_SENDNOTIFYWOW:
  370. if (lpdev->lpMap->hwnd) {
  371. SendNotifyMessage(lpdev->lpMap->hwnd, lpdev->lpMap->msg, (WPARAM)lpdev->idMap, 0);
  372. // To simulate app-modal dialog, pass message's to this threads
  373. // windows to DefWndProc, until 16-bit fax driver UI is dismissed.
  374. while (!lpdev->lpMap->status) {
  375. if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
  376. // EasyFax ver2.0 & MyTalk ver 2.0
  377. // Also Procomm+ 3 cover sheets. Bug #305665
  378. if ((msg.message == WM_PAINT) ||
  379. (msg.message >= WM_USER) ||
  380. ((msg.message >= WM_DDE_FIRST) &&
  381. (msg.message <= WM_DDE_LAST) )) {
  382. DispatchMessage(&msg);
  383. }
  384. else {
  385. DefWindowProc(msg.hwnd, msg.message, msg.wParam, msg.lParam);
  386. }
  387. }
  388. }
  389. }
  390. break;
  391. case DRVFAX_CREATEMAP:
  392. case DRVFAX_DESTROYMAP:
  393. if (lpdev->lpMap) {
  394. // destroys the current map - for both iActions
  395. UnmapViewOfFile(lpdev->lpMap);
  396. CloseHandle(lpdev->hMap);
  397. lpdev->lpMap = 0;
  398. lpdev->hMap = 0;
  399. lpT = 0;
  400. }
  401. if (iAction == DRVFAX_CREATEMAP) {
  402. // GetFaxDataMapName is WOWFAX_INC_COMMON_CODE in wowfax.h.
  403. GetFaxDataMapName((DWORD)lpdev->idMap, lpdev->szMap);
  404. hMap = CreateFileMapping((HANDLE)-1, NULL, PAGE_READWRITE,
  405. 0, lpdev->cbMapLow, lpdev->szMap);
  406. if (hMap) {
  407. if (GetLastError() == ERROR_ALREADY_EXISTS) {
  408. CloseHandle(hMap);
  409. }
  410. else {
  411. lpT = (LPWOWFAXINFO)MapViewOfFile(hMap, FILE_MAP_WRITE, 0, 0, 0);
  412. if (lpT) {
  413. lpdev->hMap = hMap;
  414. lpdev->lpMap = lpT;
  415. }
  416. else {
  417. LOGDEBUG(0, (L"WOWFAXUI!InterProcCommHandler, MapViewOfFile failed, LastError = %ld\n", GetLastError()));
  418. CloseHandle(hMap);
  419. }
  420. }
  421. }
  422. else {
  423. LOGDEBUG(0, (L"WOWFAXUI!InterProcCommHandler, CreateFileMapping failed, LastError = %ld\n", GetLastError()));
  424. }
  425. }
  426. break;
  427. } //switch
  428. return((BOOL)lpT);
  429. }
  430. //************************************************************************
  431. // InSetupMode: Checks if this process is running during NT setup.
  432. //
  433. // return:
  434. // TRUE if in NT setup
  435. // FALSE otherwise
  436. //************************************************************************
  437. BOOL InSetupMode(void)
  438. {
  439. DWORD SetupMode = FALSE;
  440. LONG results;
  441. DWORD Type, Size = sizeof(DWORD);
  442. HKEY Key;
  443. results = RegOpenKeyW(HKEY_LOCAL_MACHINE, L"System\\Setup", &Key );
  444. if (results != ERROR_SUCCESS) return FALSE;
  445. results = RegQueryValueExW(Key, L"SystemSetupInProgress",
  446. NULL, &Type, (PBYTE)&SetupMode, &Size);
  447. RegCloseKey(Key);
  448. return (!(SetupMode == 0));
  449. }