Skip to content

Commit 86374ff

Browse files
committed
GetCPInfoEx functions, winnls.h line 1568
1 parent c333796 commit 86374ff

File tree

4 files changed

+197
-0
lines changed

4 files changed

+197
-0
lines changed

src-native/THNETII.WinApi.Headers.WinNls/WinNlsFunctions.cs

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,5 +238,84 @@ public static extern bool GetCPInfo(
238238
out CPINFO lpCPInfo
239239
);
240240
#endregion
241+
// C:\Program Files (x86)\Windows Kits\10\Include\10.0.17134.0\um\WinNls.h, line 1548
242+
#region GetCPInfoExA function
243+
/// <inheritdoc cref="GetCPInfoEx"/>
244+
[Obsolete("Use Unicode. The information in this structure cannot represent all encodings accuratedly and may be unreliable on many machines.")]
245+
[DllImport(Kernel32, CallingConvention = CallingConvention.Winapi, SetLastError = true, CharSet = CharSet.Ansi)]
246+
[return: MarshalAs(UnmanagedType.Bool)]
247+
public static extern bool GetCPInfoExA(
248+
[In] int CodePage,
249+
[In] int dwFlags,
250+
out CPINFOEXA lpCPInfoEx
251+
);
252+
#endregion
253+
// C:\Program Files (x86)\Windows Kits\10\Include\10.0.17134.0\um\WinNls.h, line 1556
254+
#region GetCPInfoExW function
255+
/// <inheritdoc cref="GetCPInfoEx"/>
256+
[Obsolete("Use Unicode. The information in this structure cannot represent all encodings accuratedly and may be unreliable on many machines.")]
257+
[DllImport(Kernel32, CallingConvention = CallingConvention.Winapi, SetLastError = true, CharSet = CharSet.Unicode)]
258+
[return: MarshalAs(UnmanagedType.Bool)]
259+
public static extern bool GetCPInfoExW(
260+
[In] int CodePage,
261+
[In] int dwFlags,
262+
out CPINFOEXW lpCPInfoEx
263+
);
264+
#endregion
265+
// C:\Program Files (x86)\Windows Kits\10\Include\10.0.17134.0\um\WinNls.h, line 1564
266+
#region GetCPInfoEx function
267+
/// <summary>
268+
/// Retrieves information about any valid installed or available code page.
269+
/// </summary>
270+
/// <param name="CodePage">
271+
/// Identifier for the code page for which to retrieve information. The application can specify the code page identifier for any installed or available code page, or one of the following predefined values. See <a href="https://docs.microsoft.com/windows/desktop/Intl/code-page-identifiers">Code Page Identifiers</a> for a list of identifiers for ANSI and other code pages.
272+
/// <list type="table">
273+
/// <listheader><term>Value</term><description>Meaning</description></listheader>
274+
/// <item><term><see cref="CP_ACP"/></term><description>Use the system default Windows ANSI code page.</description></item>
275+
/// <item><term><see cref="CP_MACCP"/></term><description>Use the system default Macintosh code page.</description></item>
276+
/// <item><term><see cref="CP_OEMCP"/></term><description>Use the system default OEM code page.</description></item>
277+
/// <item><term><see cref="CP_THREAD_ACP"/></term><description>Use the current thread's ANSI code page.</description></item>
278+
/// </list>
279+
/// </param>
280+
/// <param name="dwFlags">Reserved; must be <c>0</c> (zero).</param>
281+
/// <param name="lpCPInfoEx">A <see cref="CPINFOEX"/> structure that receives information about the code page.</param>
282+
/// <returns>
283+
/// Returns <see langword="true"/> if successful, or <see langword="false"/> otherwise. To get extended error information, the application can call <see cref="GetLastError"/>, which can return one of the following error codes:
284+
/// <list type="table">
285+
/// <item><term><see cref="ERROR_INVALID_PARAMETER"/></term><description>Any of the parameter values was invalid.</description></item>
286+
/// </list>
287+
/// </returns>
288+
/// <remarks>
289+
/// <para>The information retrieved in the <see cref="CPINFOEX"/> structure is not always useful for all code pages. To determine buffer sizes, for example, the application should call <see cref="MultiByteToWideChar"/> or <see cref="WideCharToMultiByte"/> to request an accurate buffer size. If <see cref="CPINFOEX"/> settings indicate that a lead byte exists, the conversion function does not necessarily handle lead bytes differently, for example, in the case of a missing or illegal trail byte.</para>
290+
/// <para>
291+
/// <list type="table">
292+
/// <listheader><term>Requirements</term></listheader>
293+
/// <item><term><strong>Minimum supported client:</strong></term><description>Windows 2000 Professional [desktop apps | UWP apps]</description></item>
294+
/// <item><term><strong>Minimum supported server:</strong></term><description>Windows 2000 Server [desktop apps | UWP apps]</description></item>
295+
/// </list>
296+
/// </para>
297+
/// <para>Microsoft Docs page: <a href="https://docs.microsoft.com/en-us/windows/win32/api/winnls/nf-winnls-getcpinfoexw">GetCPInfoExW function</a></para>
298+
/// </remarks>
299+
/// <exception cref="DllNotFoundException">The native library containg the function could not be found.</exception>
300+
/// <exception cref="EntryPointNotFoundException">Unable to find the entry point for the function in the native library.</exception>
301+
/// <seealso cref="CPINFOEX"/>
302+
/// <seealso cref="GetACP"/>
303+
/// <seealso cref="GetCPInfo"/>
304+
/// <seealso cref="GetOEMCP"/>
305+
/// <seealso href="https://docs.microsoft.com/windows/desktop/Intl/national-language-support">National Language Support</seealso>
306+
/// <seealso href="https://docs.microsoft.com/windows/desktop/Intl/national-language-support-functions">National Language Support Functions</seealso>
307+
[Obsolete("Use Unicode. The information in this structure cannot represent all encodings accuratedly and may be unreliable on many machines.")]
308+
[DllImport(Kernel32, CallingConvention = CallingConvention.Winapi, SetLastError = true
309+
#if !NETSTANDARD1_3
310+
, CharSet = CharSet.Auto
311+
#endif
312+
)]
313+
[return: MarshalAs(UnmanagedType.Bool)]
314+
public static extern bool GetCPInfoEx(
315+
[In] int CodePage,
316+
[In] int dwFlags,
317+
out CPINFOEX lpCPInfoEx
318+
);
319+
#endregion
241320
}
242321
}

