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.

1557 lines
39 KiB

  1. /*++
  2. Copyright (c) 1992 Microsoft Corporation
  3. Module Name:
  4. SCANSI.CXX
  5. Abstract:
  6. This file contains ansi wrappers for the Service Controller API
  7. functions.
  8. Author:
  9. Dan Lafferty (danl) 04-Feb-1992
  10. Environment:
  11. User Mode - Win32
  12. Revision History:
  13. 05-Nov-1992 Danl
  14. Added DisplayName Changes & new API.
  15. 28-May-1992 JohnRo
  16. RAID 9829: winsvc.h and related file cleanup.
  17. 14-Apr-1992 JohnRo
  18. We should not return password from any of our APIs.
  19. 04-Feb-1992 danl
  20. Created
  21. --*/
  22. #include "precomp.hxx"
  23. #include <sclib.h> // ScConvertToUnicode, ScConvertToAnsi
  24. #include <scwow.h> // 32/64-bit interop structures
  25. DWORD
  26. ROpenSCManagerA(
  27. IN LPSTR lpMachineName,
  28. IN LPSTR lpDatabaseName OPTIONAL,
  29. IN DWORD dwDesiredAccess,
  30. OUT LPSC_RPC_HANDLE lpScHandle
  31. )
  32. /*++
  33. Routine Description:
  34. Arguments:
  35. lpMachineName -
  36. lpDatabaseName -
  37. dwDesiredAccess -
  38. lpScHandle -
  39. Return Value:
  40. Note:
  41. --*/
  42. {
  43. DWORD status;
  44. LPWSTR lpDatabaseNameW = NULL;
  45. //
  46. // This parameter got us to the server side and is uninteresting
  47. // once we get here.
  48. //
  49. UNREFERENCED_PARAMETER(lpMachineName);
  50. //
  51. // Create a unicode version of lpDatabaseName
  52. //
  53. if (ARGUMENT_PRESENT(lpDatabaseName)) {
  54. if(!ScConvertToUnicode(&lpDatabaseNameW, lpDatabaseName)) {
  55. SC_LOG(ERROR,"ROpenSCManagerA:ScConvertToUnicode failed\n",0);
  56. return(ERROR_NOT_ENOUGH_MEMORY);
  57. }
  58. }
  59. status = ROpenSCManagerW (
  60. NULL,
  61. lpDatabaseNameW,
  62. dwDesiredAccess,
  63. lpScHandle);
  64. if (ARGUMENT_PRESENT(lpDatabaseName)) {
  65. LocalFree(lpDatabaseNameW);
  66. }
  67. return(status);
  68. }
  69. DWORD
  70. ROpenServiceA(
  71. IN SC_RPC_HANDLE hSCManager,
  72. IN LPSTR lpServiceName,
  73. IN DWORD dwDesiredAccess,
  74. OUT LPSC_RPC_HANDLE phService
  75. )
  76. /*++
  77. Routine Description:
  78. Returns a handle to the service. This handle is actually a pointer
  79. to a data structure that contains a pointer to the service record.
  80. Arguments:
  81. hSCManager - This is a handle to this service controller. It is an
  82. RPC context handle, and has allowed the request to get this far.
  83. lpServiceName - This is a pointer to a string containing the name of
  84. the service
  85. dwDesiredAccess - This is an access mask that contains a description
  86. of the access that is desired for this service.
  87. phService - This is a pointer to the location where the handle to the
  88. service is to be placed.
  89. Return Value:
  90. NO_ERROR - The operation was successful.
  91. ERROR_INVALID_HANDLE - The specified handle is invalid.
  92. ERROR_SERVICE_DOES_NOT_EXIST - The specified service does not exist
  93. in the database.
  94. ERROR_NOT_ENOUGH_MEMORY - The memory allocation for the handle structure
  95. failed.
  96. Note:
  97. --*/
  98. {
  99. DWORD status;
  100. LPWSTR lpServiceNameW;
  101. //
  102. // Create a unicode version of lpServiceName
  103. //
  104. if(!ScConvertToUnicode(&lpServiceNameW, lpServiceName)) {
  105. SC_LOG(ERROR,"ROpenServiceA:ScConvertToUnicode failed\n",0);
  106. return(ERROR_NOT_ENOUGH_MEMORY);
  107. }
  108. status = ROpenServiceW (
  109. hSCManager,
  110. lpServiceNameW,
  111. dwDesiredAccess,
  112. phService);
  113. LocalFree(lpServiceNameW);
  114. return(status);
  115. }
  116. DWORD
  117. RStartServiceA(
  118. IN SC_RPC_HANDLE hService,
  119. IN DWORD NumArgs,
  120. IN LPSTRING_PTRSA CmdArgs
  121. )
  122. /*++
  123. Routine Description:
  124. This function begins the execution of a service.
  125. Arguments:
  126. hService - A handle which is a pointer to a service handle structure.
  127. dwNumServiceArgs - This indicates the number of argument vectors.
  128. lpServiceArgVectors - This is a pointer to an array of string pointers.
  129. Return Value:
  130. NO_ERROR - The operation was completely successful.
  131. ERROR_ACCESS_DENIED - The specified handle was not opened with
  132. SERVICE_START access.
  133. ERROR_INVALID_HANDLE - The specified handle was invalid.
  134. ERROR_SERVICE_WAS_STARTED - An instance of the service is already running.
  135. ERROR_SERVICE_REQUEST_TIMEOUT - The service did not respond to the start
  136. request in a timely fashion.
  137. ERROR_SERVICE_NO_THREAD - A thread could not be created for the Win32
  138. service.
  139. ERROR_PATH_NOT_FOUND - The image file name could not be found in
  140. the configuration database (registry), or the image file name
  141. failed in a unicode/ansi conversion.
  142. --*/
  143. {
  144. DWORD status = NO_ERROR;
  145. LPWSTR *CmdArgsW = NULL;
  146. DWORD bufferSize=0;
  147. DWORD i;
  148. LPSTR *cmdArgs;
  149. if (NumArgs > 0) {
  150. //
  151. // If there are command args, create a unicode version of them.
  152. //
  153. //
  154. // Allocate a buffer for the unicode command arg pointers.
  155. //
  156. bufferSize = NumArgs * sizeof(LPWSTR);
  157. CmdArgsW = (LPWSTR *)LocalAlloc(LMEM_ZEROINIT, (UINT) bufferSize);
  158. if (CmdArgsW == NULL) {
  159. SC_LOG(ERROR,"RStartServicesA: LocalAlloc Failed\n",0);
  160. return(ERROR_NOT_ENOUGH_MEMORY);
  161. }
  162. __try {
  163. //
  164. // For each command Arg, allocate a string and convert the
  165. // unicode version of the string into it.
  166. //
  167. cmdArgs = (LPSTR *)CmdArgs;
  168. for (i=0; i<NumArgs; i++) {
  169. if(!ScConvertToUnicode(&(CmdArgsW[i]), cmdArgs[i])) {
  170. SC_LOG(ERROR,
  171. "RStartServicesA: LocalAlloc (convert) Failed\n",0);
  172. status = ERROR_NOT_ENOUGH_MEMORY;
  173. break;
  174. }
  175. }
  176. }
  177. __except (EXCEPTION_EXECUTE_HANDLER) {
  178. status = GetExceptionCode();
  179. if (status != EXCEPTION_ACCESS_VIOLATION) {
  180. SC_LOG(ERROR,
  181. "RStartServicesA: Unexpected Exception 0x%lx\n",status);
  182. }
  183. }
  184. //
  185. // If any errors occured in the conversion process. Abort and
  186. // return the error to the caller.
  187. //
  188. if (status != NO_ERROR) {
  189. goto CleanExit;
  190. }
  191. }
  192. status = RStartServiceW(
  193. hService,
  194. NumArgs,
  195. (LPSTRING_PTRSW)CmdArgsW);
  196. CleanExit:
  197. if (NumArgs > 0) {
  198. //
  199. // If there were unicode versions of the arguments created for
  200. // this function, release the memory that was allocated.
  201. //
  202. for(i = 0; i < NumArgs; i++) {
  203. if (CmdArgsW[i] != NULL) {
  204. LocalFree(CmdArgsW[i]);
  205. }
  206. }
  207. LocalFree(CmdArgsW);
  208. }
  209. return(status);
  210. }
  211. DWORD
  212. REnumServicesStatusA (
  213. IN SC_RPC_HANDLE hSCManager,
  214. IN DWORD dwServiceType,
  215. IN DWORD dwServiceState,
  216. OUT PBYTE lpBuffer,
  217. IN DWORD cbBufSize,
  218. OUT LPDWORD pcbBytesNeeded,
  219. OUT LPDWORD lpServicesReturned,
  220. IN OUT LPDWORD lpResumeIndex OPTIONAL
  221. )
  222. /*++
  223. Routine Description:
  224. This function lists the services installed in the Service Controllers
  225. database. The status of each service is returned with the name of
  226. the service.
  227. Arguments:
  228. hSCManager - This is a handle to the service controller.
  229. dwServiceType - Value to select the type of services to enumerate.
  230. It must be one of the bitwise OR of the following values:
  231. SERVICE_WIN32 - enumerate Win32 services only.
  232. SERVICE_DRIVER - enumerate Driver services only.
  233. dwServiceState - Value so select the services to enumerate based on the
  234. running state. It must be one or the bitwise OR of the following
  235. values:
  236. SERVICE_ACTIVE - enumerate services that have started.
  237. SERVICE_INACTIVE - enumerate services that are stopped.
  238. lpBuffer - A pointer to a buffer to receive an array of enum status
  239. (or service) entries.
  240. cbBufSize - Size of the buffer in bytes pointed to by lpBuffer.
  241. pcbBytesNeeded - A pointer to a location where the number of bytes
  242. left (to be enumerated) is to be placed. This indicates to the
  243. caller how large the buffer must be in order to complete the
  244. enumeration with the next call.
  245. lpServicesReturned - A pointer to a variable to receive the number of
  246. of service entries returned.
  247. lpResumeIndex - A pointer to a variable which on input specifies the
  248. index of a service entry to begin enumeration. An index of 0
  249. indicates to start at the beginning. On output, if this function
  250. returns ERROR_MORE_DATA, the index returned is the next service
  251. entry to resume the enumeration. The returned index is 0 if this
  252. function returns a NO_ERROR.
  253. Return Value:
  254. NO_ERROR - The operation was successful.
  255. ERROR_MORE_DATA - Not all of the data in the active database could be
  256. returned due to the size of the user's buffer. pcbBytesNeeded
  257. contains the number of bytes required to get the remaining
  258. entries.
  259. ERROR_INVALID_PARAMETER - An illegal parameter value was passed in.
  260. (such as dwServiceType).
  261. ERROR_INVALID_HANDLE - The specified handle was invalid.
  262. Note:
  263. It is expected that the RPC Stub functions will find the following
  264. parameter problems:
  265. Bad pointers for lpBuffer, pcbReturned, pcbBytesNeeded,
  266. lpBuffer, ReturnedServerName, and lpResumeIndex.
  267. --*/
  268. {
  269. DWORD status;
  270. LPENUM_SERVICE_STATUS_WOW64 pEnumRec;
  271. LPWSTR pServiceName;
  272. LPWSTR pDisplayName;
  273. DWORD i;
  274. //
  275. // Initialize entries returned because we convert the buffer on
  276. // output based on the number of returned entries.
  277. //
  278. *lpServicesReturned = 0;
  279. status = REnumServicesStatusW (
  280. hSCManager,
  281. dwServiceType,
  282. dwServiceState,
  283. lpBuffer,
  284. cbBufSize,
  285. pcbBytesNeeded,
  286. lpServicesReturned,
  287. lpResumeIndex);
  288. if (*lpServicesReturned > 0)
  289. {
  290. //
  291. // Convert the returned unicode structures to Ansi.
  292. //
  293. pEnumRec = (LPENUM_SERVICE_STATUS_WOW64) lpBuffer;
  294. for (i = 0; i < *lpServicesReturned; i++)
  295. {
  296. //
  297. // Note: in these conversions, the pointers to the names are
  298. // stored as offsets at this point.
  299. //
  300. pServiceName = (LPWSTR) (lpBuffer + pEnumRec[i].dwServiceNameOffset);
  301. pDisplayName = (LPWSTR) (lpBuffer + pEnumRec[i].dwDisplayNameOffset);
  302. if (!ScConvertToAnsi((LPSTR) pServiceName, pServiceName))
  303. {
  304. SC_LOG(ERROR,"REnumServicesStatusA:ScConvertToAnsi failed\n",0);
  305. status = ERROR_NOT_ENOUGH_MEMORY;
  306. *lpServicesReturned = 0;
  307. break;
  308. }
  309. //
  310. // Convert the Display Name.
  311. //
  312. if (!ScConvertToAnsi((LPSTR)pDisplayName, pDisplayName)) {
  313. SC_LOG(ERROR,"REnumServicesStatusA:ScConvertToAnsi failed\n",0);
  314. status = ERROR_NOT_ENOUGH_MEMORY;
  315. *lpServicesReturned = 0;
  316. break;
  317. }
  318. }
  319. }
  320. return(status);
  321. }
  322. DWORD
  323. REnumServicesStatusExA (
  324. IN SC_RPC_HANDLE hSCManager,
  325. IN SC_ENUM_TYPE InfoLevel,
  326. IN DWORD dwServiceType,
  327. IN DWORD dwServiceState,
  328. OUT PBYTE lpBuffer,
  329. IN DWORD cbBufSize,
  330. OUT LPDWORD pcbBytesNeeded,
  331. OUT LPDWORD lpServicesReturned,
  332. IN OUT LPDWORD lpResumeIndex OPTIONAL,
  333. IN LPCSTR pszGroupNameAnsi
  334. )
  335. /*++
  336. Routine Description:
  337. This function is analogous to REnumServicesStatusA, with the data
  338. being enumerated being dependent upon the InfoLevel parameter
  339. Arguments:
  340. InfoLevel - An enumerated type that determines what service attributes
  341. are enumerated:
  342. SC_ENUM_ALL_INFO - Enumerates all the service information from
  343. REnumServicesStatusW plus the service's PID and flags
  344. pszGroupName - Only enumerate services belonging to the given group.
  345. If this parameter is the empty string, services not belonging to
  346. a group are enumerated. If this parameter is NULL, no attention
  347. is paid to group.
  348. Return Value:
  349. NO_ERROR - The operation was successful.
  350. ERROR_MORE_DATA - Not all of the data in the active database could be
  351. returned due to the size of the user's buffer. pcbBytesNeeded
  352. contains the number of bytes required to get the remaining
  353. entries.
  354. ERROR_INVALID_PARAMETER - An illegal parameter value was passed in.
  355. (such as dwServiceType).
  356. ERROR_INVALID_HANDLE - The specified handle was invalid.
  357. ERROR_INVALID_LEVEL - The specified InfoLevel is invalid
  358. Note:
  359. It is expected that the RPC Stub functions will find the following
  360. parameter problems:
  361. Bad pointers for lpBuffer, pcbReturned, pcbBytesNeeded,
  362. lpBuffer, ReturnedServerName, and lpResumeIndex.
  363. --*/
  364. {
  365. DWORD status;
  366. LPENUM_SERVICE_STATUS_PROCESS_WOW64 pEnumRec;
  367. LPWSTR pServiceName;
  368. LPWSTR pDisplayName;
  369. LPWSTR pszGroupName = NULL;
  370. DWORD i;
  371. //
  372. // Initialize entries returned because we convert the buffer on
  373. // output based on the number of returned entries.
  374. //
  375. *lpServicesReturned = 0;
  376. if (ARGUMENT_PRESENT(pszGroupNameAnsi)) {
  377. if (!ScConvertToUnicode(&pszGroupName, pszGroupNameAnsi)) {
  378. SC_LOG(ERROR,"EnumServicesStatusExA: ScConvertToUnicode failed\n",0);
  379. return ERROR_NOT_ENOUGH_MEMORY;
  380. }
  381. }
  382. status = REnumServicesStatusExW (
  383. hSCManager,
  384. InfoLevel,
  385. dwServiceType,
  386. dwServiceState,
  387. lpBuffer,
  388. cbBufSize,
  389. pcbBytesNeeded,
  390. lpServicesReturned,
  391. lpResumeIndex,
  392. (LPCWSTR)pszGroupName);
  393. if (*lpServicesReturned > 0)
  394. {
  395. //
  396. // Convert the returned unicode structures to Ansi.
  397. //
  398. pEnumRec = (LPENUM_SERVICE_STATUS_PROCESS_WOW64) lpBuffer;
  399. for (i = 0; i < *lpServicesReturned; i++) {
  400. //
  401. // Note: in these conversions, the pointers to the names are
  402. // stored as offsets at this point.
  403. //
  404. pServiceName = (LPWSTR) (lpBuffer + pEnumRec[i].dwServiceNameOffset);
  405. pDisplayName = (LPWSTR) (lpBuffer + pEnumRec[i].dwDisplayNameOffset);
  406. if (!ScConvertToAnsi((LPSTR)pServiceName, pServiceName)) {
  407. SC_LOG(ERROR,"REnumServicesStatusA:ScConvertToAnsi failed\n",0);
  408. status = ERROR_NOT_ENOUGH_MEMORY;
  409. *lpServicesReturned = 0;
  410. break;
  411. }
  412. //
  413. // Convert the Display Name.
  414. //
  415. if (!ScConvertToAnsi((LPSTR)pDisplayName, pDisplayName)) {
  416. SC_LOG(ERROR,"REnumServicesStatusA:ScConvertToAnsi failed\n",0);
  417. status = ERROR_NOT_ENOUGH_MEMORY;
  418. *lpServicesReturned = 0;
  419. break;
  420. }
  421. }
  422. }
  423. LocalFree(pszGroupName);
  424. return(status);
  425. }
  426. DWORD
  427. RChangeServiceConfigA(
  428. IN SC_RPC_HANDLE hService,
  429. IN DWORD dwServiceType,
  430. IN DWORD dwStartType,
  431. IN DWORD dwErrorControl,
  432. IN LPSTR lpBinaryPathName,
  433. IN LPSTR lpLoadOrderGroup,
  434. OUT LPDWORD lpdwTagId,
  435. IN LPBYTE lpDependencies,
  436. IN DWORD dwDependSize,
  437. IN LPSTR lpServiceStartName,
  438. IN LPBYTE EncryptedPassword,
  439. IN DWORD PasswordSize,
  440. IN LPSTR lpDisplayName
  441. )
  442. /*++
  443. Routine Description:
  444. Arguments:
  445. lpDependencies - A buffer of size dwDependSize which already contains
  446. Unicode strings.
  447. Return Value:
  448. Note:
  449. --*/
  450. {
  451. DWORD status;
  452. LPWSTR lpDisplayNameW = NULL;
  453. LPWSTR lpBinaryPathNameW = NULL;
  454. LPWSTR lpLoadOrderGroupW = NULL;
  455. LPWSTR lpServiceStartNameW = NULL;
  456. //
  457. // Create a unicode version of lpBinaryPathName
  458. //
  459. if (ARGUMENT_PRESENT(lpBinaryPathName)) {
  460. if(!ScConvertToUnicode(&lpBinaryPathNameW, lpBinaryPathName)) {
  461. SC_LOG(ERROR,"ChangeServiceConfigA:ScConvertToUnicode failed\n",0);
  462. status = ERROR_NOT_ENOUGH_MEMORY;
  463. goto CleanExit;
  464. }
  465. }
  466. //
  467. // Create a unicode version of lpLoadOrderGroup
  468. //
  469. if (ARGUMENT_PRESENT(lpLoadOrderGroup)) {
  470. if(!ScConvertToUnicode(&lpLoadOrderGroupW, lpLoadOrderGroup)) {
  471. SC_LOG(ERROR,"ChangeServiceConfigA:ScConvertToUnicode failed\n",0);
  472. status = ERROR_NOT_ENOUGH_MEMORY;
  473. goto CleanExit;
  474. }
  475. }
  476. //
  477. // Create a unicode version of lpServiceStartName
  478. //
  479. if (ARGUMENT_PRESENT(lpServiceStartName)) {
  480. if(!ScConvertToUnicode(&lpServiceStartNameW, lpServiceStartName)) {
  481. SC_LOG(ERROR,"ChangeServiceConfigA:ScConvertToUnicode failed\n",0);
  482. status = ERROR_NOT_ENOUGH_MEMORY;
  483. goto CleanExit;
  484. }
  485. }
  486. //
  487. // Create a unicode version of lpDisplayName
  488. //
  489. if (ARGUMENT_PRESENT(lpDisplayName)) {
  490. if(!ScConvertToUnicode(&lpDisplayNameW, lpDisplayName)) {
  491. SC_LOG(ERROR,"ChangeServiceConfigA:ScConvertToUnicode failed\n",0);
  492. status = ERROR_NOT_ENOUGH_MEMORY;
  493. goto CleanExit;
  494. }
  495. }
  496. //
  497. // Make the Unicode API Call
  498. //
  499. status = RChangeServiceConfigW(
  500. (SC_RPC_HANDLE)hService,
  501. dwServiceType,
  502. dwStartType,
  503. dwErrorControl,
  504. lpBinaryPathNameW,
  505. lpLoadOrderGroupW,
  506. lpdwTagId,
  507. lpDependencies,
  508. dwDependSize,
  509. lpServiceStartNameW,
  510. EncryptedPassword,
  511. PasswordSize,
  512. lpDisplayNameW);
  513. CleanExit:
  514. LocalFree(lpBinaryPathNameW);
  515. LocalFree(lpLoadOrderGroupW);
  516. LocalFree(lpServiceStartNameW);
  517. LocalFree(lpDisplayNameW);
  518. return(status);
  519. }
  520. DWORD
  521. RChangeServiceConfig2A(
  522. IN SC_RPC_HANDLE hService,
  523. IN SC_RPC_CONFIG_INFOA Info
  524. )
  525. /*++
  526. Routine Description:
  527. Arguments:
  528. Return Value:
  529. Note:
  530. --*/
  531. {
  532. DWORD status = NO_ERROR;
  533. //
  534. // Make a Unicode version of the arguments to pass to the Unicode function
  535. //
  536. union
  537. {
  538. SERVICE_DESCRIPTIONW sd;
  539. SERVICE_FAILURE_ACTIONSW sfa;
  540. } Buffer;
  541. SC_RPC_CONFIG_INFOW InfoW = { Info.dwInfoLevel, &Buffer.sd };
  542. switch (Info.dwInfoLevel)
  543. {
  544. case SERVICE_CONFIG_DESCRIPTION:
  545. Buffer.sd.lpDescription = NULL;
  546. if (Info.psd == NULL)
  547. {
  548. InfoW.psd = NULL;
  549. break;
  550. }
  551. if (Info.psd->lpDescription != NULL)
  552. {
  553. if (!ScConvertToUnicode(&Buffer.sd.lpDescription,
  554. Info.psd->lpDescription))
  555. {
  556. SC_LOG0(ERROR, "RChangeServiceConfig2A: ScConvertToUnicode failed\n");
  557. status = ERROR_NOT_ENOUGH_MEMORY;
  558. goto CleanExit;
  559. }
  560. }
  561. break;
  562. case SERVICE_CONFIG_FAILURE_ACTIONS:
  563. RtlCopyMemory(&Buffer.sfa, Info.psfa, sizeof(SERVICE_FAILURE_ACTIONS));
  564. Buffer.sfa.lpRebootMsg = NULL;
  565. Buffer.sfa.lpCommand = NULL;
  566. if (Info.psfa == NULL)
  567. {
  568. InfoW.psfa = NULL;
  569. break;
  570. }
  571. if (Info.psfa->lpRebootMsg != NULL)
  572. {
  573. if (!ScConvertToUnicode(&Buffer.sfa.lpRebootMsg,
  574. Info.psfa->lpRebootMsg))
  575. {
  576. SC_LOG0(ERROR, "RChangeServiceConfig2A: ScConvertToUnicode failed\n");
  577. status = ERROR_NOT_ENOUGH_MEMORY;
  578. goto CleanExit;
  579. }
  580. }
  581. if (Info.psfa->lpCommand != NULL)
  582. {
  583. if (!ScConvertToUnicode(&Buffer.sfa.lpCommand,
  584. Info.psfa->lpCommand))
  585. {
  586. SC_LOG0(ERROR, "RChangeServiceConfig2A: ScConvertToUnicode failed\n");
  587. status = ERROR_NOT_ENOUGH_MEMORY;
  588. goto CleanExit;
  589. }
  590. }
  591. break;
  592. }
  593. //
  594. // Call the Unicode function
  595. //
  596. status = RChangeServiceConfig2W(hService, InfoW);
  597. CleanExit:
  598. //
  599. // Free the temporary Unicode strings
  600. //
  601. switch (Info.dwInfoLevel)
  602. {
  603. case SERVICE_CONFIG_DESCRIPTION:
  604. LocalFree(Buffer.sd.lpDescription);
  605. break;
  606. case SERVICE_CONFIG_FAILURE_ACTIONS:
  607. LocalFree(Buffer.sfa.lpRebootMsg);
  608. LocalFree(Buffer.sfa.lpCommand);
  609. break;
  610. }
  611. return status;
  612. }
  613. DWORD
  614. RCreateServiceA(
  615. IN SC_RPC_HANDLE hSCManager,
  616. IN LPSTR lpServiceName,
  617. IN LPSTR lpDisplayName,
  618. IN DWORD dwDesiredAccess,
  619. IN DWORD dwServiceType,
  620. IN DWORD dwStartType,
  621. IN DWORD dwErrorControl,
  622. IN LPSTR lpBinaryPathName,
  623. IN LPSTR lpLoadOrderGroup,
  624. OUT LPDWORD lpdwTagId,
  625. IN LPBYTE lpDependencies,
  626. IN DWORD dwDependSize,
  627. IN LPSTR lpServiceStartName,
  628. IN LPBYTE EncryptedPassword,
  629. IN DWORD PasswordSize,
  630. IN OUT LPSC_RPC_HANDLE lpServiceHandle
  631. )
  632. /*++
  633. Routine Description:
  634. Arguments:
  635. lpDependencies - A buffer of size dwDependSize which already contains
  636. Unicode strings.
  637. Return Value:
  638. Note:
  639. --*/
  640. {
  641. DWORD status;
  642. LPWSTR lpServiceNameW = NULL;
  643. LPWSTR lpDisplayNameW = NULL;
  644. LPWSTR lpBinaryPathNameW = NULL;
  645. LPWSTR lpLoadOrderGroupW = NULL;
  646. LPWSTR lpServiceStartNameW = NULL;
  647. //
  648. // Create a unicode version of lpServiceName
  649. //
  650. if (ARGUMENT_PRESENT(lpServiceName)) {
  651. if(!ScConvertToUnicode(&lpServiceNameW, lpServiceName)) {
  652. SC_LOG(ERROR,"RCreateServiceA:ScConvertToUnicode failed\n",0);
  653. status = ERROR_NOT_ENOUGH_MEMORY;
  654. goto CleanExit;
  655. }
  656. }
  657. //
  658. // Create a unicode version of lpDisplayName
  659. //
  660. if (ARGUMENT_PRESENT(lpDisplayName)) {
  661. if(!ScConvertToUnicode(&lpDisplayNameW, lpDisplayName)) {
  662. SC_LOG(ERROR,"RCreateServiceA:ScConvertToUnicode failed\n",0);
  663. status = ERROR_NOT_ENOUGH_MEMORY;
  664. goto CleanExit;
  665. }
  666. }
  667. //
  668. // Create a unicode version of lpBinaryPathName
  669. //
  670. if (ARGUMENT_PRESENT(lpBinaryPathName)) {
  671. if(!ScConvertToUnicode(&lpBinaryPathNameW, lpBinaryPathName)) {
  672. SC_LOG(ERROR,"RCreateServiceA:ScConvertToUnicode failed\n",0);
  673. status = ERROR_NOT_ENOUGH_MEMORY;
  674. goto CleanExit;
  675. }
  676. }
  677. //
  678. // Create a unicode version of lpLoadOrderGroup
  679. //
  680. if (ARGUMENT_PRESENT(lpLoadOrderGroup)) {
  681. if(!ScConvertToUnicode(&lpLoadOrderGroupW, lpLoadOrderGroup)) {
  682. SC_LOG(ERROR,"RCreateServiceA:ScConvertToUnicode failed\n",0);
  683. status = ERROR_NOT_ENOUGH_MEMORY;
  684. goto CleanExit;
  685. }
  686. }
  687. //
  688. // Create a unicode version of lpServiceStartName
  689. //
  690. if (ARGUMENT_PRESENT(lpServiceStartName)) {
  691. if(!ScConvertToUnicode(&lpServiceStartNameW, lpServiceStartName)) {
  692. SC_LOG(ERROR,"RCreateServiceA:ScConvertToUnicode failed\n",0);
  693. status = ERROR_NOT_ENOUGH_MEMORY;
  694. goto CleanExit;
  695. }
  696. }
  697. //
  698. // Make the Unicode API Call
  699. //
  700. status = RCreateServiceW (
  701. (SC_RPC_HANDLE)hSCManager,
  702. lpServiceNameW,
  703. lpDisplayNameW,
  704. dwDesiredAccess,
  705. dwServiceType,
  706. dwStartType,
  707. dwErrorControl,
  708. lpBinaryPathNameW,
  709. lpLoadOrderGroupW,
  710. lpdwTagId,
  711. lpDependencies,
  712. dwDependSize,
  713. lpServiceStartNameW,
  714. EncryptedPassword,
  715. PasswordSize,
  716. lpServiceHandle);
  717. CleanExit:
  718. LocalFree(lpServiceNameW);
  719. LocalFree(lpDisplayNameW);
  720. LocalFree(lpBinaryPathNameW);
  721. LocalFree(lpLoadOrderGroupW);
  722. LocalFree(lpServiceStartNameW);
  723. return(status);
  724. }
  725. DWORD
  726. REnumDependentServicesA(
  727. IN SC_RPC_HANDLE hService,
  728. IN DWORD dwServiceState,
  729. OUT LPBYTE lpBuffer,
  730. IN DWORD cbBufSize,
  731. OUT LPDWORD pcbBytesNeeded,
  732. OUT LPDWORD lpServicesReturned
  733. )
  734. /*++
  735. Routine Description:
  736. Arguments:
  737. Return Value:
  738. Note:
  739. --*/
  740. {
  741. DWORD status;
  742. LPENUM_SERVICE_STATUS_WOW64 pEnumRec;
  743. LPWSTR pServiceName;
  744. LPWSTR pDisplayName;
  745. DWORD i;
  746. //
  747. // Initialize entries returned because we convert the buffer on
  748. // output based on the number of returned entries.
  749. //
  750. *lpServicesReturned = 0;
  751. status = REnumDependentServicesW(
  752. hService,
  753. dwServiceState,
  754. lpBuffer,
  755. cbBufSize,
  756. pcbBytesNeeded,
  757. lpServicesReturned);
  758. if (*lpServicesReturned > 0) {
  759. //
  760. // Convert the returned unicode structures to Ansi.
  761. //
  762. pEnumRec = (LPENUM_SERVICE_STATUS_WOW64) lpBuffer;
  763. for (i=0; i<*lpServicesReturned; i++) {
  764. //
  765. // Note: in these conversions, the pointers to the names are
  766. // stored as offsets at this point.
  767. //
  768. pServiceName = (LPWSTR) (lpBuffer + (DWORD_PTR)(pEnumRec[i].dwServiceNameOffset));
  769. pDisplayName = (LPWSTR) (lpBuffer + (DWORD_PTR)(pEnumRec[i].dwDisplayNameOffset));
  770. if (!ScConvertToAnsi((LPSTR)pServiceName, pServiceName)) {
  771. SC_LOG(ERROR,"REnumDependendServicesA:ScConvertToAnsi failed\n",0);
  772. status = ERROR_NOT_ENOUGH_MEMORY;
  773. *lpServicesReturned = 0;
  774. break;
  775. }
  776. //
  777. // Convert the Display Name.
  778. //
  779. if (!ScConvertToAnsi((LPSTR)pDisplayName, pDisplayName)) {
  780. SC_LOG(ERROR,"REnumServicesStatusA:ScConvertToAnsi failed\n",0);
  781. status = ERROR_NOT_ENOUGH_MEMORY;
  782. *lpServicesReturned = 0;
  783. break;
  784. }
  785. }
  786. }
  787. return(status);
  788. }
  789. DWORD
  790. RQueryServiceConfigA(
  791. IN SC_RPC_HANDLE hService,
  792. OUT LPQUERY_SERVICE_CONFIGA lpServiceConfig,
  793. IN DWORD cbBufSize,
  794. OUT LPDWORD pcbBytesNeeded
  795. )
  796. /*++
  797. Routine Description:
  798. Arguments:
  799. Return Value:
  800. Note:
  801. --*/
  802. {
  803. DWORD status;
  804. //
  805. // Call the Unicode version of the API.
  806. //
  807. status = RQueryServiceConfigW(
  808. (SC_RPC_HANDLE)hService,
  809. (LPQUERY_SERVICE_CONFIGW)lpServiceConfig,
  810. cbBufSize,
  811. pcbBytesNeeded);
  812. //
  813. // If successful, convert the QUERY_SERVICE_CONFIG structure to
  814. // ansi.
  815. //
  816. if (status == NO_ERROR) {
  817. //
  818. // Convert lpBinaryPathName to Ansi
  819. //
  820. if (lpServiceConfig->lpBinaryPathName != NULL) {
  821. if(!ScConvertToAnsi(
  822. lpServiceConfig->lpBinaryPathName,
  823. ((LPQUERY_SERVICE_CONFIGW)lpServiceConfig)->lpBinaryPathName)) {
  824. SC_LOG(ERROR,"RQueryServiceConfigA:ScConvertToAnsi failed\n",0);
  825. status = ERROR_NOT_ENOUGH_MEMORY;
  826. }
  827. }
  828. //
  829. // Convert lpDisplayName to Ansi
  830. //
  831. if (lpServiceConfig->lpDisplayName != NULL) {
  832. if(!ScConvertToAnsi(
  833. lpServiceConfig->lpDisplayName,
  834. ((LPQUERY_SERVICE_CONFIGW)lpServiceConfig)->lpDisplayName)) {
  835. SC_LOG(ERROR,"RQueryServiceConfigA:ScConvertToAnsi failed\n",0);
  836. status = ERROR_NOT_ENOUGH_MEMORY;
  837. }
  838. }
  839. //
  840. // Convert lpLoadOrderGroup to Ansi
  841. //
  842. if (lpServiceConfig->lpLoadOrderGroup != NULL) {
  843. if(!ScConvertToAnsi(
  844. lpServiceConfig->lpLoadOrderGroup,
  845. ((LPQUERY_SERVICE_CONFIGW)lpServiceConfig)->lpLoadOrderGroup)) {
  846. SC_LOG(ERROR,"RQueryServiceConfigA:ScConvertToAnsi failed\n",0);
  847. status = ERROR_NOT_ENOUGH_MEMORY;
  848. }
  849. }
  850. //
  851. // Convert lpDependencies to Ansi
  852. //
  853. if (lpServiceConfig->lpDependencies != NULL) {
  854. if(!ScConvertToAnsi(
  855. lpServiceConfig->lpDependencies,
  856. ((LPQUERY_SERVICE_CONFIGW)lpServiceConfig)->lpDependencies)) {
  857. SC_LOG(ERROR,"RQueryServiceConfigA:ScConvertToAnsi failed\n",0);
  858. status = ERROR_NOT_ENOUGH_MEMORY;
  859. }
  860. }
  861. //
  862. // Convert lpServiceStartName to Ansi
  863. //
  864. if (lpServiceConfig->lpServiceStartName != NULL) {
  865. if(!ScConvertToAnsi(
  866. lpServiceConfig->lpServiceStartName,
  867. ((LPQUERY_SERVICE_CONFIGW)lpServiceConfig)->lpServiceStartName)) {
  868. SC_LOG(ERROR,"RQueryServiceConfigA:ScConvertToAnsi failed\n",0);
  869. status = ERROR_NOT_ENOUGH_MEMORY;
  870. }
  871. }
  872. }
  873. return(status);
  874. }
  875. DWORD
  876. RQueryServiceConfig2A(
  877. IN SC_RPC_HANDLE hService,
  878. IN DWORD dwInfoLevel,
  879. OUT LPBYTE lpBuffer,
  880. IN DWORD cbBufSize,
  881. OUT LPDWORD pcbBytesNeeded
  882. )
  883. /*++
  884. Routine Description:
  885. Arguments:
  886. Return Value:
  887. Note:
  888. --*/
  889. {
  890. DWORD status;
  891. DWORD StringOffset;
  892. //
  893. // Call the Unicode version of the API, using the same buffer.
  894. // Then convert the strings in the buffer to Ansi.
  895. //
  896. status = RQueryServiceConfig2W(
  897. hService,
  898. dwInfoLevel,
  899. lpBuffer,
  900. cbBufSize,
  901. pcbBytesNeeded
  902. );
  903. if (status == NO_ERROR)
  904. {
  905. switch (dwInfoLevel)
  906. {
  907. case SERVICE_CONFIG_DESCRIPTION:
  908. {
  909. StringOffset = ((LPSERVICE_DESCRIPTION_WOW64) lpBuffer)->dwDescriptionOffset;
  910. if (StringOffset != 0)
  911. {
  912. if(!ScConvertToAnsi(
  913. (LPSTR) (lpBuffer + StringOffset),
  914. (LPWSTR) (lpBuffer + StringOffset)
  915. ))
  916. {
  917. SC_LOG0(ERROR,"RQueryServiceConfig2A:ScConvertToAnsi failed\n");
  918. status = ERROR_NOT_ENOUGH_MEMORY;
  919. }
  920. }
  921. }
  922. break;
  923. case SERVICE_CONFIG_FAILURE_ACTIONS:
  924. {
  925. StringOffset = ((LPSERVICE_FAILURE_ACTIONS_WOW64) lpBuffer)->dwRebootMsgOffset;
  926. if (StringOffset != 0)
  927. {
  928. if(!ScConvertToAnsi(
  929. (LPSTR) lpBuffer + StringOffset,
  930. (LPWSTR) lpBuffer + StringOffset
  931. ))
  932. {
  933. SC_LOG0(ERROR,"RQueryServiceConfig2A:ScConvertToAnsi failed\n");
  934. status = ERROR_NOT_ENOUGH_MEMORY;
  935. break;
  936. }
  937. }
  938. StringOffset = ((LPSERVICE_FAILURE_ACTIONS_WOW64) lpBuffer)->dwCommandOffset;
  939. if (StringOffset != 0)
  940. {
  941. if(!ScConvertToAnsi(
  942. (LPSTR) lpBuffer + StringOffset,
  943. (LPWSTR) lpBuffer + StringOffset
  944. ))
  945. {
  946. SC_LOG0(ERROR,"RQueryServiceConfig2A:ScConvertToAnsi failed\n");
  947. status = ERROR_NOT_ENOUGH_MEMORY;
  948. }
  949. }
  950. }
  951. break;
  952. default:
  953. SC_ASSERT(0);
  954. break;
  955. }
  956. }
  957. return status;
  958. }
  959. DWORD
  960. RQueryServiceLockStatusA(
  961. IN SC_RPC_HANDLE hSCManager,
  962. OUT LPQUERY_SERVICE_LOCK_STATUSA lpLockStatus,
  963. IN DWORD cbBufSize,
  964. OUT LPDWORD pcbBytesNeeded
  965. )
  966. /*++
  967. Routine Description:
  968. Arguments:
  969. Return Value:
  970. Note:
  971. --*/
  972. {
  973. DWORD status;
  974. //
  975. // Call the Unicode version of the API
  976. //
  977. status = RQueryServiceLockStatusW(
  978. (SC_RPC_HANDLE)hSCManager,
  979. (LPQUERY_SERVICE_LOCK_STATUSW)lpLockStatus,
  980. cbBufSize,
  981. pcbBytesNeeded
  982. );
  983. //
  984. // If successful, convert the QUERY_SERVICE_LOCK_STATUS structure to
  985. // ansi.
  986. //
  987. if (status == NO_ERROR) {
  988. //
  989. // Convert lpLockOwner to Ansi
  990. //
  991. if (lpLockStatus->lpLockOwner != NULL) {
  992. if(!ScConvertToAnsi(
  993. lpLockStatus->lpLockOwner,
  994. ((LPQUERY_SERVICE_LOCK_STATUSW)lpLockStatus)->lpLockOwner)) {
  995. SC_LOG(ERROR,"RQueryServiceLockStatusA:ScConvertToAnsi failed\n",0);
  996. status = ERROR_NOT_ENOUGH_MEMORY;
  997. }
  998. }
  999. }
  1000. return(status);
  1001. }
  1002. DWORD
  1003. RGetServiceDisplayNameA(
  1004. SC_RPC_HANDLE hSCManager,
  1005. LPSTR lpServiceName,
  1006. LPSTR lpDisplayName,
  1007. LPDWORD lpcchBuffer
  1008. )
  1009. /*++
  1010. Routine Description:
  1011. This function returns the display name for a service that is identified
  1012. by its key name (ServiceName).
  1013. Arguments:
  1014. hSCManager - This is the handle to the Service Controller Manager that
  1015. is expected to return the display name.
  1016. lpServiceName - This is the ServiceName (which is actually a key
  1017. name) that identifies the service.
  1018. lpDisplayName - This is a pointer to a buffer that is to receive the
  1019. DisplayName string.
  1020. lpcchBuffer - This is a pointer to the size (in characters) of the
  1021. buffer that is to receive the DisplayName string. If the buffer
  1022. is not large enough to receive the entire string, then the required
  1023. buffer size is returned in this location. (NOTE: Ansi Characters,
  1024. including DBCS, are assumed to be 8 bits).
  1025. Return Value:
  1026. --*/
  1027. {
  1028. DWORD status;
  1029. LPWSTR lpServiceNameW = NULL;
  1030. DWORD numChars = 0;
  1031. DWORD numBytes = 0;
  1032. LPSTR tempBuffer=NULL;
  1033. //
  1034. // Create a unicode version of lpServiceName
  1035. //
  1036. if (ARGUMENT_PRESENT(lpServiceName)) {
  1037. if(!ScConvertToUnicode(&lpServiceNameW, lpServiceName)) {
  1038. SC_LOG(ERROR,"RCreateServiceA:ScConvertToUnicode failed\n",0);
  1039. status = ERROR_NOT_ENOUGH_MEMORY;
  1040. goto CleanExit;
  1041. }
  1042. }
  1043. //
  1044. // Because of DBCS, we can't predict what the proper buffer size should
  1045. // be. So we allocate a temporary buffer that will hold as many
  1046. // unicode characters as the original buffer would hold single byte
  1047. // characters.
  1048. //
  1049. numChars = *lpcchBuffer;
  1050. numBytes = (*lpcchBuffer) * sizeof(WCHAR);
  1051. tempBuffer = (LPSTR) LocalAlloc(LMEM_FIXED, numBytes);
  1052. if (tempBuffer == NULL)
  1053. {
  1054. status = GetLastError();
  1055. goto CleanExit;
  1056. }
  1057. //
  1058. // Make the Unicode API Call
  1059. //
  1060. status = RGetServiceDisplayNameW (
  1061. hSCManager,
  1062. lpServiceNameW,
  1063. (LPWSTR) tempBuffer,
  1064. &numChars);
  1065. if (status == NO_ERROR)
  1066. {
  1067. //
  1068. // Convert the returned Unicode string and string size back to
  1069. // ansi.
  1070. //
  1071. if (!ScConvertToAnsi(tempBuffer, (LPWSTR) tempBuffer)) {
  1072. SC_LOG0(ERROR, "RGetServiceDisplayNameA: ConvertToAnsi Failed\n");
  1073. }
  1074. numBytes = (DWORD) strlen(tempBuffer);
  1075. if ((numBytes+1) > *lpcchBuffer)
  1076. {
  1077. status = ERROR_INSUFFICIENT_BUFFER;
  1078. *lpcchBuffer = numBytes;
  1079. }
  1080. else
  1081. {
  1082. strcpy (lpDisplayName, tempBuffer);
  1083. }
  1084. }
  1085. else if (status == ERROR_INSUFFICIENT_BUFFER)
  1086. {
  1087. //
  1088. // Adjust the required buffer size for ansi.
  1089. //
  1090. *lpcchBuffer = numChars * sizeof(WCHAR);
  1091. }
  1092. CleanExit:
  1093. //
  1094. // Free up any resources that were allocated by this function.
  1095. //
  1096. LocalFree(tempBuffer);
  1097. LocalFree(lpServiceNameW);
  1098. return(status);
  1099. }
  1100. DWORD
  1101. RGetServiceKeyNameA(
  1102. SC_RPC_HANDLE hSCManager,
  1103. LPSTR lpDisplayName,
  1104. LPSTR lpServiceName,
  1105. LPDWORD lpcchBuffer
  1106. )
  1107. /*++
  1108. Routine Description:
  1109. Arguments:
  1110. Return Value:
  1111. --*/
  1112. {
  1113. DWORD status;
  1114. LPWSTR lpDisplayNameW = NULL;
  1115. DWORD numChars = 0;
  1116. DWORD numBytes = 0;
  1117. LPSTR tempBuffer=NULL;
  1118. //
  1119. // Create a unicode version of lpDisplayName
  1120. //
  1121. if (ARGUMENT_PRESENT(lpDisplayName))
  1122. {
  1123. if(!ScConvertToUnicode(&lpDisplayNameW, lpDisplayName))
  1124. {
  1125. SC_LOG(ERROR,"RGetServiceKeyNameA:ScConvertToUnicode failed\n",0);
  1126. status = ERROR_NOT_ENOUGH_MEMORY;
  1127. goto CleanExit;
  1128. }
  1129. }
  1130. //
  1131. // Because of DBCS, we can't predict what the proper buffer size should
  1132. // be. So we allocate a temporary buffer that will hold as many
  1133. // unicode characters as the original buffer would hold single byte
  1134. // characters.
  1135. //
  1136. numChars = *lpcchBuffer;
  1137. numBytes = (*lpcchBuffer) * sizeof(WCHAR);
  1138. tempBuffer = (LPSTR) LocalAlloc(LMEM_FIXED, numBytes);
  1139. if (tempBuffer == NULL)
  1140. {
  1141. status = GetLastError();
  1142. goto CleanExit;
  1143. }
  1144. //
  1145. // Make the Unicode API Call
  1146. //
  1147. status = RGetServiceKeyNameW (
  1148. hSCManager,
  1149. lpDisplayNameW,
  1150. (LPWSTR)tempBuffer,
  1151. &numChars);
  1152. if (status == NO_ERROR)
  1153. {
  1154. //
  1155. // Convert the returned Unicode string and string size back to
  1156. // ansi.
  1157. //
  1158. if (!ScConvertToAnsi(tempBuffer, (LPWSTR)tempBuffer))
  1159. {
  1160. SC_LOG0(ERROR, "RGetServiceKeyNameA: ConvertToAnsi Failed\n");
  1161. }
  1162. numBytes = (DWORD) strlen(tempBuffer);
  1163. if ((numBytes+1) > *lpcchBuffer)
  1164. {
  1165. status = ERROR_INSUFFICIENT_BUFFER;
  1166. *lpcchBuffer = numBytes;
  1167. }
  1168. else
  1169. {
  1170. strcpy (lpServiceName, tempBuffer);
  1171. }
  1172. }
  1173. else if (status == ERROR_INSUFFICIENT_BUFFER)
  1174. {
  1175. //
  1176. // Adjust the required buffer size for ansi.
  1177. //
  1178. *lpcchBuffer = numChars * sizeof(WCHAR);
  1179. }
  1180. CleanExit:
  1181. //
  1182. // Free up any resources that were allocated by this function.
  1183. //
  1184. LocalFree(tempBuffer);
  1185. LocalFree(lpDisplayNameW);
  1186. return(status);
  1187. }