随笔 - 113  文章 - 0  评论 - 218  阅读 - 73万

C#调用系统API

在Windows平台,可以利用很多系统API,而在.net平台下用C#调用系统API是比较方便而且也是比较轻松的事情。
我们可以利用C#调用API实现底层驱动程序驱动某一个部件(如串口通讯)和或者外设进行工作,也可以利用系统API实现图形化GUI功能
系统API一般分为:核心级(kernel32.dll),用户级(User32.dll), 应用级(gdi32.dll)和其他一些外设驱动等.
下面我们来看一个简单例子,用系统API获取窗口句柄。

 

 

复制代码
代码


public class User32API

{

private static Hashtable processWnd = null;

public delegate bool WNDENUMPROC(IntPtr hwnd, uint lParam);

static User32API()

{

if (processWnd == null)

{

processWnd
= new Hashtable();

}

}

[DllImport(
"user32.dll", EntryPoint = "EnumWindows", SetLastError = true)]

public static extern bool EnumWindows(WNDENUMPROC lpEnumFunc, uint lParam);

[DllImport(
"user32.dll", EntryPoint = "GetParent", SetLastError = true)]

public static extern IntPtr GetParent(IntPtr hWnd);

[DllImport(
"user32.dll", EntryPoint = "GetWindowThreadProcessId")]

public static extern uint GetWindowThreadProcessId(IntPtr hWnd, ref uint lpdwProcessId);

[DllImport(
"user32.dll", EntryPoint = "IsWindow")]

public static extern bool IsWindow(IntPtr hWnd);

[DllImport(
"kernel32.dll", EntryPoint = "SetLastError")]

public static extern void SetLastError(uint dwErrCode);

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

private static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);

Process[] processes;

const int WM_CLOSE = 0x0010;

public void GetCurrentWindowHandle()

{

Microsoft.Win32.RegistryKey oRegistryKey
= Microsoft.Win32.Registry.CurrentUser.OpenSubKey("Software").OpenSubKey("Amada Software");

string[] subKeys = oRegistryKey.GetSubKeyNames();

IntPtr ptrWnd
= IntPtr.Zero;

foreach (string subkey in subKeys)

{

processes
= Process.GetProcessesByName(subkey);

foreach (Process proc in processes)

{

uint uiPid = (uint)proc.Id;

object objWnd = processWnd[uiPid];

if (objWnd != null)

{

ptrWnd
= (IntPtr)objWnd;

if (ptrWnd != IntPtr.Zero && IsWindow(ptrWnd))

{

SendMessage(ptrWnd, WM_CLOSE,
0, 0);

}

else

{

ptrWnd
= IntPtr.Zero;

}

}

bool bResult = EnumWindows(new WNDENUMPROC(EnumWindowsProc), uiPid);

if (!bResult && Marshal.GetLastWin32Error() == 0)

{

objWnd
= processWnd[uiPid];

if (objWnd != null)

{

ptrWnd
= (IntPtr)objWnd;

SendMessage(ptrWnd, WM_CLOSE,
0, 0);

}

}

}

}

}

private static bool EnumWindowsProc(IntPtr hwnd, uint lParam)

{

uint uiPid = 0;

if (GetParent(hwnd) == IntPtr.Zero)

{

GetWindowThreadProcessId(hwnd,
ref uiPid);

if (uiPid == lParam)

{

processWnd[uiPid]
= hwnd;

SetLastError(
0);

return false;

}

}

return true;

}

}
复制代码

 

 

posted on   陈国利  阅读(2064)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· DeepSeek 开源周回顾「GitHub 热点速览」
< 2010年12月 >
28 29 30 1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31 1
2 3 4 5 6 7 8

点击右上角即可分享
微信分享提示