Windows API常用函数

转自:http://www.cnblogs.com/xiashengwang/p/4026259.html

.NET中虽然类库很强,但还是有些时候功能有限,掌握常用的api函数,

会给我们解决问题提供另一种思路。

1、SetForegroundWindow

将窗口显示到最前面,前提是窗口没有最小化。

[DIIImport("User32.dll")]

public static extern bool SetForegroundWindow(IntPtrh Wnd);

2、ShowWindowAsync

显示窗口,如最小化后显示正常,这是异步的。

[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

进程间通信,接受窗口要有消息循环才行。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

);

例子:

发送方:

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; //这里随便写什么数字

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

接收方

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

找到窗口句柄

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

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

5,SetLocalTime

设定系统时间

[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

获取执行文件的图标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;

iIcon = 0;

dwAttributes = 0;

szDisplayName = "";

szTypeName = "";

}

public IntPtr hIcon;

public int iIcon;

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)]//路径有中文字符,要用unicode

public static extern int SHGetFileInfo(

string pszPath,

int dwFileAttributes,

out SHFILEINFO psfi,

uint cbfileInfo,

SHGFI uFlags);

例子:

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

得到句柄的进程和线程ID,返回的是线程ID,ref返回的是进程ID

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

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

例子:杀死excel进程

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();

}

}

时间: 2025-01-02 15:36:44

Windows API常用函数的相关文章

使用IDA PRO+OllyDbg+PEview 追踪windows API 动态链接库函数的调用过程

http://bbs.pediy.com/showthread.php?p=1354999 标 题: [原创]使用IDA PRO+OllyDbg+PEview 追踪windows API 动态链接库函数的调用过程.作 者: shayi时 间: 2015-02-12,05:19:54链 接: http://bbs.pediy.com/showthread.php?t=197829 使用IDA PRO+OllyDbg+PEview 追踪windows API 动态链接库函数的调用过程. (本文同步更

Appium——api常用函数

appium常用函数介绍: 获取页面信息: 1. def get_current_activity(cls, driver): ''' 获取当前页面的activity :param driver: :return: ''' return driver.current_activity 2. def get_current_pagesource(cls, driver): ''' 获取当前页面源码 :param driver: :return: 返回数据类型为str ''' return driv

VC API常用函数简单例子大全[转]

第一个:FindWindow根据窗口类名或窗口标题名来获得窗口的句柄,该函数返回窗口的句柄 函数的定义:HWND WINAPI FindWindow(LPCSTR lpClassName ,LPCSTR lpWindowName); 第一个参数填窗口的类名,第二个填窗口的标题名,其实是不需要同时填两个参数的,也就是说,你只要知道窗口的类名或窗口的标题就可以了,没有的那个就用NULL代替. 比如现在有一个窗口名为"无标题.txt - 记事本"的记事本程序.那么我就可以用上面的函数获得这个

Windows API CreateEvent函数用法

CreateEvent是创建windows事件对象,作用主要用在判断线程退出和锁定方面. HANDLE CreateEvent(   LPSECURITY_ATTRIBUTES lpEventAttributes, // SD    BOOL bManualReset, // reset type    BOOL bInitialState, // initial state    LPCTSTR lpName // object name); 参数说明: lpEventAttributes:

【CUDA】Windows 下常用函数头文件

CUDA 函数 头文件 __global__ __device__ #include <cuda_runtime.h> threadIdx #include <device_launch_parameters.h>   #include <sm_20_intrinsics.h> __shfl() #include <sm_30_intrinsics.h> tex1Dfetch() #include <texture_fetch_functions.h&

使用windows API函数获取目录下所有文件名---目录下文件的扫描和处理

直接上代码 // test_max.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <windows.h> #include <Shlwapi.h> #include <iostream> #include <string> #include <vector> #pragma comment(lib,"shlwapi.lib") //注意放置的位置

VBS调用windows api函数(postmessage)实现后台发送按键脚本

'=========================================================================='' VBScript Source File -- Created with SAPIEN Technologies PrimalScript 4.0'' NAME: '' AUTHOR: Microsoft , Microsoft' DATE : 2014/8/10'' COMMENT: ''===================定义变量,注册

windows API中CreateWindow()函数详解

CreateWindow函数详解 在注册完窗口类后就需要进行窗口的创建,用到的函数理所当然就是CreateWindow(), 而这个函数是基于窗口类的,所以还需要指定几个参数来制定特定的窗口.而且像一些不带边框的窗口是怎么创建的也是具有相当的技巧的,就是创建的是不带标题和边框的窗口,然后自己在客户区绘制程序的内容,能够制作个性化的应用程序. API解释 该函数创建一个重叠式窗口.弹出式窗口或子窗口.它指定窗口类,窗口标题,窗口风格,以及窗口的初始位置及大小(可选的).函数也指该窗口的父窗口或所属

windows 下实现函数打桩:拦截API方式

windows 下实现函数打桩:拦截API方式 最近因为工作需要,开始研究函数打桩的方法.由于不想对工程做过多的修改,于是放弃了使用Google gmock的想法.但是也足足困扰另外我一天一宿.经过奋战,终于有所收获.闲话少说,开始看看有什么方法. 一.基础准备 1. 函数调用的原理:通过函数名(函数的入口地址)对函数进行访问,假设我们能够改变函数首地址指向的内存的话,使其跳转到另一个函数去执行的话,那么就可以实现函数打桩了. 2. 方法:对函数首地址出写入一条汇编语言 jmp xxx (其中x