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.

946 lines
29 KiB

  1. /* process.c
  2. * OemUnicode win32 thunks
  3. * - process\env stuff
  4. *
  5. * 14-Jan-1993 Jonle
  6. */
  7. #include <nt.h>
  8. #include <ntrtl.h>
  9. #include <nturtl.h>
  10. #include <windows.h>
  11. #include <oemuni.h>
  12. #include "oem.h"
  13. #include <vdmapi.h>
  14. #include "dpmtbls.h"
  15. UINT
  16. APIENTRY
  17. GetSystemDirectoryOem(
  18. LPSTR lpBuffer,
  19. UINT uSize
  20. )
  21. /*++
  22. Routine Description:
  23. OEM thunk to GetSystemDirectoryW
  24. --*/
  25. {
  26. OEM_STRING OemString;
  27. UNICODE_STRING Unicode;
  28. NTSTATUS Status;
  29. Unicode.MaximumLength = (USHORT)((uSize<<1)+sizeof(UNICODE_NULL));
  30. Unicode.Buffer = RtlAllocateHeap(
  31. RtlProcessHeap(), 0,
  32. Unicode.MaximumLength
  33. );
  34. if ( !Unicode.Buffer ) {
  35. BaseSetLastNTError(STATUS_NO_MEMORY);
  36. return 0;
  37. }
  38. Unicode.Length = GetSystemDirectoryW(Unicode.Buffer,
  39. (Unicode.MaximumLength-sizeof(UNICODE_NULL))/2
  40. )*2;
  41. if ( Unicode.Length > (USHORT)(Unicode.MaximumLength-sizeof(UNICODE_NULL)) ) {
  42. RtlFreeHeap(RtlProcessHeap(), 0,Unicode.Buffer);
  43. return Unicode.Length>>1;
  44. }
  45. OemString.Buffer = lpBuffer;
  46. OemString.MaximumLength = (USHORT)(uSize+1);
  47. Status = RtlUnicodeStringToOemString(&OemString,&Unicode,FALSE);
  48. RtlFreeHeap(RtlProcessHeap(), 0,Unicode.Buffer);
  49. if ( !NT_SUCCESS(Status) ) {
  50. BaseSetLastNTError(Status);
  51. return 0;
  52. }
  53. return OemString.Length;
  54. }
  55. UINT
  56. APIENTRY
  57. GetWindowsDirectoryOem(
  58. LPSTR lpBuffer,
  59. UINT uSize
  60. )
  61. /*++
  62. Routine Description:
  63. OEM thunk to GetWindowsDirectoryW
  64. --*/
  65. {
  66. OEM_STRING OemString;
  67. UNICODE_STRING Unicode;
  68. NTSTATUS Status;
  69. Unicode.MaximumLength = (USHORT)((uSize<<1)+sizeof(UNICODE_NULL));
  70. Unicode.Buffer = RtlAllocateHeap(
  71. RtlProcessHeap(), 0,
  72. Unicode.MaximumLength
  73. );
  74. if ( !Unicode.Buffer ) {
  75. BaseSetLastNTError(STATUS_NO_MEMORY);
  76. return 0;
  77. }
  78. Unicode.Length = GetWindowsDirectoryW(Unicode.Buffer,
  79. (Unicode.MaximumLength-sizeof(UNICODE_NULL))/2
  80. )*2;
  81. if ( Unicode.Length > (USHORT)(Unicode.MaximumLength-sizeof(UNICODE_NULL)) ) {
  82. RtlFreeHeap(RtlProcessHeap(), 0,Unicode.Buffer);
  83. return Unicode.Length>>1;
  84. }
  85. OemString.Buffer = lpBuffer;
  86. OemString.MaximumLength = (USHORT)(uSize+1);
  87. Status = RtlUnicodeStringToOemString(&OemString,&Unicode,FALSE);
  88. RtlFreeHeap(RtlProcessHeap(), 0,Unicode.Buffer);
  89. if ( !NT_SUCCESS(Status) ) {
  90. BaseSetLastNTError(Status);
  91. return 0;
  92. }
  93. return OemString.Length;
  94. }
  95. DWORD
  96. APIENTRY
  97. SearchPathOem (
  98. LPCSTR lpPath,
  99. LPCSTR lpFileName,
  100. LPCSTR lpExtension,
  101. DWORD nBufferLength,
  102. LPSTR lpBuffer,
  103. LPSTR *lpFilePart
  104. )
  105. /*++
  106. Routine Description:
  107. Oem thunk to SearchPathW
  108. --*/
  109. {
  110. UNICODE_STRING xlpPath;
  111. PUNICODE_STRING Unicode;
  112. UNICODE_STRING xlpExtension;
  113. PWSTR xlpBuffer;
  114. DWORD ReturnValue=0;
  115. OEM_STRING OemString;
  116. UNICODE_STRING UnicodeString;
  117. NTSTATUS Status;
  118. PWSTR FilePart;
  119. PWSTR *FilePartPtr;
  120. if ( ARGUMENT_PRESENT(lpFilePart) ) {
  121. FilePartPtr = &FilePart;
  122. } else {
  123. FilePartPtr = NULL;
  124. }
  125. Unicode = &NtCurrentTeb()->StaticUnicodeString;
  126. InitOemString(&OemString,lpFileName);
  127. Status = RtlOemStringToUnicodeString(Unicode,&OemString,FALSE);
  128. if ( !NT_SUCCESS(Status) ) {
  129. if ( Status == STATUS_BUFFER_OVERFLOW ) {
  130. SetLastError(ERROR_FILENAME_EXCED_RANGE);
  131. } else {
  132. BaseSetLastNTError(Status);
  133. }
  134. return 0;
  135. }
  136. if ( ARGUMENT_PRESENT(lpExtension) ) {
  137. InitOemString(&OemString,lpExtension);
  138. Status = RtlOemStringToUnicodeString(&xlpExtension,&OemString,TRUE);
  139. if ( !NT_SUCCESS(Status) ) {
  140. BaseSetLastNTError(Status);
  141. return 0;
  142. }
  143. } else {
  144. xlpExtension.Buffer = NULL;
  145. }
  146. if ( ARGUMENT_PRESENT(lpPath) ) {
  147. InitOemString(&OemString,lpPath);
  148. Status = RtlOemStringToUnicodeString(&xlpPath,&OemString,TRUE);
  149. if ( !NT_SUCCESS(Status) ) {
  150. if ( ARGUMENT_PRESENT(lpExtension) ) {
  151. RtlFreeUnicodeString(&xlpExtension);
  152. }
  153. BaseSetLastNTError(Status);
  154. return 0;
  155. }
  156. } else {
  157. xlpPath.Buffer = NULL;
  158. }
  159. xlpBuffer = RtlAllocateHeap(RtlProcessHeap(), 0,nBufferLength<<1);
  160. if ( !xlpBuffer ) {
  161. BaseSetLastNTError(STATUS_NO_MEMORY);
  162. goto bail0;
  163. }
  164. if(NtCurrentTeb()->Vdm) {
  165. ReturnValue = DPM_SearchPathW(
  166. xlpPath.Buffer,
  167. Unicode->Buffer,
  168. xlpExtension.Buffer,
  169. nBufferLength,
  170. xlpBuffer,
  171. FilePartPtr
  172. );
  173. } else {
  174. ReturnValue = SearchPathW(
  175. xlpPath.Buffer,
  176. Unicode->Buffer,
  177. xlpExtension.Buffer,
  178. nBufferLength,
  179. xlpBuffer,
  180. FilePartPtr
  181. );
  182. }
  183. #ifdef FE_SB
  184. if ( ReturnValue ) {
  185. RtlInitUnicodeString(&UnicodeString,xlpBuffer);
  186. ReturnValue = RtlUnicodeStringToOemSize(&UnicodeString) - 1;
  187. #endif
  188. if (ReturnValue && ReturnValue <= nBufferLength ) {
  189. #ifndef FE_SB
  190. RtlInitUnicodeString(&UnicodeString,xlpBuffer);
  191. #endif
  192. OemString.MaximumLength = (USHORT)(nBufferLength+1);
  193. OemString.Buffer = lpBuffer;
  194. Status = RtlUnicodeStringToOemString(&OemString,&UnicodeString,FALSE);
  195. if ( !NT_SUCCESS(Status) ) {
  196. BaseSetLastNTError(Status);
  197. ReturnValue = 0;
  198. } else {
  199. if ( ARGUMENT_PRESENT(lpFilePart) ) {
  200. if ( FilePart == NULL ) {
  201. *lpFilePart = NULL;
  202. } else {
  203. *lpFilePart = (LPSTR)(FilePart - xlpBuffer);
  204. *lpFilePart = *lpFilePart + (DWORD)lpBuffer;
  205. }
  206. }
  207. }
  208. }
  209. #ifdef FE_SB
  210. }
  211. #endif
  212. RtlFreeHeap(RtlProcessHeap(), 0,xlpBuffer);
  213. bail0:
  214. if ( ARGUMENT_PRESENT(lpExtension) ) {
  215. RtlFreeUnicodeString(&xlpExtension);
  216. }
  217. if ( ARGUMENT_PRESENT(lpPath) ) {
  218. RtlFreeUnicodeString(&xlpPath);
  219. }
  220. return ReturnValue;
  221. }
  222. DWORD
  223. APIENTRY
  224. GetTempPathOem(
  225. DWORD nBufferLength,
  226. LPSTR lpBuffer
  227. )
  228. /*++
  229. Routine Description:
  230. OEM thunk to GetTempPathW
  231. --*/
  232. {
  233. OEM_STRING OemString;
  234. UNICODE_STRING UnicodeString;
  235. NTSTATUS Status;
  236. UnicodeString.MaximumLength = (USHORT)((nBufferLength<<1)+sizeof(UNICODE_NULL));
  237. UnicodeString.Buffer = RtlAllocateHeap(
  238. RtlProcessHeap(), 0,
  239. UnicodeString.MaximumLength
  240. );
  241. if ( !UnicodeString.Buffer ) {
  242. BaseSetLastNTError(STATUS_NO_MEMORY);
  243. return 0;
  244. }
  245. if(NtCurrentTeb()->Vdm) {
  246. UnicodeString.Length = (USHORT)DPM_GetTempPathW(
  247. (DWORD)(UnicodeString.MaximumLength-sizeof(UNICODE_NULL))/2,
  248. UnicodeString.Buffer
  249. )*2;
  250. } else {
  251. UnicodeString.Length = (USHORT)GetTempPathW(
  252. (DWORD)(UnicodeString.MaximumLength-sizeof(UNICODE_NULL))/2,
  253. UnicodeString.Buffer
  254. )*2;
  255. }
  256. if ( UnicodeString.Length > (USHORT)(UnicodeString.MaximumLength-sizeof(UNICODE_NULL)) ) {
  257. RtlFreeHeap(RtlProcessHeap(), 0,UnicodeString.Buffer);
  258. return UnicodeString.Length>>1;
  259. }
  260. OemString.Buffer = lpBuffer;
  261. OemString.MaximumLength = (USHORT)(nBufferLength+1);
  262. Status = RtlUnicodeStringToOemString(&OemString,&UnicodeString,FALSE);
  263. RtlFreeHeap(RtlProcessHeap(), 0,UnicodeString.Buffer);
  264. if ( !NT_SUCCESS(Status) ) {
  265. BaseSetLastNTError(Status);
  266. return 0;
  267. }
  268. return OemString.Length;
  269. }
  270. UINT
  271. APIENTRY
  272. GetTempFileNameOem(
  273. LPCSTR lpPathName,
  274. LPCSTR lpPrefixString,
  275. UINT uUnique,
  276. LPSTR lpTempFileName
  277. )
  278. /*++
  279. Routine Description:
  280. Oem thunk to GetTempFileNameW
  281. --*/
  282. {
  283. PUNICODE_STRING Unicode;
  284. UNICODE_STRING UnicodePrefix;
  285. OEM_STRING OemString;
  286. NTSTATUS Status;
  287. UINT ReturnValue;
  288. UNICODE_STRING UnicodeResult;
  289. Unicode = &NtCurrentTeb()->StaticUnicodeString;
  290. InitOemString(&OemString,lpPathName);
  291. Status = RtlOemStringToUnicodeString(Unicode,&OemString,FALSE);
  292. if ( !NT_SUCCESS(Status) ) {
  293. if ( Status == STATUS_BUFFER_OVERFLOW ) {
  294. SetLastError(ERROR_FILENAME_EXCED_RANGE);
  295. } else {
  296. BaseSetLastNTError(Status);
  297. }
  298. return 0;
  299. }
  300. InitOemString(&OemString,lpPrefixString);
  301. Status = RtlOemStringToUnicodeString(&UnicodePrefix,&OemString,TRUE);
  302. if ( !NT_SUCCESS(Status) ) {
  303. BaseSetLastNTError(Status);
  304. return 0;
  305. }
  306. UnicodeResult.MaximumLength = (USHORT)((MAX_PATH<<1)+sizeof(UNICODE_NULL));
  307. UnicodeResult.Buffer = RtlAllocateHeap(RtlProcessHeap(), 0,UnicodeResult.MaximumLength);
  308. if ( !UnicodeResult.Buffer ) {
  309. SetLastError(ERROR_NOT_ENOUGH_MEMORY);
  310. RtlFreeUnicodeString(&UnicodePrefix);
  311. return 0;
  312. }
  313. if(NtCurrentTeb()->Vdm) {
  314. ReturnValue = DPM_GetTempFileNameW(
  315. Unicode->Buffer,
  316. UnicodePrefix.Buffer,
  317. uUnique,
  318. UnicodeResult.Buffer
  319. );
  320. } else {
  321. ReturnValue = GetTempFileNameW(
  322. Unicode->Buffer,
  323. UnicodePrefix.Buffer,
  324. uUnique,
  325. UnicodeResult.Buffer
  326. );
  327. }
  328. if ( ReturnValue ) {
  329. RtlInitUnicodeString(&UnicodeResult,UnicodeResult.Buffer);
  330. OemString.Buffer = lpTempFileName;
  331. OemString.MaximumLength = MAX_PATH+1;
  332. Status = RtlUnicodeStringToOemString(&OemString,&UnicodeResult,FALSE);
  333. if ( !NT_SUCCESS(Status) ) {
  334. BaseSetLastNTError(Status);
  335. ReturnValue = 0;
  336. }
  337. }
  338. RtlFreeUnicodeString(&UnicodePrefix);
  339. RtlFreeHeap(RtlProcessHeap(), 0,UnicodeResult.Buffer);
  340. return ReturnValue;
  341. }
  342. #if 0 // unused
  343. BOOL
  344. WINAPI
  345. CreateProcessOem(
  346. LPCSTR lpApplicationName,
  347. LPCSTR lpCommandLine,
  348. LPSECURITY_ATTRIBUTES lpProcessAttributes,
  349. LPSECURITY_ATTRIBUTES lpThreadAttributes,
  350. BOOL bInheritHandles,
  351. DWORD dwCreationFlags,
  352. LPVOID lpEnvironment,
  353. LPSTR lpCurrentDirectory,
  354. LPSTARTUPINFOA lpStartupInfo,
  355. LPPROCESS_INFORMATION lpProcessInformation
  356. )
  357. /*++
  358. OEM thunk to CreateProcessW
  359. --*/
  360. {
  361. NTSTATUS Status;
  362. PUNICODE_STRING CommandLine;
  363. UNICODE_STRING ApplicationName;
  364. UNICODE_STRING CurrentDirectory;
  365. STARTUPINFOW StartupInfo;
  366. OEM_STRING OemString;
  367. UNICODE_STRING Unicode;
  368. UNICODE_STRING DynamicCommandLine;
  369. BOOL ReturnStatus;
  370. CommandLine = &NtCurrentTeb()->StaticUnicodeString;
  371. if (ARGUMENT_PRESENT (lpCommandLine)) {
  372. InitOemString(&OemString,lpCommandLine);
  373. if ( OemString.Length<<1 < NtCurrentTeb()->StaticUnicodeString.MaximumLength ) {
  374. DynamicCommandLine.Buffer = NULL;
  375. Status = RtlOemStringToUnicodeString(CommandLine,&OemString,FALSE);
  376. if ( !NT_SUCCESS(Status) ) {
  377. BaseSetLastNTError(Status);
  378. return FALSE;
  379. }
  380. } else {
  381. Status = RtlOemStringToUnicodeString(&DynamicCommandLine,&OemString,TRUE);
  382. if ( !NT_SUCCESS(Status) ) {
  383. BaseSetLastNTError(Status);
  384. return FALSE;
  385. }
  386. }
  387. } else {
  388. DynamicCommandLine.Buffer = NULL;
  389. CommandLine->Buffer = NULL;
  390. }
  391. ApplicationName.Buffer = NULL;
  392. ApplicationName.Buffer = NULL;
  393. CurrentDirectory.Buffer = NULL;
  394. RtlMoveMemory(&StartupInfo,lpStartupInfo,sizeof(*lpStartupInfo));
  395. ASSERT(sizeof(StartupInfo) == sizeof(*lpStartupInfo));
  396. StartupInfo.lpReserved = NULL;
  397. StartupInfo.lpDesktop = NULL;
  398. StartupInfo.lpTitle = NULL;
  399. try {
  400. if (ARGUMENT_PRESENT(lpApplicationName)) {
  401. InitOemString(&OemString,lpApplicationName);
  402. Status = RtlOemStringToUnicodeString(&ApplicationName,&OemString,TRUE);
  403. if ( !NT_SUCCESS(Status) ) {
  404. BaseSetLastNTError(Status);
  405. ReturnStatus = FALSE;
  406. goto tryexit;
  407. }
  408. }
  409. if (ARGUMENT_PRESENT(lpCurrentDirectory)) {
  410. InitOemString(&OemString,lpCurrentDirectory);
  411. Status = RtlOemStringToUnicodeString(&CurrentDirectory,&OemString,TRUE);
  412. if ( !NT_SUCCESS(Status) ) {
  413. BaseSetLastNTError(Status);
  414. ReturnStatus = FALSE;
  415. goto tryexit;
  416. }
  417. }
  418. if (ARGUMENT_PRESENT(lpStartupInfo->lpReserved)) {
  419. InitOemString(&OemString,lpStartupInfo->lpReserved);
  420. Unicode.MaximumLength = (USHORT)RtlOemStringToUnicodeSize(&OemString) ;
  421. StartupInfo.lpReserved = RtlAllocateHeap(RtlProcessHeap(), 0, Unicode.MaximumLength);
  422. if ( !StartupInfo.lpReserved ) {
  423. BaseSetLastNTError(STATUS_NO_MEMORY);
  424. ReturnStatus = FALSE;
  425. goto tryexit;
  426. }
  427. Unicode.Buffer = StartupInfo.lpReserved;
  428. Status = RtlOemStringToUnicodeString(&Unicode,&OemString,FALSE);
  429. if ( !NT_SUCCESS(Status) ) {
  430. BaseSetLastNTError(Status);
  431. ReturnStatus = FALSE;
  432. goto tryexit;
  433. }
  434. }
  435. if (ARGUMENT_PRESENT(lpStartupInfo->lpDesktop)) {
  436. InitOemString(&OemString,lpStartupInfo->lpDesktop);
  437. Unicode.MaximumLength = (USHORT)RtlOemStringToUnicodeSize(&OemString) ;
  438. StartupInfo.lpDesktop = RtlAllocateHeap(RtlProcessHeap(), 0, Unicode.MaximumLength);
  439. if ( !StartupInfo.lpDesktop ) {
  440. BaseSetLastNTError(STATUS_NO_MEMORY);
  441. ReturnStatus = FALSE;
  442. goto tryexit;
  443. }
  444. Unicode.Buffer = StartupInfo.lpDesktop;
  445. Status = RtlOemStringToUnicodeString(&Unicode,&OemString,FALSE);
  446. if ( !NT_SUCCESS(Status) ) {
  447. BaseSetLastNTError(Status);
  448. ReturnStatus = FALSE;
  449. goto tryexit;
  450. }
  451. }
  452. if (ARGUMENT_PRESENT(lpStartupInfo->lpTitle)) {
  453. InitOemString(&OemString,lpStartupInfo->lpTitle);
  454. Unicode.MaximumLength = (USHORT)RtlOemStringToUnicodeSize(&OemString) ;
  455. StartupInfo.lpTitle = RtlAllocateHeap(RtlProcessHeap(), 0, Unicode.MaximumLength);
  456. if ( !StartupInfo.lpTitle ) {
  457. BaseSetLastNTError(STATUS_NO_MEMORY);
  458. ReturnStatus = FALSE;
  459. goto tryexit;
  460. }
  461. Unicode.Buffer = StartupInfo.lpTitle;
  462. Status = RtlOemStringToUnicodeString(&Unicode,&OemString,FALSE);
  463. if ( !NT_SUCCESS(Status) ) {
  464. BaseSetLastNTError(Status);
  465. ReturnStatus = FALSE;
  466. goto tryexit;
  467. }
  468. }
  469. ReturnStatus = CreateProcessW(
  470. ApplicationName.Buffer,
  471. DynamicCommandLine.Buffer ? DynamicCommandLine.Buffer : CommandLine->Buffer,
  472. lpProcessAttributes,
  473. lpThreadAttributes,
  474. bInheritHandles,
  475. dwCreationFlags,
  476. lpEnvironment,
  477. CurrentDirectory.Buffer,
  478. &StartupInfo,
  479. lpProcessInformation
  480. );
  481. tryexit:;
  482. } finally {
  483. if (DynamicCommandLine.Buffer) {
  484. RtlFreeUnicodeString(&DynamicCommandLine);
  485. DynamicCommandLine.Buffer = NULL;
  486. }
  487. if (ApplicationName.Buffer) {
  488. RtlFreeUnicodeString(&ApplicationName);
  489. ApplicationName.Buffer = NULL;
  490. }
  491. if (CurrentDirectory.Buffer) {
  492. RtlFreeUnicodeString(&CurrentDirectory);
  493. CurrentDirectory.Buffer = NULL;
  494. }
  495. if (StartupInfo.lpReserved) {
  496. RtlFreeHeap(RtlProcessHeap(), 0,StartupInfo.lpReserved);
  497. StartupInfo.lpReserved = NULL;
  498. }
  499. if (StartupInfo.lpDesktop) {
  500. RtlFreeHeap(RtlProcessHeap(), 0,StartupInfo.lpDesktop);
  501. StartupInfo.lpDesktop = NULL;
  502. }
  503. if (StartupInfo.lpTitle) {
  504. RtlFreeHeap(RtlProcessHeap(), 0,StartupInfo.lpTitle);
  505. StartupInfo.lpTitle = NULL;
  506. }
  507. }
  508. return ReturnStatus;
  509. }
  510. #endif
  511. DWORD
  512. WINAPI
  513. GetEnvironmentVariableOem(
  514. LPSTR lpName,
  515. LPSTR lpBuffer,
  516. DWORD nSize
  517. )
  518. /*++
  519. OEM thunk to GetEnvironmentVariableW
  520. --*/
  521. {
  522. NTSTATUS Status;
  523. UNICODE_STRING Unicode;
  524. OEM_STRING OemString;
  525. ANSI_STRING Name, Buffer;
  526. DWORD ReturnValue;
  527. Unicode.Buffer = NULL;
  528. Name.Buffer = NULL;
  529. Buffer.Buffer = NULL;
  530. ReturnValue = 0;
  531. try {
  532. InitOemString(&OemString,lpName);
  533. Status = RtlOemStringToUnicodeString(&Unicode, &OemString, TRUE);
  534. if ( !NT_SUCCESS(Status) ) {
  535. if ( Status == STATUS_BUFFER_OVERFLOW ) {
  536. SetLastError(ERROR_FILENAME_EXCED_RANGE);
  537. } else {
  538. BaseSetLastNTError(Status);
  539. }
  540. return 0;
  541. }
  542. Status = RtlUnicodeStringToAnsiString( &Name, &Unicode, TRUE );
  543. if (!NT_SUCCESS( Status )) {
  544. BaseSetLastNTError( Status );
  545. goto try_exit;
  546. }
  547. Buffer.MaximumLength = (USHORT)nSize;
  548. Buffer.Buffer = (PCHAR)
  549. RtlAllocateHeap( RtlProcessHeap(), 0, Buffer.MaximumLength );
  550. if (Buffer.Buffer == NULL) {
  551. BaseSetLastNTError( STATUS_NO_MEMORY );
  552. goto try_exit;
  553. }
  554. ReturnValue = GetEnvironmentVariableA( Name.Buffer,
  555. Buffer.Buffer,
  556. Buffer.MaximumLength
  557. );
  558. if (ReturnValue != 0) {
  559. if ( ReturnValue < nSize ) {
  560. Buffer.Length = (USHORT)ReturnValue;
  561. RtlFreeUnicodeString( &Unicode );
  562. Unicode.Buffer = NULL;
  563. Status = RtlAnsiStringToUnicodeString( &Unicode, &Buffer, TRUE );
  564. if (!NT_SUCCESS( Status )) {
  565. BaseSetLastNTError( Status );
  566. ReturnValue = 0;
  567. }
  568. OemString.Buffer = lpBuffer;
  569. OemString.MaximumLength = (USHORT)nSize;
  570. Status = RtlUnicodeStringToOemString( &OemString, &Unicode, FALSE );
  571. if (!NT_SUCCESS( Status )) {
  572. BaseSetLastNTError( Status );
  573. ReturnValue = 0;
  574. }
  575. }
  576. }
  577. try_exit:;
  578. } finally {
  579. if (Unicode.Buffer != NULL) {
  580. RtlFreeUnicodeString( &Unicode );
  581. }
  582. if (Name.Buffer != NULL) {
  583. RtlFreeAnsiString( &Name );
  584. }
  585. if (Buffer.Buffer != NULL) {
  586. RtlFreeHeap( RtlProcessHeap(), 0, Buffer.Buffer );
  587. }
  588. }
  589. return ( ReturnValue );
  590. }
  591. BOOL
  592. WINAPI
  593. SetEnvironmentVariableOem(
  594. LPSTR lpName,
  595. LPSTR lpValue
  596. )
  597. /*++
  598. OEM thunk to SetEnvironmentVariableW
  599. --*/
  600. {
  601. NTSTATUS Status;
  602. UNICODE_STRING Unicode;
  603. OEM_STRING OemString;
  604. ANSI_STRING Name, Value;
  605. DWORD ReturnValue;
  606. Unicode.Buffer = NULL;
  607. Name.Buffer = NULL;
  608. Value.Buffer = NULL;
  609. ReturnValue = 0;
  610. try {
  611. InitOemString(&OemString, lpName);
  612. Status = RtlOemStringToUnicodeString(&Unicode, &OemString, TRUE);
  613. if ( !NT_SUCCESS(Status) ) {
  614. if ( Status == STATUS_BUFFER_OVERFLOW ) {
  615. SetLastError(ERROR_FILENAME_EXCED_RANGE);
  616. } else {
  617. BaseSetLastNTError(Status);
  618. }
  619. return 0;
  620. }
  621. Status = RtlUnicodeStringToAnsiString( &Name, &Unicode, TRUE );
  622. if (!NT_SUCCESS( Status )) {
  623. BaseSetLastNTError( Status );
  624. goto try_exit;
  625. }
  626. RtlFreeUnicodeString( &Unicode );
  627. Unicode.Buffer = NULL;
  628. if (ARGUMENT_PRESENT( lpValue )) {
  629. InitOemString(&OemString, lpValue);
  630. Status = RtlOemStringToUnicodeString(&Unicode, &OemString, TRUE);
  631. if ( !NT_SUCCESS(Status) ) {
  632. if ( Status == STATUS_BUFFER_OVERFLOW ) {
  633. SetLastError(ERROR_FILENAME_EXCED_RANGE);
  634. } else {
  635. BaseSetLastNTError(Status);
  636. }
  637. return 0;
  638. }
  639. Status = RtlUnicodeStringToAnsiString( &Value, &Unicode, TRUE );
  640. if (!NT_SUCCESS( Status )) {
  641. BaseSetLastNTError( Status );
  642. goto try_exit;
  643. }
  644. }
  645. ReturnValue = SetEnvironmentVariableA( Name.Buffer,
  646. Value.Buffer
  647. );
  648. try_exit:;
  649. } finally {
  650. if (Unicode.Buffer != NULL) {
  651. RtlFreeUnicodeString( &Unicode );
  652. }
  653. if (Name.Buffer != NULL) {
  654. RtlFreeAnsiString( &Name );
  655. }
  656. if (Value.Buffer != NULL) {
  657. RtlFreeAnsiString( &Value );
  658. }
  659. }
  660. return ( ReturnValue );
  661. }
  662. DWORD
  663. WINAPI
  664. ExpandEnvironmentStringsOem(
  665. LPSTR lpSrc,
  666. LPSTR lpDst,
  667. DWORD cchDst
  668. )
  669. /*++
  670. OEM thunk to ExpandEnvironmentStrings
  671. --*/
  672. {
  673. NTSTATUS Status;
  674. UNICODE_STRING Unicode;
  675. OEM_STRING OemString;
  676. ANSI_STRING Name, Value;
  677. DWORD ReturnValue;
  678. if (!ARGUMENT_PRESENT(lpSrc)) {
  679. SetLastError(ERROR_INVALID_PARAMETER);
  680. return 0;
  681. }
  682. Unicode.Buffer = NULL;
  683. Name.Buffer = NULL;
  684. Value.Buffer = NULL;
  685. ReturnValue = 0;
  686. try {
  687. InitOemString(&OemString, lpSrc);
  688. Status = RtlOemStringToUnicodeString(&Unicode, &OemString, TRUE);
  689. if ( !NT_SUCCESS(Status) ) {
  690. if ( Status == STATUS_BUFFER_OVERFLOW ) {
  691. SetLastError(ERROR_FILENAME_EXCED_RANGE);
  692. } else {
  693. BaseSetLastNTError(Status);
  694. }
  695. return 0;
  696. }
  697. Status = RtlUnicodeStringToAnsiString( &Name, &Unicode, TRUE );
  698. if (!NT_SUCCESS( Status )) {
  699. BaseSetLastNTError( Status );
  700. goto try_exit;
  701. }
  702. RtlFreeUnicodeString( &Unicode );
  703. Unicode.Buffer = NULL;
  704. ReturnValue = ExpandEnvironmentStrings( Name.Buffer,
  705. lpDst,
  706. cchDst
  707. );
  708. if (ReturnValue != 0 && ReturnValue <= cchDst) {
  709. RtlInitString(&Value, lpDst);
  710. Status = RtlAnsiStringToUnicodeString(&Unicode, &Value, TRUE);
  711. if ( !NT_SUCCESS(Status) ) {
  712. if ( Status == STATUS_BUFFER_OVERFLOW ) {
  713. SetLastError(ERROR_FILENAME_EXCED_RANGE);
  714. } else {
  715. BaseSetLastNTError(Status);
  716. }
  717. goto try_exit;
  718. }
  719. Status = RtlUnicodeStringToOemString( &Value, &Unicode, TRUE );
  720. if (!NT_SUCCESS( Status )) {
  721. BaseSetLastNTError( Status );
  722. goto try_exit;
  723. }
  724. }
  725. try_exit:;
  726. } finally {
  727. if (Unicode.Buffer != NULL) {
  728. RtlFreeUnicodeString( &Unicode );
  729. }
  730. if (Name.Buffer != NULL) {
  731. RtlFreeAnsiString( &Name );
  732. }
  733. if (Value.Buffer != NULL) {
  734. RtlFreeAnsiString( &Value );
  735. }
  736. }
  737. return ( ReturnValue );
  738. }
  739. UINT
  740. WINAPI
  741. GetShortPathNameOem(
  742. LPSTR lpSrc,
  743. LPSTR lpDst,
  744. DWORD cchDst
  745. )
  746. /*++
  747. OEM thunk to GetShortPathNameW
  748. --*/
  749. {
  750. UNICODE_STRING UString = {0}, UStringRet;
  751. OEM_STRING OemString;
  752. NTSTATUS Status;
  753. LPWSTR lpDstW = NULL;
  754. DWORD ReturnValue = 0;
  755. if (lpSrc == NULL) {
  756. SetLastError(ERROR_INVALID_PARAMETER);
  757. return 0;
  758. }
  759. try {
  760. InitOemString(&OemString, lpSrc);
  761. Status = RtlOemStringToUnicodeString(&UString,
  762. &OemString,
  763. TRUE
  764. );
  765. if (!NT_SUCCESS(Status)) {
  766. BaseSetLastNTError(Status);
  767. leave;
  768. }
  769. if (ARGUMENT_PRESENT(lpDst) && cchDst > 0) {
  770. lpDstW = RtlAllocateHeap(RtlProcessHeap(), 0,
  771. cchDst * sizeof(WCHAR)
  772. );
  773. if (lpDstW == NULL) {
  774. SetLastError(ERROR_NOT_ENOUGH_MEMORY);
  775. leave;
  776. }
  777. } else {
  778. lpDstW = NULL;
  779. cchDst = 0;
  780. }
  781. if(NtCurrentTeb()->Vdm) {
  782. ReturnValue = DPM_GetShortPathNameW(UString.Buffer,
  783. lpDstW,
  784. cchDst
  785. );
  786. } else {
  787. ReturnValue = GetShortPathNameW(UString.Buffer,
  788. lpDstW,
  789. cchDst
  790. );
  791. }
  792. if (ReturnValue != 0 && ReturnValue <= cchDst) {
  793. if (ARGUMENT_PRESENT(lpDst)) {
  794. OemString.Buffer = lpDst;
  795. OemString.MaximumLength = (USHORT)(cchDst * sizeof(WCHAR));
  796. UStringRet.Buffer = lpDstW;
  797. UStringRet.Length = (USHORT)(ReturnValue * sizeof(WCHAR));
  798. Status = RtlUnicodeStringToOemString(&OemString,
  799. &UStringRet,
  800. FALSE
  801. );
  802. if (!NT_SUCCESS(Status)) {
  803. BaseSetLastNTError(Status);
  804. ReturnValue = 0;
  805. leave;
  806. }
  807. }
  808. }
  809. } finally {
  810. RtlFreeUnicodeString(&UString);
  811. if (lpDstW)
  812. RtlFreeHeap(RtlProcessHeap(), 0, lpDstW);
  813. }
  814. return ReturnValue;
  815. }