Windows API functions commonly used Windows API functions --- reprint

Windows API function commonly used

14:21 2014-10-15  xiashengwang   reading ( 2105) Comments ( 0)  edit   collections

Although .Net class library strong, but still sometimes limited functionality, conquer some api functions, we will solve the problem of providing another idea, here are their common to the Api function, for future reference.

I know api function, but do not know how c # or VB.net that statement, you can check this site: http: //pinvoke.net/

1,SetForegroundWindow

The window to the foreground, provided that the window is not minimized.

[DllImport("User32.dll")]

public static extern bool SetForegroundWindow(IntPtr hWnd);

2,ShowWindowAsync

Display window, such as to minimize the normal, which is asynchronous.

[DllImport("User32.dll")]

public static extern bool ShowWindowAsync(IntPtr hWnd, int cmdShow);

public enum ShowState : int

{

SW_HIDE = 0,

SW_SHOWNORMAL = 1,

SW_NORMAL = 1,

SW_SHOWMINIMIZED = 2,

SW_SHOWMAXIMIZED = 3,

SW_MAXIMIZE = 3,

SW_SHOWNOACTIVATE = 4,

SW_SHOW = 5,

SW_MINIMIZE = 6,

SW_SHOWMINNOACTIVE = 7,

SW_SHOWNA = 8,

SW_RESTORE = 9,

SW_SHOWDEFAULT = 10,

SW_FORCEMINIMIZE = 11,

SW_MAX = 11

}

3,SendMessage

Inter-process communication, there must be a window to accept the message loop for the job. WM_COPYDATA

public const int WM_COPYDATA = 0x004A;

public struct COPYDATASTRUCT

{

public IntPtr dwData;

public int cbData;

[MarshalAs(UnmanagedType.LPStr)]

public string lpData;

}

[DllImport("User32.dll", EntryPoint = "SendMessage")]

public static extern int SendMessage(

IntPtr hWnd, // handle to destination window

int Msg, // message

int wParam, // first message parameter

ref COPYDATASTRUCT lParam // second message parameter

);

example:

sender:

byte[] sarr = System.Text.Encoding.Default.GetBytes(args[0]);

Winn32.COPYDATASTRUCT copyData = new Winn32.COPYDATASTRUCT();

copyData.cbData = sarr.Length + 1;

copyData.lpData = args[0];

copyData.dwData = (IntPtr) 100; // just write here what numbers

Winn32.SendMessage(runningInstance.MainWindowHandle, Winn32.WM_COPYDATA, 0, ref copyData);

receiver

protected override void DefWndProc(ref Message m)

{

if (m.Msg == Winn32.WM_COPYDATA)

{

Winn32.COPYDATASTRUCT copyData = new Winn32.COPYDATASTRUCT();

Type type = copyData.GetType();

copyData = (Winn32.COPYDATASTRUCT)m.GetLParam(type);

this.textBox1.Text = copyData.lpData;

}

base.DefWndProc(ref m);

}

4,FindWindow

Find the window handle

[DllImport("User32.dll", EntryPoint = "FindWindow")]

private static extern int FindWindow(string lpClassName, string lpWindowName);

5,SetLocalTime

Set the system time

[StructLayout(LayoutKind.Sequential)]

public struct SYSTEMTIME

{

public ushort wYear;

public ushort wMonth;

public ushort wDayOfWeek;

public ushort wDay;

public ushort wHour;

public ushort wMinute;

public ushort wSecond;

public ushort wMilliseconds;

public void FromDateTime(DateTime dateTime)

{

wYear = (ushort)dateTime.Year;

wMonth = (ushort)dateTime.Month;

wDayOfWeek = (ushort)dateTime.DayOfWeek;

wDay = (ushort)dateTime.Day;

wHour = (ushort)dateTime.Hour;

wMinute = (ushort)dateTime.Minute;

wSecond = (ushort)dateTime.Second;

wMilliseconds = (ushort)dateTime.Millisecond;

}

public DateTime ToDateTime()

{

return new DateTime(wYear, wMonth, wDay, wHour, wMinute, wSecond);

}

}

[DllImport("kernel32.dll")]

public static extern bool SetLocalTime(ref SYSTEMTIME Time);

6,SHGetFileInfo

Get the icon of the executable file icon

[Flags]

enum SHGFI : int

