日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > C# >内容正文

C#

C# Win32 API 应用

發(fā)布時(shí)間:2025/4/14 C# 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C# Win32 API 应用 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

C# Win32 API 應(yīng)用

1 創(chuàng)建進(jìn)程及獲取進(jìn)程的信息

部分代碼:?

using?System;
using?System.Collections.Generic;
using?System.ComponentModel;
using?System.Data;
using?System.Drawing;
using?System.Linq;
using?System.Text;
using?System.Windows.Forms;
using?System.Runtime.InteropServices;?

?

namespace?CreateProcess
{
public?partial?class?Form1?:?Form
{
[DllImport("Kernel32.dll",?CharSet?=?CharSet.Ansi)]
public?static?extern?bool?CreateProcess(StringBuilder?lpApplicationName,?StringBuilder?lpCommandLine,
SECURITY_ATTRIBUTES?lpProcessAttributes,
SECURITY_ATTRIBUTES?lpThreadAttributes,
bool?bInheritHandles,
int?dwCreationFlags,
StringBuilder?lpEnvironment,
StringBuilder?lpCurrentDirectory,
ref?STARTUPINFO?lpStartupInfo,
ref?PROCESS_INFORMATION?lpProcessInformation
);
[DllImport("Kernel32.dll",?CharSet?=?CharSet.Ansi)]
public?static?extern?bool?CloseHandle(IntPtr?hObject);
[DllImport("Kernel32.dll",?CharSet?=?CharSet.Ansi)]
public?static?extern?bool?TerminateProcess(IntPtr?hProcess,?uint?ExitCode);?

[StructLayout(LayoutKind.Sequential)]
public?class?SECURITY_ATTRIBUTES
{
public?int?nLength;
public?string?lpSecurityDescriptor;
public?bool?bInheritHandle;
}

[StructLayout(LayoutKind.Sequential)]
public?struct?STARTUPINFO
{
public?int?cb;
public?string?lpReserved;
public?string?lpDesktop;
public?int?lpTitle;
public?int?dwX;
public?int?dwY;
public?int?dwXSize;
public?int?dwYSize;
public?int?dwXCountChars;
public?int?dwYCountChars;
public?int?dwFillAttribute;
public?int?dwFlags;
public?int?wShowWindow;
public?int?cbReserved2;
public?byte?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;
}

STARTUPINFO?sInfo?=?new?STARTUPINFO();
PROCESS_INFORMATION?pInfo?=?new?PROCESS_INFORMATION();

public?Form1()
{
InitializeComponent();
}

private?void?button1_Click(object?sender,?EventArgs?e)
{
OpenFileDialog?ope?=?new?OpenFileDialog();
ope.ShowDialog();
textBox1.Text?=?ope.FileName;
}

private?void?button2_Click(object?sender,?EventArgs?e)
{
bool?ret;
//StringBuilder?sbCommand?=?new?StringBuilder(1000);
//sbCommand.Append(sCommand);?

StringBuilder?sbexeFile?=?new?StringBuilder();
sbexeFile.Append(textBox1.Text);

try
{
ret=CreateProcess(sbexeFile,?null,?null,?null,?false,?0,?null,?null,
ref?sInfo,?ref?pInfo);
if?(ret?==?true)
{
MessageBox.Show(textBox1?+?"?"?+?"運(yùn)行成功");
}
else
{
MessageBox.Show(textBox1?+?"?"?+?"運(yùn)行失敗");
}
}
catch?(Exception?exp)
{
MessageBox.Show(exp.Message);
}

}?

private?void?button3_Click(object?sender,?EventArgs?e)
{
TerminateProcess(pInfo.hProcess,?0);
CloseHandle(pInfo.hProcess);
CloseHandle(pInfo.hThread);
}
}
}

2 枚舉窗口,查看窗口信息和操作;

總結(jié)

以上是生活随笔為你收集整理的C# Win32 API 应用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。