Class W32
- Namespace
- SunamoPInvoke.PInvoke
- Assembly
- SunamoPInvoke.dll
Provides P/Invoke declarations for Windows API functions from kernel32, user32, shell32, psapi, gdi32 and advapi32.
public class W32 : W32Base
- Inheritance
-
W32
- Inherited Members
- Extension Methods
Fields
CREATE_ALWAYS
Creates a new file, always. Overwrites if the file exists.
public const int CREATE_ALWAYS = 2
Field Value
CREATE_NEW
Creates a new file only if it does not already exist.
public const int CREATE_NEW = 1
Field Value
ERROR_INSUFFICIENT_BUFFER
Error code indicating the buffer is too small.
public const int ERROR_INSUFFICIENT_BUFFER = 122
Field Value
FILE_ATTRIBUTE_DIRECTORY
File attribute flag indicating a directory.
public const uint FILE_ATTRIBUTE_DIRECTORY = 16
Field Value
FILE_ATTRIBUTE_NORMAL
File attribute flag indicating a normal file.
public const uint FILE_ATTRIBUTE_NORMAL = 128
Field Value
FILE_SHARE_DELETE
Enables subsequent delete operations on a file.
public const int FILE_SHARE_DELETE = 4
Field Value
FILE_SHARE_READ
Enables subsequent read operations on a file.
public const int FILE_SHARE_READ = 1
Field Value
FILE_SHARE_WRITE
Enables subsequent write operations on a file.
public const int FILE_SHARE_WRITE = 2
Field Value
GENERIC_READ
Generic read access flag.
public const uint GENERIC_READ = 2147483648
Field Value
HWND_MESSAGE
Handle value used to find message-only windows via FindWindowEx.
public static readonly nint HWND_MESSAGE
Field Value
INVALID_HANDLE_VALUE
Constant representing an invalid handle value.
public const int INVALID_HANDLE_VALUE = -1
Field Value
OPEN_ALWAYS
Opens a file, always. Creates the file if it does not exist.
public const int OPEN_ALWAYS = 4
Field Value
OPEN_EXISTING
Opens a file only if it exists.
public const int OPEN_EXISTING = 3
Field Value
SHGFI_ADDOVERLAYS
Flag to apply the appropriate overlays.
public const uint SHGFI_ADDOVERLAYS = 32
Field Value
SHGFI_ATTRIBUTES
Flag to retrieve the attributes.
public const uint SHGFI_ATTRIBUTES = 2048
Field Value
SHGFI_ATTR_SPECIFIED
Flag to get only specified attributes.
public const uint SHGFI_ATTR_SPECIFIED = 131072
Field Value
SHGFI_DISPLAYNAME
Flag to retrieve the display name.
public const uint SHGFI_DISPLAYNAME = 512
Field Value
SHGFI_EXETYPE
Flag to return the exe type.
public const uint SHGFI_EXETYPE = 8192
Field Value
SHGFI_ICON
Flag to retrieve the icon handle.
public const uint SHGFI_ICON = 256
Field Value
SHGFI_ICONLOCATION
Flag to retrieve the icon location.
public const uint SHGFI_ICONLOCATION = 4096
Field Value
SHGFI_LARGEICON
Flag to retrieve the large icon.
public const uint SHGFI_LARGEICON = 0
Field Value
SHGFI_LINKOVERLAY
Flag to put a link overlay on the icon.
public const uint SHGFI_LINKOVERLAY = 32768
Field Value
SHGFI_OPENICON
Flag to retrieve the open icon.
public const uint SHGFI_OPENICON = 2
Field Value
SHGFI_OVERLAYINDEX
Flag to get the index of the overlay.
public const uint SHGFI_OVERLAYINDEX = 64
Field Value
SHGFI_PIDL
Indicates that pszPath is a PIDL.
public const uint SHGFI_PIDL = 8
Field Value
SHGFI_SELECTED
Flag to show icon in selected state.
public const uint SHGFI_SELECTED = 65536
Field Value
SHGFI_SHELLICONSIZE
Flag to retrieve the shell size icon.
public const uint SHGFI_SHELLICONSIZE = 4
Field Value
SHGFI_SMALLICON
Flag to retrieve the small icon.
public const uint SHGFI_SMALLICON = 1
Field Value
SHGFI_SYSICONINDEX
Flag to get the system icon index.
public const uint SHGFI_SYSICONINDEX = 16384
Field Value
SHGFI_TYPENAME
Flag to retrieve the type name.
public const uint SHGFI_TYPENAME = 1024
Field Value
SHGFI_USEFILEATTRIBUTES
Flag to use the passed dwFileAttribute.
public const uint SHGFI_USEFILEATTRIBUTES = 16
Field Value
TRUNCATE_EXISTING
Opens a file and truncates it so that its size is zero bytes.
public const int TRUNCATE_EXISTING = 5
Field Value
WM_CLIPBOARDUPDATE
Message sent when the contents of the clipboard have changed.
public const int WM_CLIPBOARDUPDATE = 797
Field Value
Methods
AddClipboardFormatListener(nint)
Places the given window in the system-maintained clipboard format listener list.
public static extern bool AddClipboardFormatListener(nint hwnd)
Parameters
hwndnintA handle to the window to be placed in the clipboard format listener list.
Returns
- bool
True if the function succeeds.
AdjustTokenPrivileges(nint, bool, ref TOKEN_PRIVILEGES, int, nint, nint)
Enables or disables privileges in the specified access token.
public static extern bool AdjustTokenPrivileges(nint htok, bool disall, ref TOKEN_PRIVILEGES newst, int len, nint prev, nint relen)
Parameters
htoknintA handle to the access token that contains the privileges to be modified.
disallboolSpecifies whether the function disables all privileges.
newstTOKEN_PRIVILEGESA pointer to a TOKEN_PRIVILEGES structure.
lenintSpecifies the size, in bytes, of the buffer pointed to by the previous state parameter.
prevnintA pointer to a buffer for the previous state.
relennintA pointer to a variable that receives the required size.
Returns
- bool
True if the function succeeds.
CallNextHookEx(nint, int, nint, nint)
Passes the hook information to the next hook procedure in the current hook chain.
public static extern nint CallNextHookEx(nint hHook, int code, nint wParam, nint lParam)
Parameters
hHooknintThis parameter is ignored.
codeintThe hook code passed to the current hook procedure.
wParamnintThe wParam value passed to the current hook procedure.
lParamnintThe lParam value passed to the current hook procedure.
Returns
- nint
The value returned by the next hook procedure in the chain.
ChangeClipboardChain(nint, nint)
Removes a specified window from the chain of clipboard viewers.
public static extern bool ChangeClipboardChain(nint hWndRemove, nint hWndNewNext)
Parameters
hWndRemovenintA handle to the window to be removed from the chain.
hWndNewNextnintA handle to the window that follows the window being removed.
Returns
- bool
True if the function succeeds.
CloseClipboard()
Closes the clipboard.
public static extern bool CloseClipboard()
Returns
- bool
True if the function succeeds.
CloseHandle(nint)
Closes an open object handle.
public static extern bool CloseHandle(nint hObject)
Parameters
hObjectnintA valid handle to an open object.
Returns
- bool
True if the function succeeds.
CreateFile(string, uint, int, nint, int, int, nint)
Creates or opens a file or I/O device.
public static extern nint CreateFile(string lpFileName, uint dwDesiredAccess, int dwShareMode, nint lpSECURITY_ATTRIBUTES, int dwCreationDisposition, int dwFlagsAndAttributes, nint hTemplateFile)
Parameters
lpFileNamestringThe name of the file or device to be created or opened.
dwDesiredAccessuintThe requested access to the file or device.
dwShareModeintThe requested sharing mode of the file or device.
lpSECURITY_ATTRIBUTESnintA pointer to a SECURITY_ATTRIBUTES structure.
dwCreationDispositionintAn action to take on a file or device that exists or does not exist.
dwFlagsAndAttributesintThe file or device attributes and flags.
hTemplateFilenintA valid handle to a template file with GENERIC_READ access right.
Returns
- nint
An open handle to the specified file or device.
CreateProcessWithTokenW(nint, int, string, string, int, nint, string, ref STARTUPINFO, out PROCESS_INFORMATION)
Creates a new process and its primary thread. The new process runs in the security context of the specified token.
public static extern bool CreateProcessWithTokenW(nint hToken, int dwLogonFlags, string lpApplicationName, string lpCommandLine, int dwCreationFlags, nint lpEnvironment, string lpCurrentDirectory, ref STARTUPINFO lpStartupInfo, out PROCESS_INFORMATION lpProcessInformation)
Parameters
hTokennintA handle to the primary token that represents a user.
dwLogonFlagsintThe logon option.
lpApplicationNamestringThe name of the module to be executed.
lpCommandLinestringThe command line to be executed.
dwCreationFlagsintThe flags that control the priority class and the creation of the process.
lpEnvironmentnintA pointer to the environment block for the new process.
lpCurrentDirectorystringThe full path to the current directory for the process.
lpStartupInfoSTARTUPINFOA pointer to a STARTUPINFO structure.
lpProcessInformationPROCESS_INFORMATIONA pointer to a PROCESS_INFORMATION structure that receives identification information about the new process.
Returns
- bool
True if the function succeeds.
DeleteObject(nint)
Deletes a logical pen, brush, font, bitmap, region, or palette, freeing all system resources associated with the object.
public static extern bool DeleteObject(nint hObject)
Parameters
hObjectnintA handle to a logical pen, brush, font, bitmap, region, or palette.
Returns
- bool
True if the function succeeds.
DestroyIcon(nint)
Destroys an icon and frees any memory the icon occupied.
public static extern bool DestroyIcon(nint hIcon)
Parameters
hIconnintA handle to the icon to be destroyed.
Returns
- bool
True if the function succeeds.
DuplicateTokenEx(nint, uint, nint, SECURITY_IMPERSONATION_LEVEL, TOKEN_TYPE, out nint)
Creates a new access token that duplicates an existing token.
public static extern bool DuplicateTokenEx(nint hExistingToken, uint dwDesiredAccess, nint lpTokenAttributes, SECURITY_IMPERSONATION_LEVEL impersonationLevel, TOKEN_TYPE tokenType, out nint phNewToken)
Parameters
hExistingTokennintA handle to an access token.
dwDesiredAccessuintSpecifies the requested access rights for the new token.
lpTokenAttributesnintA pointer to a SECURITY_ATTRIBUTES structure.
impersonationLevelSECURITY_IMPERSONATION_LEVELSpecifies the impersonation level of the new token.
tokenTypeTOKEN_TYPESpecifies the type of the new token.
phNewTokennintA pointer to a variable that receives the handle to the new token.
Returns
- bool
True if the function succeeds.
EnableWow64FSRedirection(bool)
Enables or disables file system redirection for the calling thread.
public static extern bool EnableWow64FSRedirection(bool enable)
Parameters
enableboolTrue to enable redirection, false to disable it.
Returns
- bool
True if the function succeeds.
EnumClipboardFormats(uint)
Enumerates the data formats currently available on the clipboard.
public static extern uint EnumClipboardFormats(uint format)
Parameters
formatuintA clipboard format. Set to zero to begin enumeration.
Returns
- uint
The next available clipboard format, or zero if there are no more formats.
FindMimeFromData(uint, string, byte[], uint, string, uint, out uint, uint)
Determines the MIME type from the data provided.
public static extern uint FindMimeFromData(uint pBC, string pwzUrl, byte[] pBuffer, uint cbSize, string pwzMimeProposed, uint dwMimeFlags, out uint ppwzMimeOut, uint dwReserverd)
Parameters
pBCuintA pointer to the bind context. Can be set to zero.
pwzUrlstringA string value that contains the URL of the data.
pBufferbyte[]A pointer to the buffer that contains the data to be sniffed.
cbSizeuintThe number of bytes in the buffer.
pwzMimeProposedstringA string value that contains the proposed MIME type.
dwMimeFlagsuintFlags that control the operation.
ppwzMimeOutuintThe address of the determined MIME type.
dwReserverduintReserved. Must be set to zero.
Returns
- uint
Returns S_OK if successful.
FreeLibrary(nint)
Frees the loaded dynamic-link library module.
public static extern bool FreeLibrary(nint hModule)
Parameters
hModulenintA handle to the loaded library module.
Returns
- bool
True if the function succeeds.
GetClipboardData(uint)
Retrieves data from the clipboard in a specified format.
public static extern nint GetClipboardData(uint uFormat)
Parameters
uFormatuintThe clipboard format.
Returns
- nint
A handle to the clipboard data in the specified format.
GetCurrentProcess()
Retrieves a pseudo handle for the current process.
public static extern nint GetCurrentProcess()
Returns
- nint
A pseudo handle to the current process.
GetDeviceCaps(nint, int)
Retrieves device-specific information for the specified device.
public static extern int GetDeviceCaps(nint hdc, int nIndex)
Parameters
Returns
- int
The value of the desired item.
GetFileInformationByHandle(nint, ref BY_HANDLE_FILE_INFORMATION)
Retrieves file information for the specified file.
public static extern bool GetFileInformationByHandle(nint handle, ref W32.BY_HANDLE_FILE_INFORMATION hfi)
Parameters
handlenintA handle to the file.
hfiW32.BY_HANDLE_FILE_INFORMATIONA pointer to a BY_HANDLE_FILE_INFORMATION structure that receives the file information.
Returns
- bool
True if the function succeeds.
GetFileInformationByHandleWorker(string, out int)
Retrieves the number of hard links for a file by its path.
public static uint GetFileInformationByHandleWorker(string filePath, out int lastError)
Parameters
filePathstringThe full path to the file.
lastErrorintWhen this method returns, contains the last Win32 error code if the operation failed.
Returns
- uint
The number of hard links, or uint.MaxValue if the operation failed.
GetKeyState(int)
Retrieves the status of the specified virtual key.
public static extern short GetKeyState(int keyCode)
Parameters
keyCodeintA virtual key code.
Returns
- short
The status of the specified virtual key.
GetLastError()
Retrieves the calling thread's last-error code value. Use Marshal.GetLastWin32Error instead.
public static extern uint GetLastError()
Returns
- uint
The calling thread's last-error code.
GetModuleFileNameEx(nint, nint, StringBuilder, int)
Retrieves the fully qualified path for the file containing the specified module.
public static extern uint GetModuleFileNameEx(nint hProcess, nint hModule, StringBuilder lpBaseName, int nSize)
Parameters
hProcessnintA handle to the process.
hModulenintA handle to the module. If NULL, returns the path of the executable.
lpBaseNameStringBuilderA buffer that receives the fully qualified path to the module.
nSizeintThe size of the buffer, in characters.
Returns
- uint
The length of the string copied to the buffer.
GetModuleHandle(string)
Retrieves the handle to the specified module.
public static extern nint GetModuleHandle(string lpModuleName)
Parameters
lpModuleNamestringThe name of the loaded module. If NULL, returns a handle to the calling process.
Returns
- nint
A handle to the specified module, or IntPtr.Zero on failure.
GetShellWindow()
Retrieves a handle to the Shell's desktop window.
public static extern nint GetShellWindow()
Returns
- nint
A handle to the Shell's desktop window.
GetWindowThreadProcessId(nint, out uint)
Retrieves the identifier of the thread that created the specified window and the identifier of the process.
public static extern uint GetWindowThreadProcessId(nint hWnd, out uint lpdwProcessId)
Parameters
hWndnintA handle to the window.
lpdwProcessIduintA pointer that receives the process identifier.
Returns
- uint
The identifier of the thread that created the window.
GlobalFree(nint)
Frees the specified global memory object and invalidates its handle.
public static extern nint GlobalFree(nint hMem)
Parameters
hMemnintA handle to the global memory object.
Returns
- nint
If the function succeeds, the return value is NULL.
GlobalLock(nint)
Locks a global memory object and returns a pointer to the first byte of the object's memory block.
public static extern nint GlobalLock(nint hMem)
Parameters
hMemnintA handle to the global memory object.
Returns
- nint
A pointer to the first byte of the memory block.
GlobalSize(nint)
Retrieves the current size of the specified global memory object, in bytes.
public static extern nuint GlobalSize(nint hMem)
Parameters
hMemnintA handle to the global memory object.
Returns
- nuint
The size of the specified global memory object, in bytes.
GlobalUnlock(nint)
Decrements the lock count associated with a memory object.
public static extern bool GlobalUnlock(nint hMem)
Parameters
hMemnintA handle to the global memory object.
Returns
- bool
True if the memory object is still locked after decrementing the lock count.
IsClipboardFormatAvailable(uint)
Determines whether the clipboard contains data in the specified format.
public static extern bool IsClipboardFormatAvailable(uint format)
Parameters
formatuintThe clipboard format to check.
Returns
- bool
True if the clipboard format is available.
LoadLibrary(string)
Loads the specified module into the address space of the calling process.
public static extern nint LoadLibrary(string lpFileName)
Parameters
lpFileNamestringThe name of the module to load.
Returns
- nint
A handle to the loaded module, or IntPtr.Zero on failure.
LookupPrivilegeValue(string, string, ref LUID)
Retrieves the locally unique identifier (LUID) used on a specified system to locally represent the specified privilege name.
public static extern bool LookupPrivilegeValue(string host, string name, ref LUID pluid)
Parameters
hoststringA string that specifies the name of the system.
namestringA string that specifies the name of the privilege.
pluidLUIDA pointer to an LUID variable that receives the LUID.
Returns
- bool
True if the function succeeds.
OpenClipboard(nint)
Opens the clipboard for examination and prevents other applications from modifying the clipboard content.
public static extern bool OpenClipboard(nint hWndNewOwner)
Parameters
hWndNewOwnernintA handle to the window to be associated with the open clipboard.
Returns
- bool
True if the function succeeds.
OpenProcess(ProcessAccessFlags, bool, uint)
Opens an existing local process object with specified access flags.
public static extern nint OpenProcess(ProcessAccessFlags processAccess, bool bInheritHandle, uint processId)
Parameters
processAccessProcessAccessFlagsThe access rights for the process object.
bInheritHandleboolIf true, processes created by this process will inherit the handle.
processIduintThe identifier of the local process to be opened.
Returns
- nint
An open handle to the specified process.
OpenProcess(uint, bool, int)
Opens an existing local process object.
public static extern nint OpenProcess(uint processAccess, bool bInheritHandle, int processId)
Parameters
processAccessuintThe access to the process object.
bInheritHandleboolIf true, processes created by this process will inherit the handle.
processIdintThe identifier of the local process to be opened.
Returns
- nint
An open handle to the specified process, or IntPtr.Zero on failure.
OpenProcessToken(nint, int, ref nint)
Opens the access token associated with a process.
public static extern bool OpenProcessToken(nint h, int acc, ref nint phtok)
Parameters
hnintA handle to the process whose access token is opened.
accintSpecifies an access mask that specifies the requested types of access to the access token.
phtoknintA pointer to a handle that identifies the newly opened access token when the function returns.
Returns
- bool
True if the function succeeds.
ProcessHoldingClipboard()
Retrieves the process that currently has the clipboard open.
public static Process? ProcessHoldingClipboard()
Returns
- Process
The process holding the clipboard, or null if no process has it open.
RemoveClipboardFormatListener(nint)
Removes the given window from the system-maintained clipboard format listener list.
public static extern bool RemoveClipboardFormatListener(nint hwnd)
Parameters
hwndnintA handle to the window to remove from the clipboard format listener list.
Returns
- bool
True if the function succeeds.
SHGetFileInfo(string, uint, out SHFILEINFO, uint, uint)
Retrieves information about an object in the file system, such as a file, folder, directory, or drive root.
public static extern nint SHGetFileInfo(string pszPath, uint dwFileAttributes, out SHFILEINFO psfi, uint cbFileInfo, uint uFlags)
Parameters
pszPathstringA pointer to a string that specifies the path of the file or folder.
dwFileAttributesuintA combination of file attribute flags.
psfiSHFILEINFOA pointer to a SHFILEINFO structure to receive the file information.
cbFileInfouintThe size, in bytes, of the SHFILEINFO structure.
uFlagsuintThe flags that specify the file information to retrieve.
Returns
- nint
Depends on the flags parameter.
SHGetKnownFolderPath(Guid, uint, nint, out string)
Retrieves the full path of a known folder identified by the folder's KNOWNFOLDERID.
public static extern int SHGetKnownFolderPath(Guid rfid, uint dwFlags, nint hToken, out string pszPath)
Parameters
rfidGuidA reference to the KNOWNFOLDERID that identifies the folder.
dwFlagsuintFlags that specify special retrieval options.
hTokennintAn access token that represents a particular user.
pszPathstringWhen this method returns, contains the address of a string that specifies the path of the known folder.
Returns
- int
Returns S_OK if successful, or an error value otherwise.
SendMessage(nint, int, nint, nint)
Sends the specified message to a window or windows.
public static extern int SendMessage(nint hwnd, int wMsg, nint wParam, nint lParam)
Parameters
hwndnintA handle to the window whose window procedure will receive the message.
wMsgintThe message to be sent.
wParamnintAdditional message-specific information.
lParamnintAdditional message-specific information.
Returns
- int
The result of the message processing.
SetClipboardData(uint, nint)
Places data on the clipboard in a specified clipboard format.
public static extern bool SetClipboardData(uint uFormat, nint data)
Parameters
Returns
- bool
True if the function succeeds.
SetClipboardViewer(nint)
Adds the specified window to the chain of clipboard viewers.
public static extern nint SetClipboardViewer(nint hWndNewViewer)
Parameters
hWndNewViewernintA handle to the window to be added to the clipboard chain.
Returns
- nint
A handle to the next window in the clipboard viewer chain.
SetConsoleIcon(nint)
Sets the icon for the console window.
public static extern bool SetConsoleIcon(nint hIcon)
Parameters
hIconnintA handle to the icon to be displayed.
Returns
- bool
True if the function succeeds.
SetForegroundWindow(nint)
Brings the thread that created the specified window into the foreground and activates the window.
public static extern bool SetForegroundWindow(nint hWnd)
Parameters
hWndnintA handle to the window that should be activated.
Returns
- bool
True if the window was brought to the foreground.
SetWindowsHookEx(int, HookProc, nint, int)
Installs an application-defined hook procedure into a hook chain. You would install a hook procedure to monitor the system for certain types of events.
public static extern nint SetWindowsHookEx(int idHook, W32Base.HookProc lpfn, nint hMod, int dwThreadId)
Parameters
idHookintThe type of hook procedure to be installed.
lpfnW32Base.HookProcA pointer to the hook procedure.
hModnintA handle to the DLL containing the hook procedure.
dwThreadIdintThe identifier of the thread with which the hook procedure is to be associated.
Returns
- nint
If the function succeeds, the return value is the handle to the hook procedure.
SetWindowsHookEx(int, LowLevelKeyboardProc, nint, uint)
Installs an application-defined hook procedure into a hook chain (low-level keyboard version).
public static extern nint SetWindowsHookEx(int idHook, W32Base.LowLevelKeyboardProc lpfn, nint hMod, uint dwThreadId)
Parameters
idHookintThe type of hook procedure to be installed.
lpfnW32Base.LowLevelKeyboardProcA pointer to the hook procedure.
hModnintA handle to the DLL containing the hook procedure.
dwThreadIduintThe identifier of the thread with which the hook procedure is to be associated.
Returns
- nint
If the function succeeds, the return value is the handle to the hook procedure.
UnhookWindowsHookEx(nint)
Removes a hook procedure installed in a hook chain by the SetWindowsHookEx function.
public static extern bool UnhookWindowsHookEx(nint hHook)
Parameters
hHooknintA handle to the hook to be removed.
Returns
- bool
True if the function succeeds.