{

/// <summary>get icon</summary>

Icon = 0x000000100,

/// <summary>get display name</summary>

DisplayName = 0x000000200,

/// <summary>get type name</summary>

TypeName = 0x000000400,

/// <summary>get attributes</summary>

Attributes = 0x000000800,

/// <summary>get icon location</summary>

IconLocation = 0x000001000,

/// <summary>return exe type</summary>

ExeType = 0x000002000,

/// <summary>get system icon index</summary>

SysIconIndex = 0x000004000,

/// <summary>put a link overlay on icon</summary>

LinkOverlay = 0x000008000,

/// <summary>show icon in selected state</summary>

Selected = 0x000010000,

/// <summary>get only specified attributes</summary>

Attr_Specified = 0x000020000,

/// <summary>get large icon</summary>

LargeIcon = 0x000000000,

/// <summary>get small icon</summary>

SmallIcon = 0x000000001,

/// <summary>get open icon</summary>

OpenIcon = 0x000000002,

/// <summary>get shell size icon</summary>

ShellIconSize = 0x000000004,

/// <summary>pszPath is a pidl</summary>

PIDL = 0x000000008,

/// <summary>use passed dwFileAttribute</summary>

UseFileAttributes = 0x000000010,

/// <summary>apply the appropriate overlays</summary>

AddOverlays = 0x000000020,

/// <summary>Get the index of the overlay in the upper 8 bits of the iIcon</summary>

OverlayIndex = 0x000000040,

}

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]

public struct SHFILEINFO

{

public SHFILEINFO(bool b)

{

hIcon = IntPtr.Zero;

Bacon = 0;

dwAttributes = 0;

szDisplayName = "";

szTypeName = "";

}

public IntPtr hIcon;

public int icon;

public uint dwAttributes;

[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]

public string szDisplayName;

[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)]

public string szTypeName;

};

[DllImport ( "shell32.dll", CharSet = CharSet.Unicode)] // path have Chinese characters, use unicode

public static extern int SHGetFileInfo(

string pszPath,

int dwFileAttributes,

out SHFILEINFO psfi,

uint cbfileInfo,

SHGFI uFlags);

example:

private static Icon GetIcon(string strPath, bool bSmall)

{

SHFILEINFO info = new SHFILEINFO(true);

int cbFileInfo = Marshal.SizeOf(info);

SHGFI flags;

if (bSmall)

flags = SHGFI.Icon | SHGFI.SmallIcon | SHGFI.UseFileAttributes;

else

flags = SHGFI.Icon | SHGFI.LargeIcon;

Win32API.SHGetFileInfo(strPath, 256, out info, (uint)cbFileInfo, flags);

return Icon.FromHandle(info.hIcon);

}

7,GetWindowThreadProcessId

Get a handle on the process and thread ID, it returns the thread ID, ref returns the process ID

[System.Runtime.InteropServices.DllImport("User32.dll")]

private static extern int GetWindowThreadProcessId(int Hwnd, ref int OutPressId);

Examples: Kill excel process

int processID = 0;

int threadID;

threadID = GetWindowThreadProcessId(excelApp.Hwnd, ref processID);

if (processID > 0)

{

System.Diagnostics.Process process = System.Diagnostics.Process.GetProcessById(processID);

if (process != null)

{

process.Kill();

}

}

Although .Net class library strong, but still sometimes limited functionality, conquer some api functions, we will solve the problem of providing another idea, here are their common to the Api function, for future reference.

I know api function, but do not know how c # or VB.net that statement, you can check this site: http: //pinvoke.net/

1,SetForegroundWindow

The window to the foreground, provided that the window is not minimized.

[DllImport("User32.dll")]

public static extern bool SetForegroundWindow(IntPtr hWnd);

2,ShowWindowAsync

Display window, such as to minimize the normal, which is asynchronous.

[DllImport("User32.dll")]

public static extern bool ShowWindowAsync(IntPtr hWnd, int cmdShow);

public enum ShowState : int

{

SW_HIDE = 0,

SW_SHOWNORMAL = 1,

SW_NORMAL = 1,

SW_SHOWMINIMIZED = 2,

SW_SHOWMAXIMIZED = 3,

SW_MAXIMIZE = 3,

SW_SHOWNOACTIVATE = 4,

SW_SHOW = 5,

SW_MINIMIZE = 6,

SW_SHOWMINNOACTIVE = 7,

SW_SHOWNA = 8,

SW_RESTORE = 9,

SW_SHOWDEFAULT = 10,

SW_FORCEMINIMIZE = 11,

SW_MAX = 11

}

3,SendMessage

Inter-process communication, there must be a window to accept the message loop for the job. WM_COPYDATA

public const int WM_COPYDATA = 0x004A;

public struct COPYDATASTRUCT

{

public IntPtr dwData;

public int cbData;

[MarshalAs(UnmanagedType.LPStr)]

public string lpData;

}

[DllImport("User32.dll", EntryPoint = "SendMessage")]

public static extern int SendMessage(

IntPtr hWnd, // handle to destination window

int Msg, // message

int wParam, // first message parameter

ref COPYDATASTRUCT lParam // second message parameter

);

example:

sender:

byte[] sarr = System.Text.Encoding.Default.GetBytes(args[0]);

Winn32.COPYDATASTRUCT copyData = new Winn32.COPYDATASTRUCT();

copyData.cbData = sarr.Length + 1;

copyData.lpData = args[0];

copyData.dwData = (IntPtr) 100; // just write here what numbers

Winn32.SendMessage(runningInstance.MainWindowHandle, Winn32.WM_COPYDATA, 0, ref copyData);

receiver

protected override void DefWndProc(ref Message m)

