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.

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