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.

356 lines
9.8 KiB

  1. #include "pch.cxx"
  2. #include <ole2.h>
  3. #include <stdio.h>
  4. #include <tchar.h>
  5. #include <pstgserv.hxx>
  6. CPropertyStorageServerApp cPropStgServerApp;
  7. const TCHAR tszCLSIDInterface[] = "{af4ae0d0-a37f-11cf-8d73-00aa004cd01a}";
  8. const TCHAR tszCLSIDApp[] = "{af4ae0d1-a37f-11cf-8d73-00aa004cd01a}";
  9. void
  10. SelfRegistration( HINSTANCE hinst )
  11. {
  12. LONG lRet;
  13. DWORD dwDisposition;
  14. HKEY hkeyBase = NULL;
  15. HKEY hkey = NULL;
  16. LPTSTR tszError = NULL;
  17. LPTSTR tszServerName = TEXT("PropTest Local Server");
  18. LPTSTR tszProxyStubName = TEXT("PropTest Local Server Proxy/Stub");
  19. TCHAR tszModulePathAndName[ MAX_PATH + 1 ];
  20. TCHAR tszKeyName[ MAX_PATH + 1 ];
  21. LPTSTR tszNumMethods = TEXT("5");
  22. // ----------------------------------------
  23. // Update the CLSID key for the application
  24. // ----------------------------------------
  25. // Open HKEY_CLASSES_ROOT\CLSID\{af4ae0d1-...1a}
  26. _tcscpy( tszKeyName, TEXT("CLSID\\") );
  27. _tcscat( tszKeyName, tszCLSIDApp );
  28. lRet = RegCreateKeyEx(HKEY_CLASSES_ROOT,
  29. tszKeyName,
  30. 0,
  31. NULL,
  32. 0,
  33. KEY_ALL_ACCESS,
  34. NULL,
  35. &hkeyBase,
  36. &dwDisposition );
  37. if( ERROR_SUCCESS != lRet )
  38. {
  39. tszError = TEXT("Couldn't create primary CLSID key");
  40. goto Exit;
  41. }
  42. // Write a name for this CLSID
  43. lRet = RegSetValueEx( hkeyBase,
  44. NULL,
  45. 0,
  46. REG_SZ,
  47. (const BYTE*) tszServerName,
  48. _tcslen(tszServerName) * sizeof(TCHAR) );
  49. if( ERROR_SUCCESS != lRet )
  50. {
  51. tszError = TEXT("Couldn't set local server name");
  52. goto Exit;
  53. }
  54. // Get this program's path and name
  55. if( !GetModuleFileName( hinst, tszModulePathAndName,
  56. sizeof(tszModulePathAndName)/sizeof(TCHAR) ))
  57. {
  58. tszError = TEXT("Couldn't get Module file name");
  59. goto Exit;
  60. }
  61. // Set the LocalServer32 value
  62. lRet = RegCreateKeyEx(hkeyBase,
  63. TEXT("LocalServer32"),
  64. 0,
  65. NULL,
  66. 0,
  67. KEY_ALL_ACCESS,
  68. NULL,
  69. &hkey,
  70. &dwDisposition );
  71. if( ERROR_SUCCESS != lRet )
  72. {
  73. tszError = TEXT("Couldn't create LocalServer32 key");
  74. goto Exit;
  75. }
  76. lRet = RegSetValueEx( hkey,
  77. NULL,
  78. 0,
  79. REG_SZ,
  80. (const BYTE*) tszModulePathAndName,
  81. (1 + _tcslen(tszModulePathAndName) ) * sizeof(TCHAR) );
  82. if( ERROR_SUCCESS != lRet )
  83. {
  84. tszError = TEXT("Couldn't set local server name");
  85. goto Exit;
  86. }
  87. CloseHandle( hkey );
  88. hkey = NULL;
  89. CloseHandle( hkeyBase );
  90. hkeyBase = NULL;
  91. // ---------------------------------------
  92. // Update the CLSID key for the proxy/stub
  93. // ---------------------------------------
  94. // Set the InProcServer32 value (the proxy/stub)
  95. // We assume that the proxy/stub has the same name as this
  96. // local server (except with a dll extension), and we assume
  97. // that it's in the same path. We really should have a
  98. // DllRegisterServer function in the DLL itself that does this,
  99. // but we do it here instead to save some code.
  100. _tcscpy( &tszModulePathAndName[ _tcslen(tszModulePathAndName) - 3 ],
  101. TEXT("dll") );
  102. _tcscpy( tszKeyName, TEXT("CLSID\\") );
  103. _tcscat( tszKeyName, tszCLSIDInterface );
  104. lRet = RegCreateKeyEx(HKEY_CLASSES_ROOT,
  105. tszKeyName,
  106. 0,
  107. NULL,
  108. 0,
  109. KEY_ALL_ACCESS,
  110. NULL,
  111. &hkeyBase,
  112. &dwDisposition );
  113. if( ERROR_SUCCESS != lRet )
  114. {
  115. tszError = TEXT("Couldn't create proxy/stub CLSID key");
  116. goto Exit;
  117. }
  118. // Write a name for this CLSID
  119. lRet = RegSetValueEx( hkeyBase,
  120. NULL,
  121. 0,
  122. REG_SZ,
  123. (const BYTE*) tszProxyStubName,
  124. _tcslen(tszProxyStubName) * sizeof(TCHAR) );
  125. if( ERROR_SUCCESS != lRet )
  126. {
  127. tszError = TEXT("Couldn't set proxy/stub name");
  128. goto Exit;
  129. }
  130. // Set the InproxServer32 value
  131. lRet = RegCreateKeyEx(hkeyBase,
  132. TEXT("InprocServer32"),
  133. 0,
  134. NULL,
  135. 0,
  136. KEY_ALL_ACCESS,
  137. NULL,
  138. &hkey,
  139. &dwDisposition );
  140. if( ERROR_SUCCESS != lRet )
  141. {
  142. tszError = TEXT("Couldn't create InprocServer32 key");
  143. goto Exit;
  144. }
  145. lRet = RegSetValueEx( hkey,
  146. NULL,
  147. 0,
  148. REG_SZ,
  149. (const BYTE*) tszModulePathAndName,
  150. (1 + _tcslen(tszModulePathAndName) ) * sizeof(TCHAR) );
  151. if( ERROR_SUCCESS != lRet )
  152. {
  153. tszError = TEXT("Couldn't set proxy/stub value");
  154. goto Exit;
  155. }
  156. CloseHandle( hkey );
  157. hkey = NULL;
  158. CloseHandle( hkeyBase );
  159. hkeyBase = NULL;
  160. // ------------------------
  161. // Update the Interface key
  162. // ------------------------
  163. // Open HKEY_CLASSES_ROOT\Interface\{af4ae0d0-...1a}
  164. _tcscpy( tszKeyName, TEXT("Interface\\") );
  165. _tcscat( tszKeyName, tszCLSIDInterface );
  166. lRet = RegCreateKeyEx(HKEY_CLASSES_ROOT,
  167. tszKeyName,
  168. 0,
  169. NULL,
  170. 0,
  171. KEY_ALL_ACCESS,
  172. NULL,
  173. &hkeyBase,
  174. &dwDisposition );
  175. if( ERROR_SUCCESS != lRet )
  176. {
  177. tszError = TEXT("Couldn't create interface key");
  178. goto Exit;
  179. }
  180. // Write a name for this IID
  181. lRet = RegSetValueEx( hkeyBase,
  182. NULL,
  183. 0,
  184. REG_SZ,
  185. (const BYTE*) tszServerName,
  186. _tcslen(tszServerName) * sizeof(TCHAR) );
  187. if( ERROR_SUCCESS != lRet )
  188. {
  189. tszError = TEXT("Couldn't set local server name");
  190. goto Exit;
  191. }
  192. // Set the NumMethods value
  193. lRet = RegCreateKeyEx(hkeyBase,
  194. TEXT("NumMethods"),
  195. 0,
  196. NULL,
  197. 0,
  198. KEY_ALL_ACCESS,
  199. NULL,
  200. &hkey,
  201. &dwDisposition );
  202. if( ERROR_SUCCESS != lRet )
  203. {
  204. tszError = TEXT("Couldn't create NumMethods key");
  205. goto Exit;
  206. }
  207. lRet = RegSetValueEx( hkey,
  208. NULL,
  209. 0,
  210. REG_SZ,
  211. (const BYTE*) tszNumMethods,
  212. (1 + _tcslen(tszNumMethods) ) * sizeof(TCHAR) );
  213. if( ERROR_SUCCESS != lRet )
  214. {
  215. tszError = TEXT("Couldn't set number of methods");
  216. goto Exit;
  217. }
  218. CloseHandle( hkey );
  219. hkey = NULL;
  220. // Set the Proxy/Stub CLSID
  221. lRet = RegCreateKeyEx(hkeyBase,
  222. TEXT("ProxyStubClsid32"),
  223. 0,
  224. NULL,
  225. 0,
  226. KEY_ALL_ACCESS,
  227. NULL,
  228. &hkey,
  229. &dwDisposition );
  230. if( ERROR_SUCCESS != lRet )
  231. {
  232. tszError = TEXT("Couldn't create ProxyStubClsid32 key");
  233. goto Exit;
  234. }
  235. lRet = RegSetValueEx( hkey,
  236. NULL,
  237. 0,
  238. REG_SZ,
  239. (const BYTE*) tszCLSIDInterface,
  240. (1 + _tcslen(tszCLSIDInterface) ) * sizeof(TCHAR) );
  241. if( ERROR_SUCCESS != lRet )
  242. {
  243. tszError = TEXT("Couldn't set ProxyStubClsid32");
  244. goto Exit;
  245. }
  246. CloseHandle( hkey );
  247. hkey = NULL;
  248. CloseHandle( hkeyBase );
  249. hkeyBase = NULL;
  250. // ----
  251. // Exit
  252. // ----
  253. Exit:
  254. if( ERROR_SUCCESS != lRet )
  255. {
  256. TCHAR tszErrorMessage[ 256 ];
  257. _tcscpy( tszErrorMessage, tszError );
  258. _stprintf( &tszErrorMessage[ _tcslen(tszErrorMessage) ],
  259. TEXT("\nError = %lu"),
  260. GetLastError() );
  261. MessageBox( NULL, tszErrorMessage, TEXT("PStgServ (PropTest) Self-Registration Error"), MB_OK );
  262. if( hkey ) CloseHandle( hkey );
  263. if( hkeyBase ) CloseHandle( hkeyBase );
  264. }
  265. return;
  266. }
  267. int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
  268. LPSTR lpszCmdLine, int nCmdShow )
  269. {
  270. if( !strcmp( lpszCmdLine, "/RegServer" )
  271. ||
  272. !strcmp( lpszCmdLine, "-RegServer" ))
  273. {
  274. SelfRegistration( hInstance );
  275. }
  276. else if( cPropStgServerApp.Init(hInstance, hPrevInstance,
  277. lpszCmdLine, nCmdShow) )
  278. {
  279. return( cPropStgServerApp.Run() );
  280. }
  281. return( 0 );
  282. }