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.

462 lines
11 KiB

  1. /*++
  2. Copyright (c) 1997-1998 Microsoft Corporation
  3. Module Name:
  4. wiaevent.c
  5. Abstract:
  6. Implementation of STI registration logic over WIA event interface
  7. Notes:
  8. Author:
  9. Vlad Sadovsky (VladS) 11/24/1999
  10. Environment:
  11. User Mode - Win32
  12. Revision History:
  13. 11/24/1999 VladS Created
  14. --*/
  15. //
  16. // Include files
  17. //
  18. /*
  19. #define COBJMACROS
  20. #include "wia.h"
  21. #include "wia.h"
  22. #include <stiregi.h>
  23. #include <sti.h>
  24. #include <stierr.h>
  25. #include <stiusd.h>
  26. #include "stipriv.h"
  27. #include "debug.h"
  28. */
  29. #include "sticomm.h"
  30. #define DbgFl DbgFlDevice
  31. //
  32. // Helper functions
  33. //
  34. CHAR* GetStringIntoBuf(CHAR* pInputString, CHAR* pBuf, DWORD dwBufSize)
  35. {
  36. CHAR *pCur = pInputString;
  37. CHAR EndChar = ' ';
  38. CHAR *pEndPos = NULL;
  39. ULONG ulIndex = 0;
  40. if (pInputString) {
  41. pEndPos = pInputString + lstrlenA(pInputString);
  42. //
  43. // Eat leading white spaces
  44. //
  45. while ((*pCur == ' ') && (pCur < pEndPos)) {
  46. pCur++;
  47. }
  48. //
  49. // Look for string delimiter. Default is space, but check for quote.
  50. //
  51. if (*pCur == '"') {
  52. EndChar = '"';
  53. if (pCur < pEndPos) {
  54. pCur++;
  55. }
  56. }
  57. while ((*pCur != EndChar) && (pCur < pEndPos)) {
  58. pBuf[ulIndex] = *pCur;
  59. if (ulIndex >= dwBufSize) {
  60. break;
  61. }
  62. ulIndex++;
  63. pCur++;
  64. }
  65. if (pCur < pEndPos) {
  66. pCur++;
  67. }
  68. }
  69. return pCur;
  70. }
  71. HRESULT
  72. GetEventInfoFromCommandLine(
  73. LPSTR lpszCmdLine,
  74. WCHAR *wszName,
  75. WCHAR *wszWide,
  76. BOOL *pfSetAsDefault
  77. )
  78. /*++
  79. Routine Description:
  80. Helper function to parse command line
  81. Arguments:
  82. pszWide Command line to be launched
  83. fSetAsDefault Set as default registered application callback
  84. Return Value:
  85. Status
  86. --*/
  87. {
  88. HRESULT hr = E_FAIL;
  89. CHAR szName[MAX_PATH] = {'\0'};
  90. CHAR szWide[MAX_PATH] = {'\0'};
  91. CHAR szOut[MAX_PATH] = {'\0'};
  92. CHAR *pCur = NULL;
  93. int iReturn = 0;
  94. if (lpszCmdLine) {
  95. //
  96. // Get the App name
  97. //
  98. memset(szName, 0, sizeof(szName));
  99. pCur = GetStringIntoBuf(lpszCmdLine, szName, MAX_PATH);
  100. szName[MAX_PATH - 1] = '\0';
  101. //
  102. // Get the CommandLine
  103. //
  104. memset(szWide, 0, sizeof(szWide));
  105. pCur = GetStringIntoBuf(pCur, szWide, MAX_PATH);
  106. szWide[MAX_PATH - 1] = '\0';
  107. //
  108. // Get the bool indicating whether App is default event handler
  109. //
  110. if (pCur) {
  111. iReturn = sscanf(pCur, "%d", pfSetAsDefault);
  112. if (iReturn == 0) {
  113. if (pfSetAsDefault)
  114. {
  115. *pfSetAsDefault = FALSE;
  116. }
  117. }
  118. } else {
  119. if (pfSetAsDefault)
  120. {
  121. *pfSetAsDefault = FALSE;
  122. }
  123. }
  124. }
  125. if (MultiByteToWideChar(CP_ACP,
  126. 0,
  127. szName,
  128. -1,
  129. wszName,
  130. MAX_PATH))
  131. {
  132. if (MultiByteToWideChar(CP_ACP,
  133. 0,
  134. szWide,
  135. -1,
  136. wszWide,
  137. MAX_PATH))
  138. {
  139. hr = S_OK;
  140. } else {
  141. hr = E_FAIL;
  142. }
  143. } else {
  144. hr = E_FAIL;
  145. }
  146. return hr;
  147. }
  148. //
  149. // Public functions
  150. //
  151. VOID
  152. EXTERNAL
  153. RegSTIforWiaHelper(HWND hwnd, HINSTANCE hinst, LPSTR lpszCmdLine, int nCmdShow)
  154. {
  155. HRESULT hr = E_FAIL;
  156. WCHAR wszName[MAX_PATH] = {L'\0'};
  157. WCHAR wszWide[MAX_PATH] = {L'\0'};
  158. BOOL fSetAsDefault = 0;
  159. hr = GetEventInfoFromCommandLine(lpszCmdLine, wszName, wszWide, &fSetAsDefault);
  160. if (SUCCEEDED(hr)) {
  161. if (RegisterSTIAppForWIAEvents(wszName, wszWide, fSetAsDefault)) {
  162. #ifdef MAXDEBUG
  163. OutputDebugStringA("* RegisterSTIAppForWIAEvents successful\n");
  164. #endif
  165. }
  166. }
  167. }
  168. BOOL
  169. RegisterSTIAppForWIAEvents(
  170. WCHAR *pszName,
  171. WCHAR *pszWide,
  172. BOOL fSetAsDefault)
  173. /*++
  174. Routine Description:
  175. Arguments:
  176. Return Value:
  177. TRUE - Success
  178. FALSE - Not
  179. --*/
  180. {
  181. HRESULT hr;
  182. IWiaDevMgr *pIDevMgr;
  183. BSTR bstrName;
  184. BSTR bstrDescription;
  185. BSTR bstrIcon;
  186. BSTR bstrDeviceID;
  187. IWiaItem *pIRootItem;
  188. IEnumWIA_DEV_CAPS *pIEnum;
  189. WIA_EVENT_HANDLER wiaHandler;
  190. ULONG ulFetched;
  191. BSTR bstrProgram;
  192. hr = CoInitialize(NULL);
  193. if (FAILED(hr) && hr != RPC_E_CHANGED_MODE) {
  194. // Failed to initialize COM
  195. DebugOutPtszV(DbgFl,TEXT("CoInitializeFailed!!!"));
  196. return hr;
  197. }
  198. hr = CoCreateInstance(
  199. &CLSID_WiaDevMgr,
  200. NULL,
  201. CLSCTX_LOCAL_SERVER,
  202. &IID_IWiaDevMgr,
  203. (void**)&pIDevMgr);
  204. if ( FAILED(hr) || !pIDevMgr ) {
  205. // Failed to get interface
  206. DebugOutPtszV(DbgFl,TEXT("Could not get access to WiaDevMgr interface"));
  207. CoUninitialize();
  208. return FALSE;
  209. }
  210. bstrProgram = SysAllocString(pszWide);
  211. if ( pszName ) {
  212. bstrName = SysAllocString(pszName);
  213. } else {
  214. bstrName = SysAllocString(L"STI");
  215. }
  216. bstrDescription = SysAllocString(bstrName);
  217. bstrIcon = SysAllocString(L"sti.dll,0");
  218. //
  219. // Register a program
  220. //
  221. if ( bstrDescription && bstrIcon && bstrName ) {
  222. hr = IWiaDevMgr_RegisterEventCallbackProgram(
  223. pIDevMgr,
  224. WIA_REGISTER_EVENT_CALLBACK,
  225. NULL,
  226. &WIA_EVENT_STI_PROXY,
  227. bstrProgram,
  228. bstrName,
  229. bstrDescription,
  230. bstrIcon);
  231. } else {
  232. DebugOutPtszV(DbgFl,TEXT("Could not get unicode strings for event registration, out of memory "));
  233. AssertF(FALSE);
  234. }
  235. if ( bstrDescription ) {
  236. SysFreeString(bstrDescription);bstrDescription=NULL;
  237. }
  238. if ( bstrIcon ) {
  239. SysFreeString(bstrIcon);bstrIcon=NULL;
  240. }
  241. if ( bstrName ) {
  242. SysFreeString(bstrName);bstrName=NULL;
  243. }
  244. IWiaDevMgr_Release(pIDevMgr);
  245. CoUninitialize();
  246. return TRUE;
  247. }
  248. VOID
  249. WINAPI
  250. MigrateSTIAppsHelper(
  251. HWND hWnd,
  252. HINSTANCE hInst,
  253. PTSTR pszCommandLine,
  254. INT iParam
  255. )
  256. /*++
  257. Routine Description:
  258. Arguments:
  259. Return Value:
  260. --*/
  261. {
  262. HRESULT hr;
  263. DWORD dwError;
  264. DWORD dwIndex, dwType;
  265. DWORD cbData,cbName;
  266. CHAR szAppCmdLine[MAX_PATH];
  267. CHAR szAppName[MAX_PATH];
  268. WCHAR *pwszNameW = NULL;
  269. WCHAR *pwszAppCmdLineW = NULL;
  270. HKEY hkeySTIApps;
  271. hr = CoInitialize(NULL);
  272. dwError = RegOpenKeyEx(HKEY_LOCAL_MACHINE, // hkey
  273. REGSTR_PATH_REG_APPS, // reg entry string
  274. 0, // dwReserved
  275. KEY_READ, // access
  276. &hkeySTIApps); // pHkeyReturned.
  277. if ( NOERROR == dwError ) {
  278. for ( dwIndex = 0; dwError == ERROR_SUCCESS; dwIndex++ ) {
  279. dwType = 0;
  280. *szAppCmdLine = TEXT('\0');
  281. *szAppName = TEXT('\0');
  282. cbData = sizeof(szAppCmdLine);
  283. cbName = sizeof(szAppName);
  284. dwError = RegEnumValueA(hkeySTIApps,
  285. dwIndex,
  286. szAppName,
  287. &cbName,
  288. NULL,
  289. &dwType,
  290. (LPBYTE)szAppCmdLine,
  291. &cbData);
  292. if ( ((dwType == REG_SZ ) ||(dwType == REG_EXPAND_SZ ))
  293. && *szAppCmdLine ) {
  294. if ( SUCCEEDED(OSUtil_GetWideString(&pwszNameW,szAppName)) &&
  295. SUCCEEDED(OSUtil_GetWideString(&pwszAppCmdLineW,szAppCmdLine))
  296. ) {
  297. RegisterSTIAppForWIAEvents(pwszNameW,pwszAppCmdLineW,FALSE);
  298. }
  299. FreePpv(&pwszNameW);
  300. FreePpv(&pwszAppCmdLineW);
  301. }
  302. }
  303. RegCloseKey(hkeySTIApps);
  304. }
  305. CoUninitialize();
  306. return ;
  307. }
  308. #define RUNDLL_NAME "\\rundll32.exe"
  309. #define RUNDLL_CMD_LINE " sti.dll,RegSTIforWia"
  310. HRESULT RunRegisterProcess(
  311. CHAR *szAppName,
  312. CHAR *szCmdLine)
  313. {
  314. HRESULT hr = E_FAIL;
  315. CHAR szRunDllName[MAX_PATH] = {'\0'};
  316. CHAR szCommandLine[MAX_PATH * 2] = {'\0'};
  317. //CHAR szComdLine[MAX_PATH] = {'\0'};
  318. UINT uiCharCount = 0;
  319. STARTUPINFOA startupInfo;
  320. PROCESS_INFORMATION processInfo;
  321. DWORD dwWait = 0;
  322. #ifdef WINNT
  323. uiCharCount = GetSystemDirectoryA(szRunDllName,
  324. MAX_PATH);
  325. #else
  326. uiCharCount = GetWindowsDirectoryA(szRunDllName,
  327. MAX_PATH);
  328. #endif
  329. if ((uiCharCount + lstrlenA(RUNDLL_NAME) + sizeof(CHAR)) >= MAX_PATH ) {
  330. return hr;
  331. }
  332. lstrcatA(szRunDllName, RUNDLL_NAME);
  333. if (szAppName) {
  334. _snprintf(szCommandLine,
  335. sizeof(szCommandLine) - 1,
  336. "%s \"%s\" \"%s\" %d", RUNDLL_CMD_LINE, szAppName, szCmdLine, 0);
  337. } else {
  338. _snprintf(szCommandLine,
  339. sizeof(szCommandLine) - 1,
  340. "%s STI \"%s\" %d", RUNDLL_CMD_LINE, szCmdLine, 0);
  341. }
  342. memset(&startupInfo, 0, sizeof(startupInfo));
  343. memset(&processInfo, 0, sizeof(processInfo));
  344. if (CreateProcessA(szRunDllName,
  345. szCommandLine,
  346. NULL,
  347. NULL,
  348. FALSE,
  349. 0,
  350. NULL,
  351. NULL,
  352. &startupInfo,
  353. &processInfo))
  354. {
  355. CloseHandle(processInfo.hThread);
  356. CloseHandle(processInfo.hProcess);
  357. hr = S_OK;
  358. } else {
  359. hr = E_FAIL;
  360. }
  361. return hr;
  362. }