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.

143 lines
3.8 KiB

  1. #include "priv.h"
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. HINSTANCE g_hinst;
  5. #define APP_VERSION "Version 0.2"
  6. #define PFF_SYSTEM 0x00000001
  7. void PrintSyntax(void)
  8. {
  9. fprintf(stderr, "chinproc.exe " APP_VERSION "\n\n");
  10. fprintf(stderr, "Changes the InProcServer32 entry for any CLSID using\n");
  11. fprintf(stderr, "<dll> from %%SystemRoot%%\\system32 to a local path,\n");
  12. fprintf(stderr, "or vice versa.\n");
  13. fprintf(stderr, "Syntax: chinproc [-s] {<path>|<dll>} \n\n");
  14. fprintf(stderr, " -s Change to %%SystemRoot%%\\system32\\foo.dll\n");
  15. fprintf(stderr, " (NT only)\n\n");
  16. fprintf(stderr, " Default action is to set the InProcServer32 to <path>\n");
  17. }
  18. /*----------------------------------------------------------
  19. Purpose: Worker function to do the work
  20. Returns:
  21. Cond: --
  22. */
  23. int
  24. DoWork(int cArgs, char * rgszArgs[])
  25. {
  26. LPSTR psz;
  27. LPSTR pszDll = NULL;
  28. DWORD dwFlags = 0;
  29. int i;
  30. int nRet = 0;
  31. // (The first arg is actually the exe. Skip that.)
  32. for (i = 1; i < cArgs; i++)
  33. {
  34. psz = rgszArgs[i];
  35. // Check for options
  36. if ('/' == *psz || '-' == *psz)
  37. {
  38. psz++;
  39. switch (*psz)
  40. {
  41. case '?':
  42. // Help
  43. PrintSyntax();
  44. return 0;
  45. case 's':
  46. dwFlags |= PFF_SYSTEM;
  47. *psz++;
  48. // Is this Win95?
  49. if (0x80000000 & GetVersion())
  50. {
  51. // Yes; can't allow -s
  52. fprintf(stderr, "Cannot use -s on Win95 machines.\n");
  53. return -1;
  54. }
  55. break;
  56. default:
  57. // unknown
  58. fprintf(stderr, "Invalid option -%c\n", *psz);
  59. return -1;
  60. }
  61. }
  62. else if (!pszDll)
  63. pszDll = rgszArgs[i];
  64. else
  65. {
  66. fprintf(stderr, "Ignoring invalid parameter - %s\n", rgszArgs[i]);
  67. }
  68. }
  69. if (!pszDll)
  70. {
  71. PrintSyntax();
  72. return -2;
  73. }
  74. // Enumerate the HKCR\CLSID for any CLSIDs that use pszDll.
  75. DWORD dwRet;
  76. HKEY hkeyCLSID;
  77. dwRet = RegOpenKeyEx(HKEY_CLASSES_ROOT, TEXT("CLSID"), 0, KEY_SET_VALUE | KEY_READ, &hkeyCLSID);
  78. if (NO_ERROR == dwRet)
  79. {
  80. DWORD dwIndex = 0;
  81. TCHAR szSubkey[MAX_PATH];
  82. while (ERROR_SUCCESS == RegEnumKey(hkeyCLSID, dwIndex++, szSubkey, SIZECHARS(szSubkey)))
  83. {
  84. TCHAR szPath[MAX_PATH];
  85. DWORD cbPath = sizeof(szPath);
  86. LPTSTR pszFile = PathFindFileName(pszDll);
  87. // Does this entry match pszDll?
  88. PathAppend(szSubkey, TEXT("InProcServer32"));
  89. if (NO_ERROR == SHGetValue(hkeyCLSID, szSubkey, NULL, NULL, szPath, &cbPath) &&
  90. 0 == lstrcmpi(PathFindFileName(szPath), pszFile))
  91. {
  92. // Yes; change it according to dwFlags
  93. fprintf(stdout, ".");
  94. if (dwFlags & PFF_SYSTEM)
  95. {
  96. // Prepend %SystemRoot%\system32 on it
  97. lstrcpy(szPath, TEXT("%SystemRoot%\\system32"));
  98. PathAppend(szPath, pszDll);
  99. SHSetValue(hkeyCLSID, szSubkey, NULL, REG_EXPAND_SZ, szPath, sizeof(szPath));
  100. }
  101. else
  102. {
  103. SHSetValue(hkeyCLSID, szSubkey, NULL, REG_SZ, pszDll, CbFromCch(lstrlen(pszDll) + 1));
  104. }
  105. }
  106. }
  107. fprintf(stdout, "\nFinished!\n");
  108. RegCloseKey(hkeyCLSID);
  109. }
  110. else
  111. fprintf(stderr, "Failed to open HKCR\\CLSID\n");
  112. return nRet;
  113. }
  114. int __cdecl main(int argc, char * argv[])
  115. {
  116. return DoWork(argc, argv);
  117. }