using System; using System.Runtime.InteropServices; /** * Created on Jun 05, 2016 * by Daniel Morariu */ namespace WinApiClass { public class WinApiClass { public delegate uint LPTHREAD_START_ROUTINE(IntPtr lpParam); //constants public const int STILL_ACTIVE = 259; //strutures #region API_PROCESS [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] public struct STARTUPINFOEX { public STARTUPINFO StartupInfo; public IntPtr lpAttributeList; } [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] public struct STARTUPINFO { public Int32 cb; public string lpReserved; public string lpDesktop; public string lpTitle; public Int32 dwX; public Int32 dwY; public Int32 dwXSize; public Int32 dwYSize; public Int32 dwXCountChars; public Int32 dwYCountChars; public Int32 dwFillAttribute; public Int32 dwFlags; public Int16 wShowWindow; public Int16 cbReserved2; public IntPtr lpReserved2; public IntPtr hStdInput; public IntPtr hStdOutput; public IntPtr hStdError; } [StructLayout(LayoutKind.Sequential)] public struct PROCESS_INFORMATION { public IntPtr hProcess; public IntPtr hThread; public int dwProcessId; public int dwThreadId; } public enum CreationFlags { CREATE_DEFAULT_ERROR_MODE = 0x04000000, CREATE_NEW_CONSOLE = 0x00000010, CREATE_NEW_PROCESS_GROUP = 0x00000200, CREATE_NO_WINDOW = 0x08000000, CREATE_PROTECTED_PROCESS = 0x00040000, CREATE_SEPARATE_WOW_VDM = 0x00000800, CREATE_SHARED_WOW_VDM = 0x00001000, CREATE_SUSPENDED = 0x00000004 } [StructLayout(LayoutKind.Sequential)] public struct SECURITY_ATTRIBUTES { public int nLength; public IntPtr lpSecurityDescriptor; public int bInheritHandle; } [StructLayout(LayoutKind.Sequential)] public struct PROCESSENTRY32 { public uint dwSize; public uint cntUsage; public uint th32ProcessID; public IntPtr th32DefaultHeapID; public uint th32ModuleID; public uint cntThreads; public uint th32ParentProcessID; public int pcPriClassBase; public uint dwFlags; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)] public string szExeFile; }; [DllImport("kernel32.dll")] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool CreateProcess( string lpApplicationName, string lpCommandLine, ref SECURITY_ATTRIBUTES lpProcessAttributes, ref SECURITY_ATTRIBUTES lpThreadAttributes, bool bInheritHandles, uint dwCreationFlags, IntPtr lpEnvironment, string lpCurrentDirectory, [In] ref STARTUPINFO lpStartupInfo, out PROCESS_INFORMATION lpProcessInformation); [DllImport("kernel32.dll",EntryPoint="GetStartupInfoW")] public static extern void GetStartupInfo(out STARTUPINFO lpStartupInfo); [DllImport("Kernel32.dll")] public static extern IntPtr CreateToolhelp32Snapshot(uint flags, uint processid); [DllImport("kernel32.dll")] public static extern bool Process32First(IntPtr hSnapshot, ref PROCESSENTRY32 lppe); [DllImport("kernel32.dll")] public static extern bool Process32Next(IntPtr hSnapshot, ref PROCESSENTRY32 lppe); [DllImport("kernel32.dll")] public static extern IntPtr OpenProcess( int dwDesiredAccess, bool bInheritHandle, int dwProcessId); [DllImport("kernel32.dll", SetLastError = true)] public static extern bool TerminateProcess(IntPtr hProcess, uint uExitCode); #endregion #region API_THREADS public enum ThreadState { RUN = 0x00010000, CREATE_SUSPENDED = 0x00000004 } public enum ThreadPriority { THREAD_MODE_BACKGROUND_BEGIN = 0x00010000, THREAD_MODE_BACKGROUND_END = 0x00020000, THREAD_PRIORITY_ABOVE_NORMAL = 1, THREAD_PRIORITY_BELOW_NORMAL = -1, THREAD_PRIORITY_HIGHEST = 2, THREAD_PRIORITY_IDLE = -15, THREAD_PRIORITY_LOWEST = -2, THREAD_PRIORITY_NORMAL = 0, THREAD_PRIORITY_TIME_CRITICAL = 15 } [DllImport("kernel32")] public static extern uint CreateThread( IntPtr lpThreadAttributes, UInt32 dwStackSize, LPTHREAD_START_ROUTINE lpStartAddress, IntPtr lpParameter, ThreadState dwCreationFlags, out uint lpThreadId); [DllImport("Kernel32.dll")] public static extern int TerminateThread(uint hThread, uint exitcode); [DllImport("Kernel32.dll")] public static extern bool GetExitCodeThread(uint handle, out uint lpExitCode); [DllImport("kernel32.dll")] public static extern uint ResumeThread(IntPtr hThread); [DllImport("kernel32.dll")] public static extern uint SuspendThread(IntPtr hThread); [DllImport("kernel32.dll")] public static extern bool SetThreadPriority(IntPtr hThread, ThreadPriority nPriority); [DllImport("kernel32.dll")] public static extern bool GetThreadTimes(IntPtr hThread, out FILETIME lpCreationTime, out FILETIME lpExitTime, out FILETIME lpKernelTime, out FILETIME lpUserTime); /** * Exemplu: //definirea unei variabile de tip THREAD_START_ROUTINE care memoreaza adresa functiei care se va apela pentru threadul respectiv WinApiClass.WinApiClass.LPTHREAD_START_ROUTINE lpThread = new WinApiClass.WinApiClass.LPTHREAD_START_ROUTINE(numele functie care se doreste apelata); //convertirea parametrului in memorea reala pentru trimiterea catre fir a acestuia GCHandle handle1 = GCHandle.Alloc(adresa componenta de pe forma care se doreste a fi trimisa ca si parametru); //functia care este executata de catre fir public static uint FunctieThread(IntPtr lpParam) { //preluarea parametrului si convertirea acestuia la tipul de baza pe care la avut inainte de apel GCHandle handle2 = (GCHandle)lpParam; //pentru preluarea informatiilor din parametrul functiei Tip_Parametru pb = (handle2.Target as Tip_Parametru); MethodInvoker m = new MethodInvoker(() => metoda de pe forma (din alt thread) care se doreste apelata); //apelul metodei pb.Invoke(m); } **/ #endregion #region API_STRUCTURES [StructLayout(LayoutKind.Sequential)] public struct FILETIME { public uint DateTimeLow; public uint DateTimeHigh; } [StructLayout(LayoutKind.Sequential)] public struct SYSTEMTIME { [MarshalAs(UnmanagedType.U2)] public short Year; [MarshalAs(UnmanagedType.U2)] public short Month; [MarshalAs(UnmanagedType.U2)] public short DayOfWeek; [MarshalAs(UnmanagedType.U2)] public short Day; [MarshalAs(UnmanagedType.U2)] public short Hour; [MarshalAs(UnmanagedType.U2)] public short Minute; [MarshalAs(UnmanagedType.U2)] public short Second; [MarshalAs(UnmanagedType.U2)] public short Milliseconds; public SYSTEMTIME(DateTime dt) { dt = dt.ToUniversalTime(); // SetSystemTime expects the SYSTEMTIME in UTC Year = (short)dt.Year; Month = (short)dt.Month; DayOfWeek = (short)dt.DayOfWeek; Day = (short)dt.Day; Hour = (short)dt.Hour; Minute = (short)dt.Minute; Second = (short)dt.Second; Milliseconds = (short)dt.Millisecond; } } #endregion #region API_FUNCTIONS [DllImport("Kernel32.dll")] public static extern int GetLastError(); [DllImport("Kernel32.dll")] public static extern int CloseHandle(uint hObject); [DllImport("kernel32.dll")] public static extern bool FileTimeToSystemTime([In] ref FILETIME lpFileTime, out SYSTEMTIME lpSystemTime); #endregion #region SECTIUNI_CRITICE [StructLayout(LayoutKind.Sequential)] public struct CRITICAL_SECTION { int DebugInfo; // // The following three fields control entering and exiting the critical // section for the resource // UInt32 LockCount; UInt32 RecursionCount; IntPtr OwningThread; // from the thread's ClientId->UniqueThread IntPtr LockSemaphore; UIntPtr SpinCount; // force size on 64-bit systems when packed } [DllImport("kernel32.dll")] public static extern void InitializeCriticalSection(out CRITICAL_SECTION lpCriticalSection); [DllImport("kernel32.dll")] public static extern void DeleteCriticalSection(ref CRITICAL_SECTION lpCriticalSection); [DllImport("kernel32.dll")] public static extern void EnterCriticalSection(ref CRITICAL_SECTION lpCriticalSection); [DllImport("kernel32.dll")] public static extern void LeaveCriticalSection(ref CRITICAL_SECTION lpCriticalSection); [DllImport("kernel32.dll")] public static extern bool TryEnterCriticalSection(ref CRITICAL_SECTION lpCriticalSection); #endregion #region ACCESS_FISIER [Flags] public enum FileAccess : uint { // // Standart Section // AccessSystemSecurity = 0x1000000, // AccessSystemAcl access type MaximumAllowed = 0x2000000, // MaximumAllowed access type Delete = 0x10000, ReadControl = 0x20000, WriteDAC = 0x40000, WriteOwner = 0x80000, Synchronize = 0x100000, StandardRightsRequired = 0xF0000, StandardRightsRead = ReadControl, StandardRightsWrite = ReadControl, StandardRightsExecute = ReadControl, StandardRightsAll = 0x1F0000, SpecificRightsAll = 0xFFFF, FILE_READ_DATA = 0x0001, // file & pipe FILE_LIST_DIRECTORY = 0x0001, // directory FILE_WRITE_DATA = 0x0002, // file & pipe FILE_ADD_FILE = 0x0002, // directory FILE_APPEND_DATA = 0x0004, // file FILE_ADD_SUBDIRECTORY = 0x0004, // directory FILE_CREATE_PIPE_INSTANCE = 0x0004, // named pipe FILE_READ_EA = 0x0008, // file & directory FILE_WRITE_EA = 0x0010, // file & directory FILE_EXECUTE = 0x0020, // file FILE_TRAVERSE = 0x0020, // directory FILE_DELETE_CHILD = 0x0040, // directory FILE_READ_ATTRIBUTES = 0x0080, // all FILE_WRITE_ATTRIBUTES = 0x0100, // all // // Generic Section // GenericRead = 0x80000000, GenericWrite = 0x40000000, GenericExecute = 0x20000000, GenericAll = 0x10000000, SPECIFIC_RIGHTS_ALL = 0x00FFFF, FILE_ALL_ACCESS = StandardRightsRequired | Synchronize | 0x1FF, FILE_GENERIC_READ = StandardRightsRead | FILE_READ_DATA | FILE_READ_ATTRIBUTES | FILE_READ_EA | Synchronize, FILE_GENERIC_WRITE = StandardRightsWrite | FILE_WRITE_DATA | FILE_WRITE_ATTRIBUTES | FILE_WRITE_EA | FILE_APPEND_DATA | Synchronize, FILE_GENERIC_EXECUTE = StandardRightsExecute | FILE_READ_ATTRIBUTES | FILE_EXECUTE | Synchronize } [Flags] public enum FileShare : uint { /// /// /// None = 0x00000000, /// /// Enables subsequent open operations on an object to request read access. /// Otherwise, other processes cannot open the object if they request read access. /// If this flag is not specified, but the object has been opened for read access, the function fails. /// Read = 0x00000001, /// /// Enables subsequent open operations on an object to request write access. /// Otherwise, other processes cannot open the object if they request write access. /// If this flag is not specified, but the object has been opened for write access, the function fails. /// Write = 0x00000002, /// /// Enables subsequent open operations on an object to request delete access. /// Otherwise, other processes cannot open the object if they request delete access. /// If this flag is not specified, but the object has been opened for delete access, the function fails. /// Delete = 0x00000004 } public enum CreationDisposition : uint { /// /// Creates a new file. The function fails if a specified file exists. /// New = 1, /// /// Creates a new file, always. /// If a file exists, the function overwrites the file, clears the existing attributes, combines the specified file attributes, /// and flags with FILE_ATTRIBUTE_ARCHIVE, but does not set the security descriptor that the SECURITY_ATTRIBUTES structure specifies. /// CreateAlways = 2, /// /// Opens a file. The function fails if the file does not exist. /// OpenExisting = 3, /// /// Opens a file, always. /// If a file does not exist, the function creates a file as if dwCreationDisposition is CREATE_NEW. /// OpenAlways = 4, /// /// Opens a file and truncates it so that its size is 0 (zero) bytes. The function fails if the file does not exist. /// The calling process must open the file with the GENERIC_WRITE access right. /// TruncateExisting = 5 } [Flags] public enum FileAttributes : uint { Readonly = 0x00000001, Hidden = 0x00000002, System = 0x00000004, Directory = 0x00000010, Archive = 0x00000020, Device = 0x00000040, Normal = 0x00000080, Temporary = 0x00000100, SparseFile = 0x00000200, ReparsePoint = 0x00000400, Compressed = 0x00000800, Offline = 0x00001000, NotContentIndexed = 0x00002000, Encrypted = 0x00004000, Write_Through = 0x80000000, Overlapped = 0x40000000, NoBuffering = 0x20000000, RandomAccess = 0x10000000, SequentialScan = 0x08000000, DeleteOnClose = 0x04000000, BackupSemantics = 0x02000000, PosixSemantics = 0x01000000, OpenReparsePoint = 0x00200000, OpenNoRecall = 0x00100000, FirstPipeInstance = 0x00080000 } [Flags] public enum OpenFileStyle : uint { OF_CANCEL = 0x00000800, // Ignored. For a dialog box with a Cancel button, use OF_PROMPT. OF_CREATE = 0x00001000, // Creates a new file. If file exists, it is truncated to zero (0) length. OF_DELETE = 0x00000200, // Deletes a file. OF_EXIST = 0x00004000, // Opens a file and then closes it. Used to test that a file exists OF_PARSE = 0x00000100, // Fills the OFSTRUCT structure, but does not do anything else. OF_PROMPT = 0x00002000, // Displays a dialog box if a requested file does not exist OF_READ = 0x00000000, // Opens a file for reading only. OF_READWRITE = 0x00000002, // Opens a file with read/write permissions. OF_REOPEN = 0x00008000, // Opens a file by using information in the reopen buffer. // For MS-DOS–based file systems, opens a file with compatibility mode, allows any process on a // specified computer to open the file any number of times. // Other efforts to open a file with other sharing modes fail. This flag is mapped to the // FILE_SHARE_READ|FILE_SHARE_WRITE flags of the CreateFile function. OF_SHARE_COMPAT = 0x00000000, // Opens a file without denying read or write access to other processes. // On MS-DOS-based file systems, if the file has been opened in compatibility mode // by any other process, the function fails. // This flag is mapped to the FILE_SHARE_READ|FILE_SHARE_WRITE flags of the CreateFile function. OF_SHARE_DENY_NONE = 0x00000040, // Opens a file and denies read access to other processes. // On MS-DOS-based file systems, if the file has been opened in compatibility mode, // or for read access by any other process, the function fails. // This flag is mapped to the FILE_SHARE_WRITE flag of the CreateFile function. OF_SHARE_DENY_READ = 0x00000030, // Opens a file and denies write access to other processes. // On MS-DOS-based file systems, if a file has been opened in compatibility mode, // or for write access by any other process, the function fails. // This flag is mapped to the FILE_SHARE_READ flag of the CreateFile function. OF_SHARE_DENY_WRITE = 0x00000020, // Opens a file with exclusive mode, and denies both read/write access to other processes. // If a file has been opened in any other mode for read/write access, even by the current process, // the function fails. OF_SHARE_EXCLUSIVE = 0x00000010, // Verifies that the date and time of a file are the same as when it was opened previously. // This is useful as an extra check for read-only files. OF_VERIFY = 0x00000400, // Opens a file for write access only. OF_WRITE = 0x00000001 } [System.Runtime.InteropServices.StructLayout(LayoutKind.Sequential)] public struct OFSTRUCT { public byte cBytes; public byte fFixedDisc; public UInt16 nErrCode; public UInt16 Reserved1; public UInt16 Reserved2; [System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst = 128)] public string szPathName; } public enum FileMode : uint { CREATE_ALWAYS = 2, CREATE_NEW = 1, OPEN_ALWAYS = 4, OPEN_EXISTING = 3, TRUNCATE_EXISTING = 5 } [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)] public static extern IntPtr CreateFile( [MarshalAs(UnmanagedType.LPTStr)] string filename, [MarshalAs(UnmanagedType.U4)] FileAccess access, [MarshalAs(UnmanagedType.U4)] FileShare share, IntPtr securityAttributes, // optional SECURITY_ATTRIBUTES struct or IntPtr.Zero [MarshalAs(UnmanagedType.U4)] FileMode creationDisposition, [MarshalAs(UnmanagedType.U4)] FileAttributes flagsAndAttributes, IntPtr templateFile); [DllImport("kernel32.dll", BestFitMapping = false, ThrowOnUnmappableChar = true)] public static extern int OpenFile([System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPStr)]string lpFileName, out OFSTRUCT lpReOpenBuff, OpenFileStyle uStyle); [DllImport("kernel32.dll", SetLastError = true)] public static extern bool ReadFile(IntPtr hFile, [Out] byte[] lpBuffer, uint nNumberOfBytesToRead, out uint lpNumberOfBytesRead, IntPtr lpOverlapped); [DllImport("kernel32.dll")] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool WriteFile(IntPtr hFile, byte[] lpBuffer, uint nNumberOfBytesToWrite, out uint lpNumberOfBytesWritten, IntPtr lpOverlapped); #endregion #region EVENIMENTE public const UInt32 INFINITE = 0xFFFFFFFF; public const UInt32 WAIT_ABANDONED = 0x00000080; public const UInt32 WAIT_OBJECT_0 = 0x00000000; public const UInt32 WAIT_TIMEOUT = 0x00000102; [DllImport("kernel32.dll", SetLastError = true)] public static extern UInt32 WaitForSingleObject(IntPtr hHandle, UInt32 dwMilliseconds); [DllImport("kernel32.dll")] public static extern uint WaitForMultipleObjects(uint nCount, IntPtr[] lpHandles, bool bWaitAll, uint dwMilliseconds); [DllImport("kernel32.dll")] public static extern IntPtr CreateEvent(IntPtr lpEventAttributes, bool bManualReset, bool bInitialState, string lpName); [DllImport("Kernel32.dll", SetLastError = true)] public static extern IntPtr OpenEvent(uint dwDesiredAccess, bool bInheritHandle, string lpName); [DllImport("kernel32.dll")] public static extern bool SetEvent(IntPtr hEvent); [DllImport("kernel32.dll")] public static extern bool ResetEvent(IntPtr hEvent); [DllImport("kernel32.dll")] public static extern bool PulseEvent(IntPtr hEvent); #endregion #region MUTEX public const int ERROR_ALREADY_EXISTS = 183; [DllImport("kernel32.dll")] public static extern IntPtr CreateMutex(IntPtr lpMutexAttributes, bool bInitialOwner, string lpName); [DllImport("kernel32.dll")] public static extern IntPtr OpenMutex(uint dwDesiredAccess, bool bInheritHandle, string lpName); [DllImport("kernel32.dll")] public static extern bool ReleaseMutex(IntPtr hMutex); #endregion #region SEMAPHORE [DllImport("kernel32.dll")] public static extern IntPtr CreateSemaphore(ref SECURITY_ATTRIBUTES securityAttributes, int initialCount, int maximumCount, string name); [DllImport("kernel32.dll")] static extern IntPtr OpenSemaphore(uint dwDesiredAccess, [MarshalAs(UnmanagedType.Bool)] bool bInheritHandle, string lpName); [DllImport("kernel32.dll")] static extern bool ReleaseSemaphore(IntPtr hSemaphore, int lReleaseCount, IntPtr lpPreviousCount); #endregion #region WAITABLETIMER [DllImport("kernel32.dll")] public static extern IntPtr CreateWaitableTimer(ref SECURITY_ATTRIBUTES lpTimerAttributes, bool bManualReset, string lpTimerName); public delegate void TimerAPCProc(IntPtr completionArg, UInt32 timerLowValue, UInt32 timerHighValue); [DllImport("kernel32.dll")] public static extern bool SetWaitableTimer(IntPtr hTimer, [In] ref long pDueTime, int lPeriod, TimerAPCProc pfnCompletionRoutine, IntPtr lpArgToCompletionRoutine, bool fResume); #endregion public WinApiClass() { } } }