Corrected more blittability problems

pull/60/head
David Hall 2019-06-07 11:51:47 -06:00
parent 68775cdb5b
commit da8b40b1b9
6 changed files with 86 additions and 40 deletions

View File

@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using Vanara.Extensions;
using Vanara.InteropServices;
using FILETIME = System.Runtime.InteropServices.ComTypes.FILETIME;
@ -2863,11 +2864,25 @@ namespace Vanara.PInvoke
[StructLayout(LayoutKind.Sequential)]
public struct FILE_ID_128
{
private ulong id0;
private ulong id1;
/// <summary>
/// <para>A byte array containing the 128 bit identifier.</para>
/// </summary>
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
public byte[] Identifier;
public byte[] Identifier
{
get
{
using (var pin = new PinnedObject(id0))
return ((IntPtr)pin).ToArray<byte>(16);
}
set
{
using (var pin = new PinnedObject(id0))
Marshal.Copy(value, 0, pin, 16);
}
}
}
/// <summary>
@ -3495,8 +3510,7 @@ namespace Vanara.PInvoke
public Smb2 Smb2;
[FieldOffset(0)]
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
public uint[] Reserved;
public Guid Reserved;
}
[StructLayout(LayoutKind.Sequential)]

View File

@ -5206,48 +5206,39 @@ namespace Vanara.PInvoke
public byte[] LeadByte;
}
/// <summary>Contains information about a code page. This structure is used by the <c>GetCPInfoEx</c> function.</summary>
// typedef struct _cpinfoex { UINT MaxCharSize; BYTE DefaultChar[MAX_DEFAULTCHAR]; BYTE LeadByte[MAX_LEADBYTES]; WCHAR
// UnicodeDefaultChar; UINT CodePage; TCHAR CodePageName[MAX_PATH];} CPINFOEX, *LPCPINFOEX; https://msdn.microsoft.com/en-us/library/windows/desktop/dd317781(v=vs.85).aspx
[PInvokeData("Winnls.h", MSDNShortId = "dd317781")]
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
/// <summary>Contains information about a code page. This structure is used by the GetCPInfoEx function.</summary>
/// <remarks>
/// <para>Lead bytes are unique to DBCS code pages that allow for more than 256 characters. A lead byte is the first byte of a 2-byte character in a DBCS. On each DBCS code page, the lead bytes occupy a specific range of byte values. This range is different for different code pages.</para>
/// <para>The lead byte information is not very helpful for most code pages, and is not even provided for many multi-byte encodings, for example, UTF-8 and GB18030. Your applications are discouraged from using this information to predict what the MultiByteToWideChar or WideCharToMultiByte function will do. The function might end up using a default character or performing other default behavior if the bytes following the lead byte are not as expected.</para>
/// </remarks>
// https://docs.microsoft.com/en-us/windows/desktop/api/winnls/ns-winnls-_cpinfoexa
// typedef struct _cpinfoexA { UINT MaxCharSize; BYTE DefaultChar[MAX_DEFAULTCHAR]; BYTE LeadByte[MAX_LEADBYTES]; WCHAR UnicodeDefaultChar; UINT CodePage; CHAR CodePageName[MAX_PATH]; } CPINFOEXA, *LPCPINFOEXA;
[PInvokeData("winnls.h", MSDNShortId = "9639bb11-477e-45ee-b9fb-d5d099925e00")]
[StructLayout(LayoutKind.Sequential)]
public struct CPINFOEX
{
/// <summary>
/// Maximum length, in bytes, of a character in the code page. The length can be 1 for a single-byte character set (SBCS), 2 for
/// a double-byte character set (DBCS), or a value larger than 2 for other character set types. The function cannot use the size
/// to distinguish an SBCS or a DBCS from other character sets because of other factors, for example, the use of ISCII or
/// ISO-2022-xx code pages.
/// </summary>
/// <summary>Maximum length, in bytes, of a character in the code page. The length can be 1 for a single-byte character set (SBCS), 2 for a double-byte character set (DBCS), or a value larger than 2 for other character set types. The function cannot use the size to distinguish an SBCS or a DBCS from other character sets because of other factors, for example, the use of ISCII or ISO-2022-xx code pages.</summary>
public uint MaxCharSize;
/// <summary>
/// Default character used when translating character strings to the specific code page. This character is used by the
/// <c>WideCharToMultiByte</c> function if an explicit default character is not specified. The default is usually the "?"
/// character for the code page.
/// </summary>
/// <summary>Default character used when translating character strings to the specific code page. This character is used by the WideCharToMultiByte function if an explicit default character is not specified. The default is usually the "?" character for the code page.</summary>
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)]
public byte[] DefaultChar;
/// <summary>
/// A fixed-length array of lead byte ranges, for which the number of lead byte ranges is variable. If the code page has no lead
/// bytes, every element of the array is set to <c>NULL</c>. If the code page has lead bytes, the array specifies a starting
/// value and an ending value for each range. Ranges are inclusive, and the maximum number of ranges for any code page is five.
/// The array uses two bytes to describe each range, with two null bytes as a terminator after the last range.
/// <para>A fixed-length array of lead byte ranges, for which the number of lead byte ranges is variable. If the code page has no lead bytes, every element of the array is set to <c>NULL</c>. If the code page has lead bytes, the array specifies a starting value and an ending value for each range. Ranges are inclusive, and the maximum number of ranges for any code page is five. The array uses two bytes to describe each range, with two null bytes as a terminator after the last range.</para>
/// <para>
/// <c>Note</c> Some code pages use lead bytes and a combination of other encoding mechanisms. This member is usually only populated for a subset of the code pages that use lead bytes in some form. For more information, see the Remarks section.</para>
/// </summary>
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 12)]
public char UnicodeDefaultChar;
public byte[] LeadByte;
/// <summary>
/// Code page value. This value reflects the code page passed to the <c>GetCPInfoEx</c> function. See Code Page Identifiers for a
/// list of ANSI and other code pages.
/// </summary>
/// <summary>Unicode default character used in translations from the specific code page. The default is usually the "?" character or the katakana middle dot character. The Unicode default character is used by the MultiByteToWideChar function.</summary>
public ushort UnicodeDefaultChar;
/// <summary>Code page value. This value reflects the code page passed to the GetCPInfoEx function. See Code Page Identifiers for a list of ANSI and other code pages.</summary>
public uint CodePage;
/// <summary>
/// Full name of the code page. Note that this name is localized and is not guaranteed for uniqueness or consistency between
/// operating system versions or computers.
/// </summary>
/// <summary>Full name of the code page. Note that this name is localized and is not guaranteed for uniqueness or consistency between operating system versions or computers.</summary>
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
public string CodePageName;
}

