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.

398 lines
10 KiB

  1. #include <stdio.h>
  2. #include <windows.h>
  3. #include <tchar.h>
  4. #include <oleauto.h>
  5. int __cdecl main(int ,char *argv[]);
  6. void ShowHelp(void);
  7. int DoesFileExist(char *input_filespec);
  8. int ProcessFile(char szTheFileName[]);
  9. DWORD CallProcedureInDll(LPCTSTR lpszDLLFile, LPCTSTR lpszProcedureToCall, BOOL bInitOleFlag);
  10. int g_Param_A = FALSE;
  11. int g_Param_B = FALSE;
  12. //-------------------------------------------------------------------
  13. // purpose: main
  14. //-------------------------------------------------------------------
  15. int __cdecl main(int argc,char *argv[])
  16. {
  17. int argno;
  18. int nflags=0;
  19. char szTempFileName1[_MAX_PATH];
  20. char szTempFileName2[_MAX_PATH];
  21. int iLen = 0;
  22. szTempFileName1[0] = '\0';
  23. szTempFileName2[0] = '\0';
  24. // process command line arguments
  25. for(argno=1; argno<argc; argno++)
  26. {
  27. if ( argv[argno][0] == '-' || argv[argno][0] == '/' )
  28. {
  29. nflags++;
  30. switch (argv[argno][1])
  31. {
  32. case 'a':
  33. g_Param_A = TRUE;
  34. break;
  35. case 'b':
  36. g_Param_B = TRUE;
  37. break;
  38. case '?':
  39. goto exit_with_help;
  40. break;
  41. }
  42. } // if switch character found
  43. else
  44. {
  45. if ( *szTempFileName1 == '\0' )
  46. {
  47. strcpy(szTempFileName1, argv[argno]);
  48. }
  49. else
  50. {
  51. if ( *szTempFileName2 == '\0' )
  52. {
  53. strcpy(szTempFileName2, argv[argno]);
  54. }
  55. }
  56. } // non-switch char found
  57. } // for all arguments
  58. // check if filename was specified
  59. if ( *szTempFileName1 == '\0')
  60. {
  61. _tprintf("Too few arguments, argc=%d\n\n",argc);
  62. goto exit_with_help;
  63. }
  64. // Check if the file exists!
  65. if (FALSE == DoesFileExist(szTempFileName1))
  66. {
  67. _tprintf("File '%s', does not exist!\r\n", szTempFileName1);
  68. goto exit_gracefully;
  69. }
  70. if (g_Param_A)
  71. {
  72. if ( *szTempFileName2 == '\0')
  73. {
  74. _tprintf("Calling DllRegisterServer() in %s\r\n",szTempFileName1);
  75. CallProcedureInDll(szTempFileName1,_T("DllRegisterServer"),TRUE);
  76. }
  77. else
  78. {
  79. _tprintf("Calling %s() in %s\r\n", szTempFileName2,szTempFileName1);
  80. CallProcedureInDll(szTempFileName1,szTempFileName2,TRUE);
  81. }
  82. }
  83. else
  84. {
  85. // run the function to do everything
  86. ProcessFile(szTempFileName1);
  87. }
  88. exit_gracefully:
  89. _tprintf("Done.\n");
  90. return TRUE;
  91. exit_with_help:
  92. ShowHelp();
  93. return FALSE;
  94. }
  95. //-------------------------------------------------------------------
  96. // purpose:
  97. //-------------------------------------------------------------------
  98. int ProcessFile(char szTheFileName[])
  99. {
  100. HINSTANCE hLibHandle;
  101. if ( ( hLibHandle = LoadLibrary( szTheFileName ) ) != NULL )
  102. {
  103. _tprintf("LoadLibarary Successfull.\n");
  104. {
  105. FreeLibrary(hLibHandle);
  106. return TRUE;
  107. }
  108. }
  109. else
  110. {
  111. _tprintf("LoadLibarary Failed.\n");
  112. return FALSE;
  113. }
  114. }
  115. //-------------------------------------------------------------------
  116. // purpose:
  117. //-------------------------------------------------------------------
  118. void ShowHelp()
  119. {
  120. _tprintf("\n");
  121. _tprintf("LoadLib - does a loadlibrary call on the specified file.\n");
  122. _tprintf("Usage: LoadLib <input file> \n");
  123. return;
  124. }
  125. //-------------------------------------------------------------------
  126. // purpose:
  127. //-------------------------------------------------------------------
  128. int DoesFileExist(char *input_filespec)
  129. {
  130. if (GetFileAttributes(input_filespec) == -1) {return(FALSE);}
  131. return (TRUE);
  132. }
  133. BOOL IsFileExist(LPCTSTR szFile)
  134. {
  135. // Check if the file has expandable Environment strings
  136. LPTSTR pch = NULL;
  137. pch = _tcschr( (LPTSTR) szFile, _T('%'));
  138. if (pch)
  139. {
  140. TCHAR szValue[_MAX_PATH];
  141. _tcscpy(szValue,szFile);
  142. if (!ExpandEnvironmentStrings( (LPCTSTR)szFile, szValue, sizeof(szValue)/sizeof(TCHAR)))
  143. {_tcscpy(szValue,szFile);}
  144. return (GetFileAttributes(szValue) != 0xFFFFFFFF);
  145. }
  146. else
  147. {
  148. return (GetFileAttributes(szFile) != 0xFFFFFFFF);
  149. }
  150. }
  151. int iOleInitialize(void)
  152. {
  153. int iBalanceOLE = FALSE;
  154. HRESULT hInitRes = NULL;
  155. hInitRes = OleInitialize(NULL);
  156. if ( SUCCEEDED(hInitRes) || hInitRes == RPC_E_CHANGED_MODE )
  157. {
  158. if ( SUCCEEDED(hInitRes))
  159. {
  160. iBalanceOLE = TRUE;
  161. }
  162. else
  163. {
  164. _tprintf("iOleInitialize failed 1\n");
  165. }
  166. }
  167. else
  168. {
  169. _tprintf("iOleInitialize failed 2\n");
  170. }
  171. return iBalanceOLE;
  172. }
  173. void iOleUnInitialize(int iBalanceOLE)
  174. {
  175. if (iBalanceOLE)
  176. {
  177. OleUninitialize();
  178. }
  179. return;
  180. }
  181. void InetGetFilePath(LPCTSTR szFile, LPTSTR szPath)
  182. {
  183. // if UNC name \\computer\share\local1\local2
  184. if (*szFile == _T('\\') && *(_tcsinc(szFile)) == _T('\\')) {
  185. TCHAR szTemp[_MAX_PATH], szLocal[_MAX_PATH];
  186. TCHAR *p = NULL;
  187. int i = 0;
  188. _tcscpy(szTemp, szFile);
  189. p = szTemp;
  190. while (*p) {
  191. if (*p == _T('\\'))
  192. i++;
  193. if (i == 4) {
  194. *p = _T('\0');
  195. p = _tcsinc(p); // p is now pointing at local1\local2
  196. break;
  197. }
  198. p = _tcsinc(p);
  199. }
  200. _tcscpy(szPath, szTemp); // now szPath contains \\computer\share
  201. if (i == 4 && *p) { // p is pointing the local path now
  202. _tcscpy(szLocal, p);
  203. p = _tcsrchr(szLocal, _T('\\'));
  204. if (p)
  205. *p = _T('\0');
  206. _tcscat(szPath, _T("\\"));
  207. _tcscat(szPath, szLocal); // szPath contains \\computer\share\local1
  208. }
  209. } else { // NOT UNC name
  210. TCHAR *p;
  211. if (GetFullPathName(szFile, _MAX_PATH, szPath, &p)) {
  212. p = _tcsrchr(szPath, _T('\\'));
  213. if (p)
  214. {
  215. TCHAR *p2 = NULL;
  216. p2 = _tcsdec(szPath, p);
  217. if (p2)
  218. {
  219. if (*p2 == _T(':') )
  220. {p = _tcsinc(p);}
  221. }
  222. *p = _T('\0');
  223. }
  224. } else {
  225. _tprintf("InetGetFilePath failed\n");
  226. }
  227. }
  228. return;
  229. }
  230. typedef HRESULT (CALLBACK *HCRET)(void);
  231. DWORD CallProcedureInDll(LPCTSTR lpszDLLFile, LPCTSTR lpszProcedureToCall, BOOL bInitOleFlag)
  232. {
  233. TCHAR szErrorString[100];
  234. DWORD dwReturn = ERROR_SUCCESS;
  235. HINSTANCE hDll = NULL;
  236. // Diferent function prototypes...
  237. HCRET hProc = NULL;
  238. int iTempProcGood = FALSE;
  239. HRESULT hRes = 0;
  240. BOOL bBalanceOLE = FALSE;
  241. HRESULT hInitRes = NULL;
  242. int err = NOERROR;
  243. // Variables to changing and saving dirs
  244. TCHAR szDirName[_MAX_PATH], szFilePath[_MAX_PATH];
  245. // Variable to set error string
  246. TCHAR szErrString[256];
  247. _tcscpy(szDirName, _T(""));
  248. _tcscpy(szErrString, _T(""));
  249. _tprintf("CallProcedureInDll start\n");
  250. // If we need to initialize the ole library then init it.
  251. if (bInitOleFlag)
  252. {
  253. bBalanceOLE = iOleInitialize();
  254. if (FALSE == bBalanceOLE)
  255. {
  256. hInitRes = OleInitialize(NULL);
  257. // Ole Failed.
  258. dwReturn = hInitRes;
  259. SetLastError(dwReturn);
  260. _tprintf("CallProcedureInDll failed 1\n");
  261. goto CallProcedureInDll_Exit;
  262. }
  263. }
  264. // Check if the file exists
  265. if (!IsFileExist(lpszDLLFile))
  266. {
  267. dwReturn = ERROR_FILE_NOT_FOUND;
  268. _tprintf("CallProcedureInDll failed 2\n");
  269. SetLastError(dwReturn);
  270. goto CallProcedureInDll_Exit;
  271. }
  272. // Change Directory
  273. GetCurrentDirectory( _MAX_PATH, szDirName );
  274. InetGetFilePath(lpszDLLFile, szFilePath);
  275. // Change to The Drive.
  276. if (SetCurrentDirectory(szFilePath) == 0) {}
  277. // Try to load the module,dll,ocx.
  278. hDll = LoadLibraryEx(lpszDLLFile, NULL, LOAD_WITH_ALTERED_SEARCH_PATH );
  279. if (!hDll)
  280. {
  281. // Failed to load library, Probably because some .dll file is missing.
  282. // Show the error message.
  283. dwReturn = TYPE_E_CANTLOADLIBRARY;
  284. _tprintf("CallProcedureInDll failed 3\n");
  285. SetLastError(dwReturn);
  286. goto CallProcedureInDll_Exit;
  287. }
  288. // Ok module was successfully loaded. now let's try to get the Address of the Procedure
  289. // Convert the function name to ascii before passing it to GetProcAddress()
  290. char AsciiProcedureName[255];
  291. #if defined(UNICODE) || defined(_UNICODE)
  292. // convert to ascii
  293. WideCharToMultiByte( CP_ACP, 0, (TCHAR *)lpszProcedureToCall, -1, AsciiProcedureName, 255, NULL, NULL );
  294. #else
  295. // the is already ascii so just copy
  296. strcpy(AsciiProcedureName, lpszProcedureToCall);
  297. #endif
  298. iTempProcGood = TRUE;
  299. hProc = (HCRET)GetProcAddress(hDll, AsciiProcedureName);
  300. if (!hProc){iTempProcGood = FALSE;}
  301. if (!iTempProcGood)
  302. {
  303. // failed to load,find or whatever this function.
  304. dwReturn = ERROR_PROC_NOT_FOUND;
  305. _tprintf("CallProcedureInDll failed 4\n");
  306. SetLastError(dwReturn);
  307. goto CallProcedureInDll_Exit;
  308. }
  309. // Call the function that we got the handle to
  310. __try
  311. {
  312. hRes = (*hProc)();
  313. }
  314. __except(EXCEPTION_EXECUTE_HANDLER)
  315. {
  316. _tprintf(szErrorString, _T("\r\n\r\nException Caught in CallProcedureInDll(). GetExceptionCode()=0x%x.\r\n\r\n"), GetExceptionCode());
  317. _tprintf(szErrorString);
  318. }
  319. if (FAILED(hRes))
  320. {
  321. dwReturn = E_FAIL;
  322. _tprintf("CallProcedureInDll failed 5\n");
  323. // this function returns E_FAIL but
  324. // the actual error is in GetLastError()
  325. // set the last error to whatever was returned from the function call
  326. SetLastError(hRes);
  327. }
  328. else
  329. {
  330. }
  331. CallProcedureInDll_Exit:
  332. if (hDll)
  333. {
  334. FreeLibrary(hDll);
  335. }
  336. else
  337. {
  338. }
  339. if (_tcscmp(szDirName, _T("")) != 0){SetCurrentDirectory(szDirName);}
  340. // To close the library gracefully, each successful call to OleInitialize,
  341. // including those that return S_FALSE, must be balanced by a corresponding
  342. // call to the OleUninitialize function.
  343. iOleUnInitialize(bBalanceOLE);
  344. return dwReturn;
  345. }