Table of Contents

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

int

CREATE_NEW

Creates a new file only if it does not already exist.

public const int CREATE_NEW = 1

Field Value

int

ERROR_INSUFFICIENT_BUFFER

Error code indicating the buffer is too small.

public const int ERROR_INSUFFICIENT_BUFFER = 122

Field Value

int

FILE_ATTRIBUTE_DIRECTORY

File attribute flag indicating a directory.

public const uint FILE_ATTRIBUTE_DIRECTORY = 16

Field Value

uint

FILE_ATTRIBUTE_NORMAL

File attribute flag indicating a normal file.

public const uint FILE_ATTRIBUTE_NORMAL = 128

Field Value

uint

FILE_SHARE_DELETE

Enables subsequent delete operations on a file.

public const int FILE_SHARE_DELETE = 4

Field Value

int

FILE_SHARE_READ

Enables subsequent read operations on a file.

public const int FILE_SHARE_READ = 1

Field Value

int

FILE_SHARE_WRITE

Enables subsequent write operations on a file.

public const int FILE_SHARE_WRITE = 2

Field Value

int

GENERIC_READ

Generic read access flag.

public const uint GENERIC_READ = 2147483648

Field Value

uint

HWND_MESSAGE

Handle value used to find message-only windows via FindWindowEx.

public static readonly nint HWND_MESSAGE

Field Value

nint

INVALID_HANDLE_VALUE

Constant representing an invalid handle value.

public const int INVALID_HANDLE_VALUE = -1

Field Value

int

OPEN_ALWAYS

Opens a file, always. Creates the file if it does not exist.

public const int OPEN_ALWAYS = 4

Field Value

int

OPEN_EXISTING

Opens a file only if it exists.

public const int OPEN_EXISTING = 3

Field Value

int

SHGFI_ADDOVERLAYS

Flag to apply the appropriate overlays.

public const uint SHGFI_ADDOVERLAYS = 32

Field Value

uint

SHGFI_ATTRIBUTES

Flag to retrieve the attributes.

public const uint SHGFI_ATTRIBUTES = 2048

Field Value

uint

SHGFI_ATTR_SPECIFIED

Flag to get only specified attributes.

public const uint SHGFI_ATTR_SPECIFIED = 131072

Field Value

uint

SHGFI_DISPLAYNAME

Flag to retrieve the display name.

public const uint SHGFI_DISPLAYNAME = 512

Field Value

uint

SHGFI_EXETYPE

Flag to return the exe type.

public const uint SHGFI_EXETYPE = 8192

Field Value

uint

SHGFI_ICON

Flag to retrieve the icon handle.

public const uint SHGFI_ICON = 256

Field Value

uint

SHGFI_ICONLOCATION

Flag to retrieve the icon location.

public const uint SHGFI_ICONLOCATION = 4096

Field Value

uint

SHGFI_LARGEICON

Flag to retrieve the large icon.

public const uint SHGFI_LARGEICON = 0

Field Value

uint

SHGFI_LINKOVERLAY

Flag to put a link overlay on the icon.

public const uint SHGFI_LINKOVERLAY = 32768

Field Value

uint

SHGFI_OPENICON

Flag to retrieve the open icon.

public const uint SHGFI_OPENICON = 2

Field Value

uint

SHGFI_OVERLAYINDEX

Flag to get the index of the overlay.

public const uint SHGFI_OVERLAYINDEX = 64

Field Value

uint

SHGFI_PIDL

Indicates that pszPath is a PIDL.

public const uint SHGFI_PIDL = 8

Field Value

uint

SHGFI_SELECTED

Flag to show icon in selected state.

public const uint SHGFI_SELECTED = 65536

Field Value

uint

SHGFI_SHELLICONSIZE

Flag to retrieve the shell size icon.

public const uint SHGFI_SHELLICONSIZE = 4

Field Value

uint

SHGFI_SMALLICON

Flag to retrieve the small icon.

public const uint SHGFI_SMALLICON = 1

Field Value

uint

SHGFI_SYSICONINDEX

Flag to get the system icon index.

public const uint SHGFI_SYSICONINDEX = 16384

Field Value

uint

SHGFI_TYPENAME

Flag to retrieve the type name.

public const uint SHGFI_TYPENAME = 1024

Field Value

uint

SHGFI_USEFILEATTRIBUTES

Flag to use the passed dwFileAttribute.

public const uint SHGFI_USEFILEATTRIBUTES = 16

Field Value

uint

TRUNCATE_EXISTING

Opens a file and truncates it so that its size is zero bytes.

public const int TRUNCATE_EXISTING = 5

Field Value

int

WM_CLIPBOARDUPDATE

Message sent when the contents of the clipboard have changed.

public const int WM_CLIPBOARDUPDATE = 797

Field Value

int

Methods

AddClipboardFormatListener(nint)

Places the given window in the system-maintained clipboard format listener list.

public static extern bool AddClipboardFormatListener(nint hwnd)

Parameters

hwnd nint

A 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

htok nint

A handle to the access token that contains the privileges to be modified.

disall bool

Specifies whether the function disables all privileges.

newst TOKEN_PRIVILEGES

A pointer to a TOKEN_PRIVILEGES structure.

len int

