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.

262 lines
5.4 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (C) Microsoft Corporation, 1995 - 1999
  6. //
  7. // File: certwrap.cpp
  8. //
  9. // Contents: Wrap a command line and expand envronment variables.
  10. //
  11. //--------------------------------------------------------------------------
  12. #include <windows.h>
  13. #include <stdlib.h>
  14. #include "resource.h"
  15. #define WM_WRAPCOMMAND WM_USER+0
  16. WCHAR wszAppName[] = L"CertWrap";
  17. HINSTANCE hInstApp;
  18. #define ARRAYLEN(a) (sizeof(a)/sizeof((a)[0]))
  19. VOID
  20. WrapCommand(
  21. HWND hWnd,
  22. WCHAR const *pwszCommand)
  23. {
  24. WCHAR awc[4096];
  25. WCHAR awcVar[128];
  26. WCHAR const *pwszSrc;
  27. WCHAR *pwszDst;
  28. WCHAR *pwszVar;
  29. BOOL fTooLong = FALSE;
  30. BOOL fVarTooLong = FALSE;
  31. BOOL fVarNotFound = FALSE;
  32. DWORD cwc;
  33. STARTUPINFO si;
  34. PROCESS_INFORMATION pi;
  35. BOOL b;
  36. pwszSrc = pwszCommand;
  37. pwszDst = awc;
  38. while (L'\0' != (*pwszDst = *pwszSrc++))
  39. {
  40. if ('%' == *pwszDst)
  41. {
  42. *pwszDst = L'\0';
  43. pwszVar = awcVar;
  44. while (L'\0' != *pwszSrc)
  45. {
  46. if (L'%' == *pwszSrc)
  47. {
  48. pwszSrc++;
  49. break;
  50. }
  51. *pwszVar++ = *pwszSrc++;
  52. if (pwszVar >= &awcVar[ARRAYLEN(awcVar) - 1])
  53. {
  54. fVarTooLong = TRUE;
  55. goto error;
  56. }
  57. }
  58. *pwszVar = L'\0';
  59. cwc = GetEnvironmentVariable(
  60. awcVar,
  61. pwszDst,
  62. (DWORD) (ULONG_PTR) (&awcVar[ARRAYLEN(awcVar)] - pwszDst));
  63. if (0 == cwc)
  64. {
  65. fVarNotFound = TRUE;
  66. goto error;
  67. }
  68. if ((DWORD) (ULONG_PTR) (&awcVar[ARRAYLEN(awcVar)] - pwszDst) <= cwc)
  69. {
  70. fTooLong = TRUE;
  71. goto error;
  72. }
  73. pwszDst += cwc;
  74. }
  75. else
  76. {
  77. pwszDst++;
  78. }
  79. if (pwszDst >= &awc[ARRAYLEN(awc)])
  80. {
  81. fTooLong = TRUE;
  82. goto error;
  83. }
  84. }
  85. error:
  86. if (fVarNotFound)
  87. {
  88. MessageBox(
  89. hWnd,
  90. L"Environment Variable Not Found",
  91. awcVar,
  92. MB_OK | MB_ICONERROR | MB_SETFOREGROUND);
  93. }
  94. if (fVarTooLong)
  95. {
  96. MessageBox(
  97. hWnd,
  98. L"Environment Variable Name Too Long",
  99. L"CertWrapper",
  100. MB_OK | MB_ICONERROR | MB_SETFOREGROUND);
  101. }
  102. if (fTooLong)
  103. {
  104. MessageBox(
  105. hWnd,
  106. L"Command Line Too Long",
  107. L"CertWrapper",
  108. MB_OK | MB_ICONERROR | MB_SETFOREGROUND);
  109. }
  110. MessageBox(hWnd, awc, L"CertWrapper", MB_OK);
  111. ZeroMemory(&si, sizeof(si));
  112. si.cb = sizeof(si);
  113. si.dwFlags = STARTF_USESHOWWINDOW;
  114. si.wShowWindow = SW_SHOW;
  115. b = CreateProcess(
  116. NULL,
  117. awc,
  118. NULL,
  119. NULL,
  120. FALSE,
  121. 0,
  122. NULL,
  123. NULL, // lpCurrentDirectory
  124. &si,
  125. &pi);
  126. if (!b)
  127. {
  128. DWORD err;
  129. WCHAR awcErr[MAX_PATH];
  130. err = GetLastError();
  131. wsprintf(awcErr, L"CreateProcess failed: %d(%x)", err, err);
  132. MessageBox(
  133. hWnd,
  134. awcErr,
  135. L"CertWrapper",
  136. MB_OK | MB_ICONERROR | MB_SETFOREGROUND);
  137. }
  138. }
  139. //**************************************************************************
  140. // FUNCTION: MainWndProc(...)
  141. // ARGUMENTS:
  142. //**************************************************************************
  143. LRESULT APIENTRY
  144. MainWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
  145. {
  146. switch (msg)
  147. {
  148. case WM_CREATE:
  149. return(0);
  150. case WM_SIZE:
  151. return(0);
  152. case WM_DESTROY:
  153. PostQuitMessage(0);
  154. break;
  155. case WM_WRAPCOMMAND:
  156. WrapCommand(hWnd, (WCHAR const *) lParam);
  157. PostQuitMessage(0);
  158. break;
  159. default:
  160. return(DefWindowProc(hWnd, msg, wParam, lParam));
  161. }
  162. return(0);
  163. }
  164. //+------------------------------------------------------------------------
  165. //
  166. // Function: WinMain()
  167. //
  168. // Synopsis: Entry Point
  169. //
  170. // Arguments: [hInstance] -- Instance handle
  171. // [hPrevInstance] -- Obsolete
  172. // [lpCmdLine] -- App command line
  173. // [nCmdShow] -- Starting show state
  174. //-------------------------------------------------------------------------
  175. extern "C" int APIENTRY
  176. wWinMain(
  177. HINSTANCE hInstance,
  178. HINSTANCE hPrevInstance,
  179. LPWSTR pwszCmdLine,
  180. int nCmdShow)
  181. {
  182. MSG msg;
  183. WNDCLASS wcApp;
  184. HWND hWndMain;
  185. // Save the current instance
  186. hInstApp = hInstance;
  187. // Set up the application's window class
  188. wcApp.style = 0;
  189. wcApp.lpfnWndProc = MainWndProc;
  190. wcApp.cbClsExtra = 0;
  191. wcApp.cbWndExtra = 0;
  192. wcApp.hInstance = hInstance;
  193. wcApp.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  194. wcApp.hCursor = LoadCursor(NULL, IDC_ARROW);
  195. wcApp.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
  196. wcApp.lpszMenuName = NULL;
  197. wcApp.lpszClassName = wszAppName;
  198. if (!RegisterClass(&wcApp))
  199. {
  200. return(FALSE);
  201. }
  202. // Create Main Window
  203. hWndMain = CreateWindow(wszAppName,
  204. L"CertWrapper Application",
  205. WS_OVERLAPPEDWINDOW,
  206. CW_USEDEFAULT, CW_USEDEFAULT,
  207. CW_USEDEFAULT, CW_USEDEFAULT,
  208. NULL,
  209. NULL,
  210. hInstance,
  211. NULL);
  212. if (NULL == hWndMain)
  213. {
  214. return(FALSE);
  215. }
  216. // Make window visible
  217. // ShowWindow(hWndMain,nCmdShow);
  218. // Update window client area
  219. UpdateWindow(hWndMain);
  220. // Send off the message to get things started
  221. PostMessage(hWndMain, WM_WRAPCOMMAND, 0, (LPARAM) pwszCmdLine);
  222. // Message Loop
  223. while (GetMessage(&msg, NULL, 0, 0))
  224. {
  225. TranslateMessage(&msg);
  226. DispatchMessage(&msg);
  227. }
  228. return (int)(msg.wParam);
  229. }