Skip to content
This repository has been archived by the owner on Jul 26, 2023. It is now read-only.

Commit

Permalink
Merge pull request #129 from AArnott/fix76_kernel32
Browse files Browse the repository at this point in the history
Switch to native pointer with codegen IntPtr overloads for Kernel32
  • Loading branch information
AArnott committed Dec 30, 2015
2 parents 6977f7d + 765c587 commit 01a34ed
Show file tree
Hide file tree
Showing 10 changed files with 71 additions and 28 deletions.
19 changes: 19 additions & 0 deletions src/Kernel32.Desktop/Kernel32+PROC_THREAD_ATTRIBUTE_LIST.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright (c) to owners found in https://github.com/AArnott/pinvoke/blob/master/COPYRIGHT.md. All rights reserved.
// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.

namespace PInvoke
{
/// <content>
/// Contains the <see cref="PROC_THREAD_ATTRIBUTE_LIST"/> nested type.
/// </content>
public partial class Kernel32
{
/// <summary>
/// An empty struct.
/// </summary>
public struct PROC_THREAD_ATTRIBUTE_LIST
{
// This struct intentionally left blank. It has no fields, per the processthreadsapi.h file.
}
}
}
2 changes: 1 addition & 1 deletion src/Kernel32.Desktop/Kernel32+PipeMode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public enum PipeMode : uint
/// <summary>
/// Blocking mode is enabled. When the pipe handle is specified in the
/// <see cref="ReadFile(SafeObjectHandle,void*,int,NullableUInt32,OVERLAPPED*)" />,
/// <see cref="WriteFile(SafeObjectHandle,void*,int,NullableUInt32,OVERLAPPED*)" />, or <see cref="ConnectNamedPipe"/> function, the
/// <see cref="WriteFile(SafeObjectHandle,void*,int,NullableUInt32,OVERLAPPED*)" />, or <see cref="ConnectNamedPipe(SafeObjectHandle, OVERLAPPED*)"/> function, the
/// operations are not completed until there is data to read, all data is written, or a client is connected. Use of
/// this mode can mean waiting indefinitely in some situations for a client process to perform an action.
/// </summary>
Expand Down
4 changes: 2 additions & 2 deletions src/Kernel32.Desktop/Kernel32+STARTUPINFOEX.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace PInvoke
public partial class Kernel32
{
/// <summary>
/// Specifies the window station, desktop, standard handles, and attributes for a new process. It is used with the <see cref="CreateProcess"/> and <see cref="CreateProcessAsUser"/> functions.
/// Specifies the window station, desktop, standard handles, and attributes for a new process. It is used with the <see cref="CreateProcess(string, string, SECURITY_ATTRIBUTES, SECURITY_ATTRIBUTES, bool, CreateProcessFlags, void*, string, ref STARTUPINFO, out PROCESS_INFORMATION)"/> and <see cref="CreateProcessAsUser(IntPtr, string, string, SECURITY_ATTRIBUTES, SECURITY_ATTRIBUTES, bool, CreateProcessFlags, void*, string, ref STARTUPINFO, out PROCESS_INFORMATION)"/> functions.
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct STARTUPINFOEX
Expand All @@ -23,7 +23,7 @@ public struct STARTUPINFOEX
public STARTUPINFO StartupInfo;

/// <summary>
/// An attribute list. This list is created by the <see cref="InitializeProcThreadAttributeList"/> function.
/// An attribute list. This list is created by the <see cref="InitializeProcThreadAttributeList(PROC_THREAD_ATTRIBUTE_LIST*, int, uint, ref IntPtr)"/> function.
/// </summary>
public IntPtr lpAttributeList;

Expand Down
14 changes: 13 additions & 1 deletion src/Kernel32.Desktop/Kernel32.Desktop.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
<Compile Include="Kernel32+ProcessAccess.cs" />
<Compile Include="Kernel32+PROCESSENTRY32.cs" />
<Compile Include="Kernel32+PROCESS_INFORMATION.cs" />
<Compile Include="Kernel32+PROC_THREAD_ATTRIBUTE_LIST.cs" />
<Compile Include="Kernel32+QueryFullProcessImageNameFlags.cs" />
<Compile Include="Kernel32+SafeLibraryHandle.cs" />
<Compile Include="Kernel32+SECURITY_IMPERSONATION_LEVEL.cs" />
Expand All @@ -49,12 +50,23 @@
<Compile Include="Kernel32+StartupInfoFlags.cs" />
<Compile Include="Kernel32+STARTUPINFOEX.cs" />
<Compile Include="Kernel32+STARTUPINFO.cs" />
<Compile Include="Kernel32.cs" />
<Compile Include="Kernel32.cs">
<Generator>MSBuild:GenerateCodeFromAttributes</Generator>
</Compile>
<Compile Include="Kernel32.Helpers.cs" />
<Compile Include="Kernel32+NullablePipeMode.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\CodeGenerationAttributes.Net40\CodeGenerationAttributes.Net40.csproj">
<Project>{6a77281b-c503-44ea-90c1-0e9868d06cd0}</Project>
<Name>CodeGenerationAttributes.Net40</Name>
</ProjectReference>
<ProjectReference Include="..\CodeGeneration\CodeGeneration.csproj">
<Project>{C1815471-02AF-4BB9-8D83-652ADBAFF5B6}</Project>
<Name>CodeGeneration</Name>
<ReferenceOutputAssembly>false</ReferenceOutputAssembly>
</ProjectReference>
<ProjectReference Include="..\Windows.Core\Windows.Core.csproj">
<Project>{b08c3c79-4cdd-4d37-933c-07d3452fd5f1}</Project>
<Name>Windows.Core</Name>
Expand Down
38 changes: 19 additions & 19 deletions src/Kernel32.Desktop/Kernel32.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public static partial class Kernel32

/// <summary>
/// Creates a new process and its primary thread. The new process runs in the security context of the calling process.
/// If the calling process is impersonating another user, the new process uses the token for the calling process, not the impersonation token. To run the new process in the security context of the user represented by the impersonation token, use the <see cref="CreateProcessAsUser"/> or CreateProcessWithLogonW function.
/// If the calling process is impersonating another user, the new process uses the token for the calling process, not the impersonation token. To run the new process in the security context of the user represented by the impersonation token, use the <see cref="CreateProcessAsUser(IntPtr, string, string, SECURITY_ATTRIBUTES, SECURITY_ATTRIBUTES, bool, CreateProcessFlags, void*, string, ref STARTUPINFO, out PROCESS_INFORMATION)"/> or CreateProcessWithLogonW function.
/// </summary>
/// <param name="lpApplicationName">
/// The name of the module to be executed. This module can be a Windows-based application. It can be some other type of module (for example, MS-DOS or OS/2) if the appropriate subsystem is available on the local computer.
Expand Down Expand Up @@ -99,14 +99,14 @@ public static partial class Kernel32
/// </returns>
[DllImport(api_ms_win_core_processthreads_l1_1_1, SetLastError = true, CharSet = CharSet.Unicode)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool CreateProcess(
public static unsafe extern bool CreateProcess(
string lpApplicationName,
string lpCommandLine,
SECURITY_ATTRIBUTES lpProcessAttributes,
SECURITY_ATTRIBUTES lpThreadAttributes,
[MarshalAs(UnmanagedType.Bool)] bool bInheritHandles,
CreateProcessFlags dwCreationFlags,
IntPtr lpEnvironment, // IntPtr because it may point to unicode or ANSI characters, based on a flag.
void* lpEnvironment, // pointer because it may point to unicode or ANSI characters, based on a flag.
string lpCurrentDirectory,
ref STARTUPINFO lpStartupInfo,
out PROCESS_INFORMATION lpProcessInformation);
Expand Down Expand Up @@ -180,15 +180,15 @@ public static extern bool CreateProcess(
/// </returns>
[DllImport(api_ms_win_core_processthreads_l1_1_1, SetLastError = true, CharSet = CharSet.Unicode)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool CreateProcessAsUser(
public static unsafe extern bool CreateProcessAsUser(
IntPtr hToken,
string lpApplicationName,
string lpCommandLine,
SECURITY_ATTRIBUTES lpProcessAttributes,
SECURITY_ATTRIBUTES lpThreadAttributes,
[MarshalAs(UnmanagedType.Bool)] bool bInheritHandles,
CreateProcessFlags dwCreationFlags,
IntPtr lpEnvironment, // IntPtr because it may point to unicode or ANSI characters, based on a flag.
void* lpEnvironment, // pointer because it may point to unicode or ANSI characters, based on a flag.
string lpCurrentDirectory,
ref STARTUPINFO lpStartupInfo,
out PROCESS_INFORMATION lpProcessInformation);
Expand Down Expand Up @@ -227,13 +227,13 @@ public static extern void GetStartupInfo(
/// </returns>
/// <remarks>
/// First, call this function with the <paramref name="dwAttributeCount "/> parameter set to the maximum number of attributes you will be using and the lpAttributeList to NULL. The function returns the required buffer size in bytes in the lpSize parameter. Allocate enough space for the data in the lpAttributeList buffer and call the function again to initialize the buffer.
/// To add attributes to the list, call the <see cref="UpdateProcThreadAttribute"/> function. To specify these attributes when creating a process, specify <see cref="CreateProcessFlags.EXTENDED_STARTUPINFO_PRESENT"/> in the dwCreationFlag parameter and a <see cref="STARTUPINFOEX"/> structure in the lpStartupInfo parameter. Note that you can specify the same <see cref="STARTUPINFOEX"/> structure to multiple child processes.
/// When you have finished using the list, call the <see cref="DeleteProcThreadAttributeList"/> function.
/// To add attributes to the list, call the <see cref="UpdateProcThreadAttribute(PROC_THREAD_ATTRIBUTE_LIST*, uint, ref uint, void*, IntPtr, ref IntPtr, ref IntPtr)"/> function. To specify these attributes when creating a process, specify <see cref="CreateProcessFlags.EXTENDED_STARTUPINFO_PRESENT"/> in the dwCreationFlag parameter and a <see cref="STARTUPINFOEX"/> structure in the lpStartupInfo parameter. Note that you can specify the same <see cref="STARTUPINFOEX"/> structure to multiple child processes.
/// When you have finished using the list, call the <see cref="DeleteProcThreadAttributeList(PROC_THREAD_ATTRIBUTE_LIST*)"/> function.
/// </remarks>
[DllImport(api_ms_win_core_processthreads_l1_1_1, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool InitializeProcThreadAttributeList(
IntPtr lpAttributeList,
public static unsafe extern bool InitializeProcThreadAttributeList(
PROC_THREAD_ATTRIBUTE_LIST* lpAttributeList,
int dwAttributeCount,
uint dwFlags,
ref IntPtr lpSize); // SIZE_T (the size varies with the bitness)
Expand All @@ -242,7 +242,7 @@ public static extern bool InitializeProcThreadAttributeList(
/// Updates the specified attribute in a list of attributes for process and thread creation.
/// </summary>
/// <param name="lpAttributeList">
/// A pointer to an attribute list created by the <see cref="InitializeProcThreadAttributeList"/> function.
/// A pointer to an attribute list created by the <see cref="InitializeProcThreadAttributeList(PROC_THREAD_ATTRIBUTE_LIST*, int, uint, ref IntPtr)"/> function.
/// </param>
/// <param name="dwFlags">
/// This parameter is reserved and must be zero.
Expand All @@ -251,7 +251,7 @@ public static extern bool InitializeProcThreadAttributeList(
/// The attribute key to update in the attribute list.
/// </param>
/// <param name="lpValue">
/// A pointer to the attribute value. This value should persist until the attribute is destroyed using the <see cref="DeleteProcThreadAttributeList"/> function.
/// A pointer to the attribute value. This value should persist until the attribute is destroyed using the <see cref="DeleteProcThreadAttributeList(PROC_THREAD_ATTRIBUTE_LIST*)"/> function.
/// </param>
/// <param name="cbSize">
/// The size of the attribute value specified by the <paramref name="lpValue"/> parameter.
Expand All @@ -266,11 +266,11 @@ public static extern bool InitializeProcThreadAttributeList(
/// </returns>
[DllImport(api_ms_win_core_processthreads_l1_1_1, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool UpdateProcThreadAttribute(
IntPtr lpAttributeList,
public static unsafe extern bool UpdateProcThreadAttribute(
PROC_THREAD_ATTRIBUTE_LIST* lpAttributeList,
uint dwFlags,
ref uint Attribute,
IntPtr lpValue,
void* lpValue,
IntPtr cbSize, // SIZE_T varies by bitness
ref IntPtr lpPreviousValue,
ref IntPtr lpReturnSize);
Expand All @@ -279,11 +279,11 @@ public static extern bool UpdateProcThreadAttribute(
/// Deletes the specified list of attributes for process and thread creation.
/// </summary>
/// <param name="lpAttributeList">
/// The attribute list. This list is created by the <see cref="InitializeProcThreadAttributeList"/> function.
/// The attribute list. This list is created by the <see cref="InitializeProcThreadAttributeList(PROC_THREAD_ATTRIBUTE_LIST*, int, uint, ref IntPtr)"/> function.
/// </param>
[DllImport(api_ms_win_core_processthreads_l1_1_1)]
public static extern void DeleteProcThreadAttributeList(
IntPtr lpAttributeList);
public static unsafe extern void DeleteProcThreadAttributeList(
PROC_THREAD_ATTRIBUTE_LIST* lpAttributeList);

/// <summary>
/// Allocates a new console for the calling process.
Expand Down Expand Up @@ -657,7 +657,7 @@ public static extern unsafe bool GetOverlappedResult(
/// <returns>
/// If the function succeeds, the return value is nonzero. The cancel operation for all pending I/O operations issued by
/// the calling thread for the specified file handle was successfully requested. The thread can use the
/// <see cref="GetOverlappedResult" /> function to determine when the I/O operations themselves have been completed.
/// <see cref="GetOverlappedResult(SafeObjectHandle, OVERLAPPED*, out int, bool)" /> function to determine when the I/O operations themselves have been completed.
/// <para>
/// If the function fails, the return value is zero (0). To get extended error information, call the
/// <see cref="GetLastError" /> function.
Expand Down Expand Up @@ -872,7 +872,7 @@ public static extern SafeObjectHandle CreateNamedPipe(

/// <summary>
/// Waits until either a time-out interval elapses or an instance of the specified named pipe is available for
/// connection (that is, the pipe's server process has a pending <see cref="ConnectNamedPipe" /> operation on the
/// connection (that is, the pipe's server process has a pending <see cref="ConnectNamedPipe(SafeObjectHandle, OVERLAPPED*)" /> operation on the
/// pipe).
/// </summary>
/// <param name="lpNamedPipeName">
Expand Down
2 changes: 1 addition & 1 deletion src/Kernel32.Shared/Kernel32+FindFirstFileExFlags.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace PInvoke
public partial class Kernel32
{
/// <summary>
/// Optional flags to pass to the <see cref="FindFirstFileEx"/> method.
/// Optional flags to pass to the <see cref="FindFirstFileEx(string, FINDEX_INFO_LEVELS, out WIN32_FIND_DATA, FINDEX_SEARCH_OPS, void*, FindFirstFileExFlags)"/> method.
/// </summary>
[Flags]
public enum FindFirstFileExFlags
Expand Down
2 changes: 1 addition & 1 deletion src/Kernel32.Shared/Kernel32+FormatMessageFlags.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace PInvoke
public partial class Kernel32
{
/// <summary>
/// Flags passed to the <see cref="FormatMessage"/> method.
/// Flags passed to the <see cref="FormatMessage(FormatMessageFlags, void*, uint, uint, System.Text.StringBuilder, int, IntPtr[])"/> method.
/// </summary>
[Flags]
public enum FormatMessageFlags
Expand Down
4 changes: 3 additions & 1 deletion src/Kernel32.Shared/Kernel32.Shared.projitems
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
<Compile Include="$(MSBuildThisFileDirectory)Kernel32+SafeObjectHandle.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Kernel32+WaitForSingleObjectResult.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Kernel32+WIN32_FIND_DATA.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Kernel32.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Kernel32.cs">
<Generator>MSBuild:GenerateCodeFromAttributes</Generator>
</Compile>
<Compile Include="$(MSBuildThisFileDirectory)Kernel32.Helpers.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Kernel32Extensions.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Win32Exception.cs" />
Expand Down
5 changes: 3 additions & 2 deletions src/Kernel32.Shared/Kernel32.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ namespace PInvoke
/// <summary>
/// Exported functions from the Kernel32.dll Windows library.
/// </summary>
[OfferIntPtrOverloads]
public static partial class Kernel32
{
/// <summary>
Expand Down Expand Up @@ -84,7 +85,7 @@ public static partial class Kernel32
/// If the function fails or fails to locate files from the search string in the lpFileName parameter, the return value is INVALID_HANDLE_VALUE and the contents of lpFindFileData are indeterminate.To get extended error information, call the <see cref="GetLastError"/> function.
/// </returns>
[DllImport(api_ms_win_core_file_l1_2_0)]
public static extern SafeFindFilesHandle FindFirstFileEx(string lpFileName, FINDEX_INFO_LEVELS fInfoLevelId, out WIN32_FIND_DATA lpFindFileData, FINDEX_SEARCH_OPS fSearchOp, IntPtr lpSearchFilter, FindFirstFileExFlags dwAdditionalFlags);
public static unsafe extern SafeFindFilesHandle FindFirstFileEx(string lpFileName, FINDEX_INFO_LEVELS fInfoLevelId, out WIN32_FIND_DATA lpFindFileData, FINDEX_SEARCH_OPS fSearchOp, void* lpSearchFilter, FindFirstFileExFlags dwAdditionalFlags);

/// <summary>
/// Formats a message string. The function requires a message definition as input. The message definition can come from a buffer passed into the function. It can come from a message table resource in an already-loaded module. Or the caller can ask the function to search the system's message table resource(s) for the message definition. The function finds the message definition in a message table resource based on a message identifier and a language identifier. The function copies the formatted message text to an output buffer, processing any embedded insert sequences if requested.
Expand Down Expand Up @@ -131,7 +132,7 @@ public static partial class Kernel32
/// If the function fails, the return value is zero.To get extended error information, call <see cref="GetLastError"/>.
/// </returns>
[DllImport(api_ms_win_core_localization_l1_2_0, CharSet = CharSet.Unicode, SetLastError = true)]
public static extern int FormatMessage(FormatMessageFlags dwFlags, IntPtr lpSource, uint dwMessageId, uint dwLanguageId, StringBuilder lpBuffer, int nSize, IntPtr[] Arguments);
public static unsafe extern int FormatMessage(FormatMessageFlags dwFlags, void* lpSource, uint dwMessageId, uint dwLanguageId, StringBuilder lpBuffer, int nSize, IntPtr[] Arguments);

/// <summary>
/// Retrieves the thread identifier of the calling thread.
Expand Down
9 changes: 9 additions & 0 deletions src/Kernel32/Kernel32.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@
<None Include="project.json" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\CodeGenerationAttributes\CodeGenerationAttributes.csproj">
<Project>{1387e009-7086-4572-ac8d-ce4242adec77}</Project>
<Name>CodeGenerationAttributes</Name>
</ProjectReference>
<ProjectReference Include="..\CodeGeneration\CodeGeneration.csproj">
<Project>{C1815471-02AF-4BB9-8D83-652ADBAFF5B6}</Project>
<Name>CodeGeneration</Name>
<ReferenceOutputAssembly>false</ReferenceOutputAssembly>
</ProjectReference>
<ProjectReference Include="..\Windows.Core\Windows.Core.csproj">
<Project>{b08c3c79-4cdd-4d37-933c-07d3452fd5f1}</Project>
<Name>Windows.Core</Name>
Expand Down

0 comments on commit 01a34ed

Please sign in to comment.