Skip to content

Commit 4ce5d90

Browse files
author
dahall
committed
Adjusted Kernel32, User32, and Net assemblies to work with generator
1 parent 0218e69 commit 4ce5d90

File tree

11 files changed

+272
-30
lines changed

11 files changed

+272
-30
lines changed

Net/DhcpClient.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public class DhcpClient : IDisposable
1616
private static readonly DhcpInit init = new();
1717
#pragma warning restore IDE0052 // Remove unread private members
1818

19-
private readonly SafeEventHandle closing = CreateEvent(default, false, false), updateList = CreateEvent(default, false, false);
19+
private SafeEventHandle closing = CreateEvent(), updateList = CreateEvent();
2020
private readonly SafeHTHREAD hThread;
2121
private readonly Dictionary<HEVENT, DHCP_OPTION_ID> paramChgEvents = new();
2222

PInvoke/Kernel32/FileApi.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11044,7 +11044,7 @@ public partial class SafeFindChangeNotificationHandle { }
1104411044

1104511045
/// <summary>Provides a <see cref="SafeHandle"/> that releases a created HFILE instance at disposal using CloseHandle.</summary>
1104611046
[AutoSafeHandle(null, typeof(HFILE), typeof(SafeSyncHandle))]
11047-
[AdjustAutoMethodNamePattern(@"File|ExW\b|Ex\b|W?\b", "")]
11047+
[AdjustAutoMethodNamePattern(@"File(?!Type)|ExW\b|Ex\b|W?\b", "")]
1104811048
public partial class SafeHFILE
1104911049
{
1105011050
/// <summary>Performs an implicit conversion from <see cref="Microsoft.Win32.SafeHandles.SafeFileHandle"/> to <see cref="SafeHFILE"/>.</summary>

PInvoke/Kernel32/LibLoaderApi.cs

Lines changed: 180 additions & 15 deletions
Large diffs are not rendered by default.

PInvoke/Kernel32/ProcessThreadsApi.cs

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7866,6 +7866,88 @@ public partial class SafeHPROCESS
78667866
[AdjustAutoMethodNamePattern(@"Thread|Ex\b", "")]
78677867
public partial class SafeHTHREAD
78687868
{
7869+
/// <summary>
7870+
/// <para>Creates a thread to execute within the virtual address space of the calling process.</para>
7871+
/// <para>To create a thread that runs in the virtual address space of another process, use the <c>CreateRemoteThread</c> function.</para>
7872+
/// </summary>
7873+
/// <param name="lpStartAddress">
7874+
/// A pointer to the application-defined function to be executed by the thread. This pointer represents the starting address of the
7875+
/// thread. For more information on the thread function, see <c>ThreadProc</c>.
7876+
/// </param>
7877+
/// <param name="lpParameter">A pointer to a variable to be passed to the thread.</param>
7878+
/// <param name="lpThreadId">
7879+
/// A pointer to a variable that receives the thread identifier. If this parameter is <c>NULL</c>, the thread identifier is not returned.
7880+
/// </param>
7881+
/// <param name="dwCreationFlags">
7882+
/// <para>The flags that control the creation of the thread.</para>
7883+
/// <list type="table">
7884+
/// <listheader>
7885+
/// <term>Value</term>
7886+
/// <term>Meaning</term>
7887+
/// </listheader>
7888+
/// <item>
7889+
/// <term>0</term>
7890+
/// <term>The thread runs immediately after creation.</term>
7891+
/// </item>
7892+
/// <item>
7893+
/// <term>CREATE_SUSPENDED 0x00000004</term>
7894+
/// <term>The thread is created in a suspended state, and does not run until the ResumeThread function is called.</term>
7895+
/// </item>
7896+
/// <item>
7897+
/// <term>STACK_SIZE_PARAM_IS_A_RESERVATION 0x00010000</term>
7898+
/// <term>
7899+
/// The dwStackSize parameter specifies the initial reserve size of the stack. If this flag is not specified, dwStackSize specifies
7900+
/// the commit size.
7901+
/// </term>
7902+
/// </item>
7903+
/// </list>
7904+
/// </param>
7905+
/// <param name="dwStackSize">
7906+
/// The initial size of the stack, in bytes. The system rounds this value to the nearest page. If this parameter is zero, the new
7907+
/// thread uses the default size for the executable. For more information, see Thread Stack Size.
7908+
/// </param>
7909+
/// <param name="lpThreadAttributes">
7910+
/// <para>
7911+
/// A pointer to a <c>SECURITY_ATTRIBUTES</c> structure that determines whether the returned handle can be inherited by child
7912+
/// processes. If lpThreadAttributes is NULL, the handle cannot be inherited.
7913+
/// </para>
7914+
/// <para>
7915+
/// The <c>lpSecurityDescriptor</c> member of the structure specifies a security descriptor for the new thread. If lpThreadAttributes
7916+
/// is NULL, the thread gets a default security descriptor. The ACLs in the default security descriptor for a thread come from the
7917+
/// primary token of the creator.
7918+
/// </para>
7919+
/// </param>
7920+
/// <returns>
7921+
/// <para>If the function succeeds, the return value is a handle to the new thread.</para>
7922+
/// <para>If the function fails, the return value is <c>NULL</c>. To get extended error information, call <c>GetLastError</c>.</para>
7923+
/// <para>
7924+
/// Note that <c>CreateThread</c> may succeed even if lpStartAddress points to data, code, or is not accessible. If the start address
7925+
/// is invalid when the thread runs, an exception occurs, and the thread terminates. Thread termination due to a invalid start
7926+
/// address is handled as an error exit for the thread's process. This behavior is similar to the asynchronous nature of
7927+
/// <c>CreateProcess</c>, where the process is created even if it refers to invalid or missing dynamic-link libraries (DLLs).
7928+
/// </para>
7929+
/// </returns>
7930+
public static SafeHTHREAD Create(ThreadProc lpStartAddress, [In, Optional] IntPtr lpParameter, out uint lpThreadId, [Optional] CREATE_THREAD_FLAGS dwCreationFlags, [Optional] SizeT dwStackSize, [In, Optional] SECURITY_ATTRIBUTES? lpThreadAttributes) =>
7931+
Kernel32.CreateThread(lpThreadAttributes, dwStackSize, lpStartAddress, lpParameter, dwCreationFlags, out lpThreadId);
7932+
7933+
/// <summary>
7934+
/// <para>Creates a thread to execute within the virtual address space of the calling process.</para>
7935+
/// <para>To create a thread that runs in the virtual address space of another process, use the <c>CreateRemoteThread</c> function.</para>
7936+
/// </summary>
7937+
/// <param name="lpStartAddress">A pointer to the application-defined function to be executed by the thread. This pointer represents the starting address of the
7938+
/// thread. For more information on the thread function, see <c>ThreadProc</c>.</param>
7939+
/// <returns>
7940+
/// <para>If the function succeeds, the return value is a handle to the new thread.</para>
7941+
/// <para>If the function fails, the return value is <c>NULL</c>. To get extended error information, call <c>GetLastError</c>.</para>
7942+
/// <para>
7943+
/// Note that <c>CreateThread</c> may succeed even if lpStartAddress points to data, code, or is not accessible. If the start address
7944+
/// is invalid when the thread runs, an exception occurs, and the thread terminates. Thread termination due to a invalid start
7945+
/// address is handled as an error exit for the thread's process. This behavior is similar to the asynchronous nature of
7946+
/// <c>CreateProcess</c>, where the process is created even if it refers to invalid or missing dynamic-link libraries (DLLs).
7947+
/// </para>
7948+
/// </returns>
7949+
public static SafeHTHREAD Create(ThreadProc lpStartAddress) => Create(lpStartAddress, IntPtr.Zero, out _);
7950+
78697951
/// <summary>Gets a handle to the current thread that can be used across processes.</summary>
78707952
/// <value>The current thread handle.</value>
78717953
public static SafeHTHREAD Current => new(GetCurrentThread().Duplicate());

PInvoke/Kernel32/SynchApi.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,6 @@ public enum WAIT_STATUS : uint
355355
// LPCTSTR lpName); https://msdn.microsoft.com/en-us/library/windows/desktop/ms682396(v=vs.85).aspx
356356
[DllImport(Lib.Kernel32, SetLastError = true, CharSet = CharSet.Auto)]
357357
[PInvokeData("WinBase.h", MSDNShortId = "ms682396")]
358-
[return: AddAsCtor]
359358
public static extern SafeEventHandle CreateEvent([In, Optional] SECURITY_ATTRIBUTES? lpEventAttributes, [Optional, MarshalAs(UnmanagedType.Bool)] bool bManualReset,
360359
[Optional, MarshalAs(UnmanagedType.Bool)] bool bInitialState, [In, Optional] string? lpName);
361360

@@ -1245,7 +1244,6 @@ public static extern bool InitOnceBeginInitialize(ref INIT_ONCE lpInitOnce, INIT
12451244
// HANDLE WINAPI OpenEvent( _In_ DWORD dwDesiredAccess, _In_ BOOL bInheritHandle, _In_ LPCTSTR lpName); https://msdn.microsoft.com/en-us/library/windows/desktop/ms684305(v=vs.85).aspx
12461245
[DllImport(Lib.Kernel32, SetLastError = true, CharSet = CharSet.Auto)]
12471246
[PInvokeData("WinBase.h", MSDNShortId = "ms684305")]
1248-
[return: AddAsCtor]
12491247
public static extern SafeEventHandle OpenEvent(ACCESS_MASK dwDesiredAccess, [MarshalAs(UnmanagedType.Bool)] bool bInheritHandle, string lpName);
12501248

12511249
/// <summary>Opens an existing named mutex object.</summary>

PInvoke/Kernel32/Vanara.PInvoke.Kernel32.csproj

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,4 @@ Structures
2626
<ItemGroup>
2727
<ProjectReference Include="..\Shared\Vanara.PInvoke.Shared.csproj" />
2828
</ItemGroup>
29-
<ItemGroup>
30-
<Folder Include="Properties\" />
31-
</ItemGroup>
3229
</Project>

PInvoke/Kernel32/WinBase.App.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -647,7 +647,7 @@ public static extern HRESULT GetApplicationRestartSettings([In] HPROCESS hProces
647647
[DllImport(Lib.Kernel32, SetLastError = true, ExactSpelling = true, CharSet = CharSet.Unicode)]
648648
[PInvokeData("Winbase.h", MSDNShortId = "aa375700")]
649649
[return: MarshalAs(UnmanagedType.Bool)]
650-
public static extern bool QueryActCtxSettingsW([Optional, Ignore] uint dwFlags, [In, AddAsMember] HACTCTX hActCtx, [Optional, Ignore] string? settingsNameSpace,
650+
public static extern bool QueryActCtxSettingsW([Optional, Ignore] uint dwFlags, [In, AddAsMember] HACTCTX hActCtx, [Optional] string? settingsNameSpace,
651651
string settingName, [Out, SizeDef(nameof(dwBuffer), SizingMethod.Query, OutVarName = nameof(pdwWrittenOrRequired))] StringBuilder? pvBuffer, SizeT dwBuffer, out SizeT pdwWrittenOrRequired);
652652

653653
/// <summary>The <c>QueryActCtxW</c> function queries the activation context.</summary>

PInvoke/Kernel32/Wow64ApiSet.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ public enum IMAGE_FILE_MACHINE : ushort
195195
/// <see langword="true"/> if the process is running under WOW64. If the process is running under 32-bit Windows, the value is set
196196
/// to <see langword="false"/>. If the process is a 64-bit application running under 64-bit Windows, the value is also set to <see langword="false"/>.
197197
/// </returns>
198-
public static bool IsWow64(this HPROCESS hProc) => Environment.OSVersion.Version >= new Version(5, 1) && IsWow64Process(hProc, out var b) && b;
198+
public static bool IsWow64([In] this HPROCESS hProc) => Environment.OSVersion.Version >= new Version(5, 1) && IsWow64Process(hProc, out var b) && b;
199199

200200
/// <summary>
201201
/// Determines whether the specified process is running under WOW64; also returns additional machine process and architecture information.

PInvoke/User32/ModalDialog.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public ModalDialog(HINSTANCE hInst, ResourceId dlgId) : this(new SafeHINSTANCE(h
2626
/// dialog box template or an integer value that specifies the resource identifier of the dialog box template.
2727
/// </param>
2828
public ModalDialog(string libraryPath, ResourceId dlgId)
29-
: this(Win32Error.ThrowLastErrorIfInvalid(LoadLibraryEx(libraryPath, LoadLibraryExFlags.LOAD_LIBRARY_AS_DATAFILE)), dlgId) { }
29+
: this(Win32Error.ThrowLastErrorIfInvalid(LoadLibraryEx(libraryPath, default, LoadLibraryExFlags.LOAD_LIBRARY_AS_DATAFILE)), dlgId) { }
3030

3131
/// <inheritdoc/>
3232
public HWND Handle { get; protected set; }

UnitTests/PInvoke/Kernel32/SynchApiTests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ public void CriticalSectionTest()
239239
SetCriticalSectionSpinCount(ref critSect, 400);
240240
try
241241
{
242-
using SafeHTHREAD hThread = CreateThread(null, 0, ThreadProc, default, 0, out _);
242+
using var hThread = SafeHTHREAD.Create(ThreadProc);
243243
WaitForSingleObject(hThread, INFINITE);
244244
Assert.That(GetExitCodeThread(hThread, out uint c), ResultIs.Successful);
245245
Assert.That(c, Is.Zero);
@@ -355,7 +355,7 @@ public void EventTest()
355355
TestContext.Write("MyMain thread writing to the shared buffer...\n");
356356

357357
// Set ghWriteEvent to signaled
358-
Assert.That(SetEvent(ghWriteEvent), ResultIs.Successful);
358+
Assert.That(ghWriteEvent.Set(), ResultIs.Successful);
359359

360360
TestContext.Write("MyMain thread waiting for threads to exit...\n");
361361

@@ -377,8 +377,8 @@ public void EventTest()
377377
foreach (SafeHTHREAD t in ghThreads)
378378
t.Dispose();
379379

380-
Assert.That(ResetEvent(ghWriteEvent), ResultIs.Successful);
381-
Assert.That(PulseEvent(ghWriteEvent), ResultIs.Successful);
380+
Assert.That(ghWriteEvent.Reset(), ResultIs.Successful);
381+
Assert.That(ghWriteEvent.Pulse(), ResultIs.Successful);
382382
}
383383

384384
uint ThreadProc(IntPtr _)

0 commit comments

Comments
 (0)