test/THNETII.WinApi.Native.Test/THNETII.WinApi.Native.Test.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323
<ItemGroup>
2424
<ProjectReference Include="..\..\src-native\THNETII.WinApi.Constants.SCardErr\THNETII.WinApi.Constants.SCardErr.csproj" />
2525
<ProjectReference Include="..\..\src-native\THNETII.WinApi.Constants.WinError\THNETII.WinApi.Constants.WinError.csproj" />
26+
<ProjectReference Include="..\..\src-native\THNETII.WinApi.Constants.WinNls\THNETII.WinApi.Constants.WinNls.csproj">
27+
<PrivateAssets>All</PrivateAssets>
28+
</ProjectReference>
2629
<ProjectReference Include="..\..\src-native\THNETII.WinApi.Headers.ErrHandlingApi\THNETII.WinApi.Headers.ErrHandlingApi.csproj" />
2730
<ProjectReference Include="..\..\src-native\THNETII.WinApi.Headers.MinWinBase\THNETII.WinApi.Headers.MinWinBase.csproj" />
2831
<ProjectReference Include="..\..\src-native\THNETII.WinApi.Headers.MinWinDef\THNETII.WinApi.Headers.MinWinDef.csproj" />

test/THNETII.WinApi.Native.Test/WinNls.Test/GetCPInfo.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,16 @@
77