View File

@ -521,19 +521,15 @@ namespace Vanara.PInvoke
public StrPtrUni pszOwner;
/// <summary>This member is unused.</summary>
[MarshalAs(UnmanagedType.LPWStr)]
public StrPtrUni pszCompany;
/// <summary>This member is unused.</summary>
[MarshalAs(UnmanagedType.LPWStr)]
public StrPtrUni pszComments;
/// <summary>This member is unused.</summary>
[MarshalAs(UnmanagedType.LPWStr)]
public StrPtrUni pszContact;
/// <summary>This member is unused.</summary>
[MarshalAs(UnmanagedType.LPWStr)]
public StrPtrUni pszSupportUrl;
/// <summary>

View File

@ -271,9 +271,9 @@ namespace Vanara.PInvoke.Tests
var kerb = new KERB_INTERACTIVE_LOGON
{
MessageType = KERB_LOGON_SUBMIT_TYPE.KerbInteractiveLogon,
LogonDomainName = new LSA_UNICODE_STRING(domain),
UserName = new LSA_UNICODE_STRING(user),
Password = new LSA_UNICODE_STRING(pwd)
LogonDomainName = new SafeLSA_UNICODE_STRING(domain),
UserName = new SafeLSA_UNICODE_STRING(user),
Password = new SafeLSA_UNICODE_STRING(pwd)
};
var mem = SafeHGlobalHandle.CreateFromStructure(kerb);
AllocateLocallyUniqueId(out var srcLuid);

View File

@ -47,6 +47,7 @@
<Compile Include="Secur32\SaslTests.cs" />
<Compile Include="Secur32\SspiTests.cs" />
<Compile Include="Secur32\Secur32Tests.cs" />
<Compile Include="SecurityTests.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\Core\Vanara.Core.csproj">

View File

@ -0,0 +1,44 @@
using NUnit.Framework;
using System;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using Vanara.Extensions;
using Vanara.InteropServices;
using static Vanara.PInvoke.AdvApi32;
namespace Vanara.PInvoke.Tests
{
[TestFixture()]
public class SecurityTests
{
[Test]
public void StructMarshalTest()
{
bool allGood = true;
using (var mem = new SafeHGlobalHandle(4096))
{
//foreach (var asm in Assembly.GetExecutingAssembly().GetReferencedAssemblies().Where(n => n.Name.StartsWith("Vanara")).Select(n => Assembly.Load(n)))
//{
var asm = typeof(Vanara.PInvoke.AdvApi32).Assembly;
foreach (var tstr in asm.GetTypes().Where(t => t.IsValueType && !t.IsPrimitive && !t.IsEnum && !t.IsGenericType))
{
if (tstr.Name.StartsWith("<>")) continue;
try
{
Marshal.PtrToStructure(mem.DangerousGetHandle(), tstr);
}
catch (Exception e)
{
TestContext.WriteLine(e);
allGood = false;
}
}
//}
}
Assert.That(allGood);
}
}
}