1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.Diagnostics;
namespace System.Runtime.InteropServices
{
public static partial class Marshal
{
// Win32 has the concept of Atoms, where a pointer can either be a pointer
// or an int. If it's less than 64K, this is guaranteed to NOT be a
// pointer since the bottom 64K bytes are reserved in a process' page table.
// We should be careful about deallocating this stuff. Extracted to
// a function to avoid C# problems with lack of support for IntPtr.
// We have 2 of these methods for slightly different semantics for NULL.
private static bool IsWin32Atom(IntPtr ptr)
{
const long HIWORDMASK = unchecked((long)0xffffffffffff0000L);
long lPtr = (long)ptr;
return 0 == (lPtr & HIWORDMASK);
}
internal static unsafe int StringToAnsiString(string s, byte* buffer, int bufferLength, bool bestFit = false, bool throwOnUnmappableChar = false)
{
Debug.Assert(bufferLength >= (s.Length + 1) * SystemMaxDBCSCharSize, "Insufficient buffer length passed to StringToAnsiString");
int nb;
uint flags = bestFit ? 0 : Interop.Kernel32.WC_NO_BEST_FIT_CHARS;
uint defaultCharUsed = 0;
fixed (char* pwzChar = s)
{
nb = Interop.Kernel32.WideCharToMultiByte(
Interop.Kernel32.CP_ACP,
flags,
pwzChar,
s.Length,
buffer,
bufferLength,
IntPtr.Zero,
throwOnUnmappableChar ? new IntPtr(&defaultCharUsed) : IntPtr.Zero);
}
if (defaultCharUsed != 0)
{
throw new ArgumentException(SR.Interop_Marshal_Unmappable_Char);
}
buffer[nb] = 0;
return nb;
}
}
}
|