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.

399 lines
9.5 KiB

  1. //
  2. // Generic Windows program template
  3. //
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <stdarg.h>
  7. #include <windows.h>
  8. #include <winddi.h>
  9. #include <tchar.h>
  10. #include <winbase.h>
  11. #include <winreg.h>
  12. CHAR* programName; // program name
  13. HINSTANCE appInstance; // handle to the application instance
  14. LPSTR driverName = "Microsoft Mirror Driver";
  15. LPSTR dispCode[7] = {
  16. "Change Successful",
  17. "Must Restart",
  18. "Bad Flags",
  19. "Bad Parameters",
  20. "Failed",
  21. "Bad Mode",
  22. "Not Updated"};
  23. LPSTR GetDispCode(INT code)
  24. {
  25. switch (code) {
  26. case DISP_CHANGE_SUCCESSFUL: return dispCode[0];
  27. case DISP_CHANGE_RESTART: return dispCode[1];
  28. case DISP_CHANGE_BADFLAGS: return dispCode[2];
  29. case DISP_CHANGE_BADPARAM: return dispCode[3];
  30. case DISP_CHANGE_FAILED: return dispCode[4];
  31. case DISP_CHANGE_BADMODE: return dispCode[5];
  32. case DISP_CHANGE_NOTUPDATED: return dispCode[6];
  33. default:
  34. static char tmp[MAX_PATH];
  35. sprintf(&tmp[0],"Unknown code: %08x\n", code);
  36. return (LPTSTR)&tmp[0];
  37. }
  38. return NULL; // can't happen
  39. }
  40. //
  41. // Handle window repaint event
  42. //
  43. VOID
  44. DoPaint(
  45. HWND hwnd
  46. )
  47. {
  48. HDC hdc;
  49. PAINTSTRUCT ps;
  50. hdc = BeginPaint(hwnd, &ps);
  51. COLORREF red = 0x00FF0000;
  52. HBRUSH hbr = CreateSolidBrush(red);
  53. RECT r;
  54. r.left = ps.rcPaint.left;
  55. r.top = ps.rcPaint.top;
  56. r.right = ps.rcPaint.right;
  57. r.bottom = ps.rcPaint.bottom;
  58. FillRect(hdc, &r, hbr);
  59. EndPaint(hwnd, &ps);
  60. }
  61. //
  62. // Window callback procedure
  63. //
  64. LRESULT CALLBACK
  65. MyWindowProc(
  66. HWND hwnd,
  67. UINT uMsg,
  68. WPARAM wParam,
  69. LPARAM lParam
  70. )
  71. {
  72. switch (uMsg)
  73. {
  74. case WM_PAINT:
  75. DoPaint(hwnd);
  76. break;
  77. case WM_DISPLAYCHANGE:
  78. {
  79. WORD cxScreen = LOWORD(lParam);
  80. WORD cyScreen = HIWORD(lParam);
  81. WPARAM format = wParam;
  82. // Add hook to re-initialize the mirror driver's surface
  83. }
  84. break;
  85. case WM_DESTROY:
  86. PostQuitMessage(0);
  87. break;
  88. default:
  89. return DefWindowProc(hwnd, uMsg, wParam, lParam);
  90. }
  91. return 0;
  92. }
  93. //
  94. // Create main application window
  95. //
  96. VOID
  97. CreateMyWindow(
  98. //********************************************************************
  99. PCSTR title
  100. )
  101. #define MYWNDCLASSNAME "Mirror Sample"
  102. {
  103. //********************************************************************
  104. //
  105. // Register window class if necessary
  106. //
  107. static BOOL wndclassRegistered = FALSE;
  108. if (!wndclassRegistered)
  109. {
  110. WNDCLASS wndClass =
  111. {
  112. 0,
  113. MyWindowProc,
  114. 0,
  115. 0,
  116. appInstance,
  117. NULL,
  118. NULL,
  119. NULL,
  120. NULL,
  121. MYWNDCLASSNAME
  122. };
  123. RegisterClass(&wndClass);
  124. wndclassRegistered = TRUE;
  125. }
  126. HWND hwnd;
  127. INT width = 300, height = 300;
  128. //********************************************************************
  129. hwnd = CreateWindow(
  130. MYWNDCLASSNAME,
  131. title,
  132. WS_OVERLAPPED | WS_SYSMENU | WS_VISIBLE,
  133. CW_USEDEFAULT,
  134. CW_USEDEFAULT,
  135. width,
  136. height,
  137. NULL,
  138. NULL,
  139. appInstance,
  140. NULL);
  141. if (hwnd == NULL)
  142. {
  143. printf("Can't create main window.\n");
  144. exit(-1);
  145. }
  146. }
  147. //
  148. // Main program entrypoint
  149. //
  150. VOID _cdecl
  151. main(
  152. INT argc,
  153. CHAR **argv
  154. )
  155. {
  156. programName = *argv++;
  157. argc--;
  158. appInstance = GetModuleHandle(NULL);
  159. //
  160. // Create the main application window
  161. //
  162. //********************************************************************
  163. DEVMODE devmode;
  164. FillMemory(&devmode, sizeof(DEVMODE), 0);
  165. devmode.dmSize = sizeof(DEVMODE);
  166. devmode.dmDriverExtra = 0;
  167. BOOL change = EnumDisplaySettings(NULL,
  168. ENUM_CURRENT_SETTINGS,
  169. &devmode);
  170. devmode.dmFields = DM_BITSPERPEL |
  171. DM_PELSWIDTH |
  172. DM_PELSHEIGHT;
  173. if (change)
  174. {
  175. // query all display devices in the system until we hit
  176. // our favourate mirrored driver, then extract the device name string
  177. // of the format '\\.\DISPLAY#'
  178. DISPLAY_DEVICE dispDevice;
  179. FillMemory(&dispDevice, sizeof(DISPLAY_DEVICE), 0);
  180. dispDevice.cb = sizeof(DISPLAY_DEVICE);
  181. LPSTR deviceName = NULL;
  182. devmode.dmDeviceName[0] = '\0';
  183. INT devNum = 0;
  184. BOOL result;
  185. while (result = EnumDisplayDevices(NULL,
  186. devNum,
  187. &dispDevice,
  188. 0))
  189. {
  190. if (strcmp(&dispDevice.DeviceString[0], driverName) == 0)
  191. break;
  192. devNum++;
  193. }
  194. if (!result)
  195. {
  196. printf("No '%s' found.\n", driverName);
  197. exit(0);
  198. }
  199. printf("DevNum:%d\nName:%s\nString:%s\nID:%s\nKey:%s\n\n",
  200. devNum,
  201. &dispDevice.DeviceName[0],
  202. &dispDevice.DeviceString[0],
  203. &dispDevice.DeviceID[0],
  204. &dispDevice.DeviceKey[0]);
  205. CHAR deviceNum[MAX_PATH];
  206. LPSTR deviceSub;
  207. // Simply extract 'DEVICE#' from registry key. This will depend
  208. // on how many mirrored devices your driver has and which ones
  209. // you intend to use.
  210. _strupr(&dispDevice.DeviceKey[0]);
  211. deviceSub = strstr(&dispDevice.DeviceKey[0],
  212. "\\DEVICE");
  213. if (!deviceSub)
  214. strcpy(&deviceNum[0], "DEVICE0");
  215. else
  216. strcpy(&deviceNum[0], ++deviceSub);
  217. // Add 'Attach.ToDesktop' setting.
  218. //
  219. HKEY hKeyProfileMirror = (HKEY)0;
  220. if (RegCreateKey(HKEY_LOCAL_MACHINE,
  221. _T("SYSTEM\\CurrentControlSet\\Hardware Profiles\\Current\\System\\CurrentControlSet\\Services\\mirror"),
  222. &hKeyProfileMirror) != ERROR_SUCCESS)
  223. {
  224. printf("Can't access registry.\n");
  225. return;
  226. }
  227. HKEY hKeyDevice = (HKEY)0;
  228. if (RegCreateKey(hKeyProfileMirror,
  229. _T(&deviceNum[0]),
  230. &hKeyDevice) != ERROR_SUCCESS)
  231. {
  232. printf("Can't access DEVICE# hardware profiles key.\n");
  233. return;
  234. }
  235. DWORD one = 1;
  236. if (RegSetValueEx(hKeyDevice,
  237. _T("Attach.ToDesktop"),
  238. 0,
  239. REG_DWORD,
  240. (unsigned char *)&one,
  241. 4) != ERROR_SUCCESS)
  242. {
  243. printf("Can't set Attach.ToDesktop to 0x1\n");
  244. return;
  245. }
  246. strcpy((LPSTR)&devmode.dmDeviceName[0], "mirror");
  247. deviceName = (LPSTR)&dispDevice.DeviceName[0];
  248. // add 'Default.*' settings to the registry under above hKeyProfile\mirror\device
  249. INT code =
  250. ChangeDisplaySettingsEx(deviceName,
  251. &devmode,
  252. NULL,
  253. CDS_UPDATEREGISTRY,
  254. NULL
  255. );
  256. printf("Update Register on device mode: %s\n", GetDispCode(code));
  257. code = ChangeDisplaySettingsEx(deviceName,
  258. &devmode,
  259. NULL,
  260. 0,
  261. NULL
  262. );
  263. printf("Raw dynamic mode change on device mode: %s\n", GetDispCode(code));
  264. HDC hdc = CreateDC("DISPLAY",
  265. deviceName,
  266. NULL,
  267. NULL);
  268. // we should be hooked as layered at this point
  269. HDC hdc2 = CreateCompatibleDC(hdc);
  270. // call DrvCreateDeviceBitmap
  271. HBITMAP hbm = CreateCompatibleBitmap(hdc, 100, 100);
  272. SelectObject(hdc2, hbm);
  273. BitBlt(hdc2, 0, 0, 50, 50, hdc, 0, 0, SRCCOPY);
  274. // delete the device context
  275. DeleteDC(hdc2);
  276. DeleteDC(hdc);
  277. //
  278. // CreateMyWindow("Mirror Sample");
  279. // ^^^^ Use this to test catching window initializiation messages.
  280. //
  281. // Disable attachment to desktop so we aren't attached on
  282. // the next bootup. Our test app is done!
  283. DWORD zero = 0;
  284. if (RegSetValueEx(hKeyDevice,
  285. _T("Attach.ToDesktop"),
  286. 0,
  287. REG_DWORD,
  288. (unsigned char *)&zero,
  289. 4) != ERROR_SUCCESS)
  290. {
  291. printf("Can't set Attach.ToDesktop to 0x0\n");
  292. return;
  293. }
  294. RegCloseKey(hKeyProfileMirror);
  295. RegCloseKey(hKeyDevice);
  296. printf("Performed bit blit. Finished. \n");
  297. return;
  298. }
  299. else
  300. {
  301. printf("Can't get display settings.\n");
  302. }
  303. return;
  304. }