88
namespace THNETII.WinApi.Native.WinNls.Test
99
{
10+
using static WinNlsConstants;
1011
using static WinNlsFunctions;
1112

1213
public static class GetCPInfo
1314
{
1415
[TheoryWindowsOS]
16+
[InlineData(CP_ACP)]
17+
[InlineData(CP_MACCP)]
18+
[InlineData(CP_OEMCP)]
19+
[InlineData(CP_THREAD_ACP)]
1520
[InlineData(437)] // IBM437 OEM United States
1621
[InlineData(850)] // ibm850 OEM Multilingual Latin 1; Western European (DOS)
1722
[InlineData(1250)] // windows-1250 ANSI Central European; Central European (Windows)
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
using System;
2+
using System.ComponentModel;
3+
using System.Runtime.InteropServices;
4+
5+
using Xunit;
6+
7+
namespace THNETII.WinApi.Native.WinNls.Test
8+
{
9+
using static WinNlsConstants;
10+
using static WinNlsFunctions;
11+
12+
public static class GetCPInfoEx
13+
{
14+
[TheoryWindowsOS]
15+
[InlineData(CP_ACP)]
16+
[InlineData(CP_MACCP)]
17+
[InlineData(CP_OEMCP)]
18+
[InlineData(CP_THREAD_ACP)]
19+
[InlineData(437)] // IBM437 OEM United States
20+
[InlineData(850)] // ibm850 OEM Multilingual Latin 1; Western European (DOS)
21+
[InlineData(1250)] // windows-1250 ANSI Central European; Central European (Windows)
22+
[InlineData(1252)] // windows-1252 ANSI Latin 1; Western European (Windows)
23+
[InlineData(20127)] // us-ascii US-ASCII (7-bit)
24+
[InlineData(28591)] // iso-8859-1 ISO 8859-1 Latin 1; Western European (ISO)
25+
[InlineData(28605)] // iso-8859-15 ISO 8859-15 Latin 9
26+
[InlineData(65001)] // utf-8 Unicode (UTF-8)
27+
[Obsolete("Deprecated Win API")]
28+
public static void ReturnsTrueForCodePageAnsi(int codepage)
29+
{
30+
bool success = GetCPInfoExA(codepage, default, out var cpinfo);
31+
32+
if (!success)
33+
throw new Win32Exception(Marshal.GetLastWin32Error());
34+
35+
Assert.True(success);
36+
37+
_ = cpinfo.MaxCharSize;
38+
foreach (ref readonly byte defaultByte in cpinfo.DefaultChar)
39+
_ = defaultByte;
40+
foreach (ref readonly byte leadByte in cpinfo.LeadByte)
41+
_ = leadByte;
42+
_ = cpinfo.UnicodeDefaultChar;
43+
_ = cpinfo.CodePageName;
44+
}
45+
46+
[TheoryWindowsOS]
47+
[InlineData(CP_ACP)]
48+
[InlineData(CP_MACCP)]
49+
[InlineData(CP_OEMCP)]
50+
[InlineData(CP_THREAD_ACP)]
51+
[InlineData(437)] // IBM437 OEM United States
52+
[InlineData(850)] // ibm850 OEM Multilingual Latin 1; Western European (DOS)
53+
[InlineData(1250)] // windows-1250 ANSI Central European; Central European (Windows)
54+
[InlineData(1252)] // windows-1252 ANSI Latin 1; Western European (Windows)
55+
[InlineData(20127)] // us-ascii US-ASCII (7-bit)
56+
[InlineData(28591)] // iso-8859-1 ISO 8859-1 Latin 1; Western European (ISO)
57+
[InlineData(28605)] // iso-8859-15 ISO 8859-15 Latin 9
58+
[InlineData(65001)] // utf-8 Unicode (UTF-8)
59+
[Obsolete("Deprecated Win API")]
60+
public static void ReturnsTrueForCodePageUnicode(int codepage)
61+
{
62+
bool success = GetCPInfoExW(codepage, default, out var cpinfo);
63+
64+
if (!success)
65+
throw new Win32Exception(Marshal.GetLastWin32Error());
66+
67+
Assert.True(success);
68+
69+
_ = cpinfo.MaxCharSize;
70+
foreach (ref readonly byte defaultByte in cpinfo.DefaultChar)
71+
_ = defaultByte;
72+
foreach (ref readonly byte leadByte in cpinfo.LeadByte)
73+
_ = leadByte;
74+
_ = cpinfo.UnicodeDefaultChar;
75+
_ = cpinfo.CodePageName;
76+
}
77+
78+
[TheoryWindowsOS]
79+
[InlineData(CP_ACP)]
80+
[InlineData(CP_MACCP)]
81+
[InlineData(CP_OEMCP)]
82+
[InlineData(CP_THREAD_ACP)]
83+
[InlineData(437)] // IBM437 OEM United States
84+
[InlineData(850)] // ibm850 OEM Multilingual Latin 1; Western European (DOS)
85+
[InlineData(1250)] // windows-1250 ANSI Central European; Central European (Windows)
86+
[InlineData(1252)] // windows-1252 ANSI Latin 1; Western European (Windows)
87+
[InlineData(20127)] // us-ascii US-ASCII (7-bit)
88+
[InlineData(28591)] // iso-8859-1 ISO 8859-1 Latin 1; Western European (ISO)
89+
[InlineData(28605)] // iso-8859-15 ISO 8859-15 Latin 9
90+
[InlineData(65001)] // utf-8 Unicode (UTF-8)
91+
[Obsolete("Deprecated Win API")]
92+
public static void ReturnsTrueForCodePageAuto(int codepage)
93+
{
94+
bool success = GetCPInfoEx(codepage, default, out var cpinfo);
95+
96+
if (!success)
97+
throw new Win32Exception(Marshal.GetLastWin32Error());
98+
99+
Assert.True(success);
100+
101+
_ = cpinfo.MaxCharSize;
102+
foreach (ref readonly byte defaultByte in cpinfo.DefaultChar)
103+
_ = defaultByte;
104+
foreach (ref readonly byte leadByte in cpinfo.LeadByte)
105+
_ = leadByte;
106+
_ = cpinfo.UnicodeDefaultChar;
107+
_ = cpinfo.CodePageName;
108+
}
109+
}
110+
}

0 commit comments

Comments
 (0)