Specifies the size, in bytes, of the buffer pointed to by the previous state parameter.

prev nint

A pointer to a buffer for the previous state.

relen nint

A 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

hHook nint

This parameter is ignored.

code int

The hook code passed to the current hook procedure.

wParam nint

The wParam value passed to the current hook procedure.

lParam nint

The 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

hWndRemove nint

A handle to the window to be removed from the chain.

hWndNewNext nint

A 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

hObject nint

A 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

lpFileName string

The name of the file or device to be created or opened.

dwDesiredAccess uint

The requested access to the file or device.

dwShareMode int

The requested sharing mode of the file or device.

lpSECURITY_ATTRIBUTES nint

A pointer to a SECURITY_ATTRIBUTES structure.

dwCreationDisposition int

An action to take on a file or device that exists or does not exist.

dwFlagsAndAttributes int

The file or device attributes and flags.

hTemplateFile nint

A 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

hToken nint

A handle to the primary token that represents a user.

dwLogonFlags int

The logon option.

lpApplicationName string

The name of the module to be executed.

lpCommandLine string

The command line to be executed.

dwCreationFlags int

The flags that control the priority class and the creation of the process.

lpEnvironment nint

A pointer to the environment block for the new process.

lpCurrentDirectory string

The full path to the current directory for the process.

lpStartupInfo STARTUPINFO

A pointer to a STARTUPINFO structure.

lpProcessInformation PROCESS_INFORMATION

A 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

hObject nint

A 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

hIcon nint

A 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

hExistingToken nint

A handle to an access token.

dwDesiredAccess uint

Specifies the requested access rights for the new token.

lpTokenAttributes nint

A pointer to a SECURITY_ATTRIBUTES structure.

impersonationLevel SECURITY_IMPERSONATION_LEVEL

Specifies the impersonation level of the new token.

tokenType TOKEN_TYPE

Specifies the type of the new token.

phNewToken nint

A 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

enable bool

True 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

format uint

A 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

pBC uint

A pointer to the bind context. Can be set to zero.

pwzUrl string

A string value that contains the URL of the data.

pBuffer byte[]

A pointer to the buffer that contains the data to be sniffed.

cbSize uint

The number of bytes in the buffer.

pwzMimeProposed string

A string value that contains the proposed MIME type.

dwMimeFlags uint

Flags that control the operation.

ppwzMimeOut uint

The address of the determined MIME type.

dwReserverd uint

Reserved. 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

hModule nint

A 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

uFormat uint

The 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

hdc nint

A handle to the device context.

nIndex int

The item to be returned.

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

handle nint

A handle to the file.

hfi W32.BY_HANDLE_FILE_INFORMATION

A 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

filePath string

The full path to the file.

lastError int

When 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

keyCode int

A 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

hProcess nint

A handle to the process.

hModule nint

A handle to the module. If NULL, returns the path of the executable.

lpBaseName StringBuilder

A buffer that receives the fully qualified path to the module.

nSize int

The 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

lpModuleName string

The 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

hWnd nint

A handle to the window.

lpdwProcessId uint

A 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

hMem nint

A 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

hMem nint

A 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

hMem nint

A 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

hMem nint

A 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

format uint

The 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

lpFileName string

The 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

host string

A string that specifies the name of the system.

name string

A string that specifies the name of the privilege.

pluid LUID

A 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

hWndNewOwner nint

A 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

processAccess ProcessAccessFlags

The access rights for the process object.

bInheritHandle bool

If true, processes created by this process will inherit the handle.

processId uint

The 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

processAccess uint

The access to the process object.

bInheritHandle bool

If true, processes created by this process will inherit the handle.

processId int

The 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

h nint

A handle to the process whose access token is opened.

acc int

Specifies an access mask that specifies the requested types of access to the access token.

phtok nint

A 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

hwnd nint

A 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

pszPath string

A pointer to a string that specifies the path of the file or folder.

dwFileAttributes uint

A combination of file attribute flags.

psfi SHFILEINFO

A pointer to a SHFILEINFO structure to receive the file information.

cbFileInfo uint

The size, in bytes, of the SHFILEINFO structure.

uFlags uint

The 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

rfid Guid

A reference to the KNOWNFOLDERID that identifies the folder.

dwFlags uint

Flags that specify special retrieval options.

hToken nint

An access token that represents a particular user.

pszPath string

When 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

hwnd nint

A handle to the window whose window procedure will receive the message.

wMsg int

The message to be sent.

wParam nint

Additional message-specific information.

lParam nint

Additional 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

uFormat uint

The clipboard format.

data nint

A handle to the data in the specified format.

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

hWndNewViewer nint

A 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

hIcon nint

A 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

hWnd nint

A 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

idHook int

The type of hook procedure to be installed.

lpfn W32Base.HookProc

A pointer to the hook procedure.

hMod nint

A handle to the DLL containing the hook procedure.

dwThreadId int

The 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

idHook int

The type of hook procedure to be installed.

lpfn W32Base.LowLevelKeyboardProc

A pointer to the hook procedure.

hMod nint

A handle to the DLL containing the hook procedure.

dwThreadId uint

The 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

hHook nint

A handle to the hook to be removed.

Returns

bool

True if the function succeeds.