{

if (m.Msg == Winn32.WM_COPYDATA)

{

Winn32.COPYDATASTRUCT copyData = new Winn32.COPYDATASTRUCT();

Type type = copyData.GetType();

copyData = (Winn32.COPYDATASTRUCT)m.GetLParam(type);

this.textBox1.Text = copyData.lpData;

}

base.DefWndProc(ref m);

}

4,FindWindow

Find the window handle

[DllImport("User32.dll", EntryPoint = "FindWindow")]

private static extern int FindWindow(string lpClassName, string lpWindowName);

5,SetLocalTime

Set the system time

[StructLayout(LayoutKind.Sequential)]

public struct SYSTEMTIME

{

public ushort wYear;

public ushort wMonth;

public ushort wDayOfWeek;

public ushort wDay;

public ushort wHour;

public ushort wMinute;

public ushort wSecond;

public ushort wMilliseconds;

public void FromDateTime(DateTime dateTime)

{

wYear = (ushort)dateTime.Year;

wMonth = (ushort)dateTime.Month;

wDayOfWeek = (ushort)dateTime.DayOfWeek;

wDay = (ushort)dateTime.Day;

wHour = (ushort)dateTime.Hour;

wMinute = (ushort)dateTime.Minute;

wSecond = (ushort)dateTime.Second;

wMilliseconds = (ushort)dateTime.Millisecond;

}

public DateTime ToDateTime()

{

return new DateTime(wYear, wMonth, wDay, wHour, wMinute, wSecond);

}

}

[DllImport("kernel32.dll")]

public static extern bool SetLocalTime(ref SYSTEMTIME Time);

6,SHGetFileInfo

Get the icon of the executable file icon

[Flags]

enum SHGFI : int

{

/// <summary>get icon</summary>

Icon = 0x000000100,

/// <summary>get display name</summary>

DisplayName = 0x000000200,

/// <summary>get type name</summary>

TypeName = 0x000000400,

/// <summary>get attributes</summary>

Attributes = 0x000000800,

/// <summary>get icon location</summary>

IconLocation = 0x000001000,

/// <summary>return exe type</summary>

ExeType = 0x000002000,

/// <summary>get system icon index</summary>

SysIconIndex = 0x000004000,

/// <summary>put a link overlay on icon</summary>

LinkOverlay = 0x000008000,

/// <summary>show icon in selected state</summary>

Selected = 0x000010000,

/// <summary>get only specified attributes</summary>

Attr_Specified = 0x000020000,

/// <summary>get large icon</summary>

LargeIcon = 0x000000000,

/// <summary>get small icon</summary>

SmallIcon = 0x000000001,

/// <summary>get open icon</summary>

OpenIcon = 0x000000002,

/// <summary>get shell size icon</summary>

ShellIconSize = 0x000000004,

/// <summary>pszPath is a pidl</summary>

PIDL = 0x000000008,

/// <summary>use passed dwFileAttribute</summary>

UseFileAttributes = 0x000000010,

/// <summary>apply the appropriate overlays</summary>

AddOverlays = 0x000000020,

/// <summary>Get the index of the overlay in the upper 8 bits of the iIcon</summary>

OverlayIndex = 0x000000040,

}

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]

public struct SHFILEINFO

{

public SHFILEINFO(bool b)

{

hIcon = IntPtr.Zero;

Bacon = 0;

dwAttributes = 0;

szDisplayName = "";

szTypeName = "";

}

public IntPtr hIcon;

public int icon;

public uint dwAttributes;

[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]

public string szDisplayName;

[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)]

public string szTypeName;

};

[DllImport ( "shell32.dll", CharSet = CharSet.Unicode)] // path have Chinese characters, use unicode

public static extern int SHGetFileInfo(

string pszPath,

int dwFileAttributes,

out SHFILEINFO psfi,

uint cbfileInfo,

SHGFI uFlags);

example:

private static Icon GetIcon(string strPath, bool bSmall)

{

SHFILEINFO info = new SHFILEINFO(true);

int cbFileInfo = Marshal.SizeOf(info);

SHGFI flags;

if (bSmall)

flags = SHGFI.Icon | SHGFI.SmallIcon | SHGFI.UseFileAttributes;

else

flags = SHGFI.Icon | SHGFI.LargeIcon;

Win32API.SHGetFileInfo(strPath, 256, out info, (uint)cbFileInfo, flags);

return Icon.FromHandle(info.hIcon);

}

7,GetWindowThreadProcessId

Get a handle on the process and thread ID, it returns the thread ID, ref returns the process ID

[System.Runtime.InteropServices.DllImport("User32.dll")]

private static extern int GetWindowThreadProcessId(int Hwnd, ref int OutPressId);

Examples: Kill excel process

int processID = 0;

int threadID;

threadID = GetWindowThreadProcessId(excelApp.Hwnd, ref processID);

if (processID > 0)

{

System.Diagnostics.Process process = System.Diagnostics.Process.GetProcessById(processID);

if (process != null)

{

process.Kill();

}

}

Guess you like

Origin www.cnblogs.com/bedfly/p/12154034.html