SetFileApisToOEM function (Windows)

Switch View :
ScriptFree
SetFileApisToOEM function

Applies to: desktop apps only

Causes the file I/O functions for the process to use the OEM character set code page. This function is useful for 8-bit console input and output operations.

Syntax

void WINAPI SetFileApisToOEM(void);

Parameters

This function has no parameters.

Return value

This function does not return a value.

Remarks

The file I/O functions whose code page is set by SetFileApisToOEM are those functions exported by KERNEL32.DLL that accept or return a file name. SetFileApisToOEM sets the code page per process, rather than per thread or per computer.

The SetFileApisToOEM function is complemented by the SetFileApisToANSI function, which causes the same set of file I/O functions to use the ANSI character set code page.

The 8-bit console functions use the OEM code page by default. All other functions use the ANSI code page by default. This means that strings returned by the console functions may not be processed correctly by other functions, and vice versa. For example, if the FindFirstFileA function returns a string that contains certain extended ANSI characters, and the 8-bit console functions are set to use the OEM code page, then the WriteConsoleA function will not display the string properly.

Use the AreFileApisANSI function to determine which code page the set of file I/O functions is currently using. Use the SetConsoleCP and SetConsoleOutputCP functions to set the code page for the 8-bit console functions.

To solve the problem of code page incompatibility, it is best to use Unicode for console applications. Console applications that use Unicode are much more versatile than those that use 8-bit console functions. Barring that solution, a console application can call the SetFileApisToOEM function to cause the set of file I/O functions to use OEM character set strings rather than ANSI character set strings. Use the SetFileApisToANSI function to set those functions back to the ANSI code page.

When dealing with command lines, a console application should obtain the command line in Unicode form and then convert it to OEM form using the relevant character-to-OEM functions. Note also that the array in the argv parameter of the command-line main function contains ANSI character set strings in this case.

Requirements

Minimum supported client

Windows XP

Minimum supported server

Windows Server 2003

Header

WinBase.h (include Windows.h)

Library

Kernel32.lib

DLL

Kernel32.dll

See also

AreFileApisANSI
File Management Functions
FindFirstFileA
SetConsoleCP
SetConsoleOutputCP
SetFileApisToANSI
WriteConsoleA

 

 

Send comments about this topic to Microsoft

Build date: 4/17/2012

Community Content

Nam Jung Hyun
Platform Invoke Code Snippet (SetFileApisToOEM)
/// <summary>
/// Causes the file I/O functions for the process to use the OEM character set code page. This function is useful for 8-bit console input and output operations.
/// </summary>
/// <remarks>
/// The file I/O functions whose code page is set by SetFileApisToOEM are those functions exported by KERNEL32.DLL that accept or return a file name. SetFileApisToOEM sets the code page per process, rather than per thread or per computer.
/// The SetFileApisToOEM function is complemented by the SetFileApisToANSI function, which causes the same set of file I/O functions to use the ANSI character set code page.
/// The 8-bit console functions use the OEM code page by default. All other functions use the ANSI code page by default. This means that strings returned by the console functions may not be processed correctly by other functions, and vice versa. For example, if the FindFirstFileA function returns a string that contains certain extended ANSI characters, and the 8-bit console functions are set to use the OEM code page, then the WriteConsoleA function will not display the string properly.
/// Use the AreFileApisANSI function to determine which code page the set of file I/O functions is currently using. Use the SetConsoleCP and SetConsoleOutputCP functions to set the code page for the 8-bit console functions.
/// To solve the problem of code page incompatibility, it is best to use Unicode for console applications. Console applications that use Unicode are much more versatile than those that use 8-bit console functions. Barring that solution, a console application can call the SetFileApisToOEM function to cause the set of file I/O functions to use OEM character set strings rather than ANSI character set strings. Use the SetFileApisToANSI function to set those functions back to the ANSI code page.
/// When dealing with command lines, a console application should obtain the command line in Unicode form and then convert it to OEM form using the relevant character-to-OEM functions. Note also that the array in the argv parameter of the command-line main function contains ANSI character set strings in this case.
/// </remarks>
[DllImport("kernel32.dll", CharSet = CharSet.None, ExactSpelling = true, CallingConvention = CallingConvention.Winapi)]
static extern void SetFileApisToOEM();

Gideon7
8-bit console apps using the OEM codepage

Note: If you are writing/porting an 8-bit console application, in addition to calling SetFileApisToOem() you should also call setlocale(LC_ALL, ".OCP") followed by _setmbcp(_MB_CP_LOCALE).

Argv is encoded in the ANSI codepage, not the OEM codepage. This means that printf("%s\n", argv[i]) will display mangled characters outside of 7-bit ASCII. To fix call CharToOem(argv[i], szOem) and printf szOem.

Best Practice: To avoid these issues you should redesign your console application to use Unicode exclusively for all console input/output. #define UNICODE and #define _UNICODE.