Source code of Windows XP (NT5)
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.

445 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. BOOL
  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. if (lpszCmdLine) {
  94. //
  95. // Get the App name
  96. //
  97. memset(szName, 0, sizeof(szName));
  98. pCur = GetStringIntoBuf(lpszCmdLine, szName, MAX_PATH);
  99. szName[MAX_PATH - 1] = '\0';
  100. //
  101. // Get the CommandLine
  102. //
  103. memset(szWide, 0, sizeof(szWide));
  104. pCur = GetStringIntoBuf(pCur, szWide, MAX_PATH);
  105. szWide[MAX_PATH - 1] = '\0';
  106. //
  107. // Get the bool indicating whether App is default event handler
  108. //
  109. if (pCur) {
  110. sscanf(pCur, "%d", pfSetAsDefault);
  111. } else {
  112. *pfSetAsDefault = FALSE;
  113. }
  114. }
  115. if (MultiByteToWideChar(CP_ACP,
  116. 0,
  117. szName,
  118. -1,
  119. wszName,
  120. MAX_PATH))
  121. {
  122. if (MultiByteToWideChar(CP_ACP,
  123. 0,
  124. szWide,
  125. -1,
  126. wszWide,
  127. MAX_PATH))
  128. {
  129. hr = S_OK;
  130. } else {
  131. hr = E_FAIL;
  132. }
  133. } else {
  134. hr = E_FAIL;
  135. }
  136. return hr;
  137. }
  138. //
  139. // Public functions
  140. //
  141. VOID
  142. EXTERNAL
  143. RegSTIforWiaHelper(HWND hwnd, HINSTANCE hinst, LPSTR lpszCmdLine, int nCmdShow)
  144. {
  145. HRESULT hr = E_FAIL;
  146. WCHAR wszName[MAX_PATH] = {L'\0'};
  147. WCHAR wszWide[MAX_PATH] = {L'\0'};
  148. BOOL fSetAsDefault = 0;
  149. hr = GetEventInfoFromCommandLine(lpszCmdLine, wszName, wszWide, &fSetAsDefault);
  150. if (SUCCEEDED(hr)) {
  151. if (RegisterSTIAppForWIAEvents(wszName, wszWide, fSetAsDefault)) {
  152. #ifdef MAXDEBUG
  153. OutputDebugStringA("* RegisterSTIAppForWIAEvents successful\n");
  154. #endif
  155. }
  156. }
  157. }
  158. BOOL
  159. RegisterSTIAppForWIAEvents(
  160. WCHAR *pszName,
  161. WCHAR *pszWide,
  162. BOOL fSetAsDefault)
  163. /*++
  164. Routine Description:
  165. Arguments:
  166. Return Value:
  167. TRUE - Success
  168. FALSE - Not
  169. --*/
  170. {
  171. HRESULT hr;
  172. IWiaDevMgr *pIDevMgr;
  173. BSTR bstrName;
  174. BSTR bstrDescription;
  175. BSTR bstrIcon;
  176. BSTR bstrDeviceID;
  177. IWiaItem *pIRootItem;
  178. IEnumWIA_DEV_CAPS *pIEnum;
  179. WIA_EVENT_HANDLER wiaHandler;
  180. ULONG ulFetched;
  181. BSTR bstrProgram;
  182. hr = CoInitialize(NULL);
  183. if (FAILED(hr) && hr != RPC_E_CHANGED_MODE) {
  184. // Failed to initialize COM
  185. DebugOutPtszV(DbgFl,TEXT("CoInitializeFailed!!!"));
  186. return hr;
  187. }
  188. hr = CoCreateInstance(
  189. &CLSID_WiaDevMgr,
  190. NULL,
  191. CLSCTX_LOCAL_SERVER,
  192. &IID_IWiaDevMgr,
  193. (void**)&pIDevMgr);
  194. if ( FAILED(hr) || !pIDevMgr ) {
  195. // Failed to get interface
  196. DebugOutPtszV(DbgFl,TEXT("Could not get access to WiaDevMgr interface"));
  197. CoUninitialize();
  198. return FALSE;
  199. }
  200. bstrProgram = SysAllocString(pszWide);
  201. if ( pszName ) {
  202. bstrName = SysAllocString(pszName);
  203. } else {
  204. bstrName = SysAllocString(L"STI");
  205. }
  206. bstrDescription = SysAllocString(bstrName);
  207. bstrIcon = SysAllocString(L"sti.dll,0");
  208. //
  209. // Register a program
  210. //
  211. if ( bstrDescription && bstrIcon && bstrName ) {
  212. hr = IWiaDevMgr_RegisterEventCallbackProgram(
  213. pIDevMgr,
  214. WIA_REGISTER_EVENT_CALLBACK,
  215. NULL,
  216. &WIA_EVENT_STI_PROXY,
  217. bstrProgram,
  218. bstrName,
  219. bstrDescription,
  220. bstrIcon);
  221. } else {
  222. DebugOutPtszV(DbgFl,TEXT("Could not get unicode strings for event registration, out of memory "));
  223. AssertF(FALSE);
  224. }
  225. if ( bstrDescription ) {
  226. SysFreeString(bstrDescription);bstrDescription=NULL;
  227. }
  228. if ( bstrIcon ) {
  229. SysFreeString(bstrIcon);bstrIcon=NULL;
  230. }
  231. if ( bstrName ) {
  232. SysFreeString(bstrName);bstrName=NULL;
  233. }
  234. IWiaDevMgr_Release(pIDevMgr);
  235. CoUninitialize();
  236. return TRUE;
  237. }
  238. VOID
  239. WINAPI
  240. MigrateSTIAppsHelper(
  241. HWND hWnd,
  242. HINSTANCE hInst,
  243. PTSTR pszCommandLine,
  244. INT iParam
  245. )
  246. /*++
  247. Routine Description:
  248. Arguments:
  249. Return Value:
  250. --*/
  251. {
  252. HRESULT hr;
  253. DWORD dwError;
  254. DWORD dwIndex, dwType;
  255. DWORD cbData,cbName;
  256. CHAR szAppCmdLine[MAX_PATH];
  257. CHAR szAppName[MAX_PATH];
  258. WCHAR *pwszNameW = NULL;
  259. WCHAR *pwszAppCmdLineW = NULL;
  260. HKEY hkeySTIApps;
  261. hr = CoInitialize(NULL);
  262. dwError = RegOpenKeyEx(HKEY_LOCAL_MACHINE, // hkey
  263. REGSTR_PATH_REG_APPS, // reg entry string
  264. 0, // dwReserved
  265. KEY_READ, // access
  266. &hkeySTIApps); // pHkeyReturned.
  267. if ( NOERROR == dwError ) {
  268. for ( dwIndex = 0; dwError == ERROR_SUCCESS; dwIndex++ ) {
  269. dwType = 0;
  270. *szAppCmdLine = TEXT('\0');
  271. *szAppName = TEXT('\0');
  272. cbData = sizeof(szAppCmdLine);
  273. cbName = sizeof(szAppName);
  274. dwError = RegEnumValueA(hkeySTIApps,
  275. dwIndex,
  276. szAppName,
  277. &cbName,
  278. NULL,
  279. &dwType,
  280. (LPBYTE)szAppCmdLine,
  281. &cbData);
  282. if ( ((dwType == REG_SZ ) ||(dwType == REG_EXPAND_SZ ))
  283. && *szAppCmdLine ) {
  284. if ( SUCCEEDED(OSUtil_GetWideString(&pwszNameW,szAppName)) &&
  285. SUCCEEDED(OSUtil_GetWideString(&pwszAppCmdLineW,szAppCmdLine))
  286. ) {
  287. RegisterSTIAppForWIAEvents(pwszNameW,pwszAppCmdLineW,FALSE);
  288. }
  289. FreePpv(&pwszNameW);
  290. FreePpv(&pwszAppCmdLineW);
  291. }
  292. }
  293. RegCloseKey(hkeySTIApps);
  294. }
  295. CoUninitialize();
  296. return ;
  297. }
  298. #define RUNDLL_NAME "\\rundll32.exe"
  299. #define RUNDLL_CMD_LINE " sti.dll,RegSTIforWia"
  300. HRESULT RunRegisterProcess(
  301. CHAR *szAppName,
  302. CHAR *szCmdLine)
  303. {
  304. HRESULT hr = E_FAIL;
  305. CHAR szRunDllName[MAX_PATH] = {'\0'};
  306. CHAR szCommandLine[MAX_PATH] = {'\0'};
  307. CHAR szComdLine[MAX_PATH] = {'\0'};
  308. UINT uiCharCount = 0;
  309. STARTUPINFOA startupInfo;
  310. PROCESS_INFORMATION processInfo;
  311. DWORD dwWait = 0;
  312. #ifdef WINNT
  313. uiCharCount = GetSystemDirectoryA(szRunDllName,
  314. MAX_PATH);
  315. #else
  316. uiCharCount = GetWindowsDirectoryA(szRunDllName,
  317. MAX_PATH);
  318. #endif
  319. if ((uiCharCount + lstrlenA(RUNDLL_NAME) + sizeof(CHAR)) >= MAX_PATH ) {
  320. return hr;
  321. }
  322. lstrcatA(szRunDllName, RUNDLL_NAME);
  323. if (szAppName) {
  324. wsprintfA(szCommandLine, "%s \"%s\" \"%s\" %d", RUNDLL_CMD_LINE, szAppName, szCmdLine, 0);
  325. } else {
  326. wsprintfA(szCommandLine, "%s STI \"%s\" %d", RUNDLL_CMD_LINE, szCmdLine, 0);
  327. }
  328. memset(&startupInfo, 0, sizeof(startupInfo));
  329. memset(&processInfo, 0, sizeof(processInfo));
  330. if (CreateProcessA(szRunDllName,
  331. szCommandLine,
  332. NULL,
  333. NULL,
  334. FALSE,
  335. 0,
  336. NULL,
  337. NULL,
  338. &startupInfo,
  339. &processInfo))
  340. {
  341. CloseHandle(processInfo.hThread);
  342. CloseHandle(processInfo.hProcess);
  343. hr = S_OK;
  344. } else {
  345. hr = E_FAIL;
  346. }
  347. return hr;
  348. }