关键字: C#,API,FindWindow,FindWindowEx,SendMessage,进程,注册表 设计初衷: 公司为了便于网络管理,使用了IEEE 802.1X的网络访问控制,这样每次开机需要输入两次登录密码,于是我就研究了一下用C#来帮我输入第二此登录的密码
设计思想: 主要是通过调用Windows API中的一些方法,主要使用的也就是FindWindow,FindWindowEx和SendMessage这三个函数,循环遍历当前的所有窗口,找到目标窗口和进程以后把保存在特定位置的用户名密码以及域信息自动填入输入框中,然后再触发一下button事件,最后程序本身退出。
环境: 在Windows 2000中文版 + sp4,VS.net 2003中文版下开发 在Windows 2000中文版下测试通过
程序截图:
具体设计这个Form的代码就略过不详细说了
为了使用Win32 API,需要先引入下面这个命名空间:
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/4b40b32a-580c-40d0-949a-affbaf756cb8.gif)
using System.Runtime.InteropServices;
另外还需要用到进程和注册表,所以还需要引入下面的两个命名空间:
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/4b40b32a-580c-40d0-949a-affbaf756cb8.gif)
using System.Threading;
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/4b40b32a-580c-40d0-949a-affbaf756cb8.gif)
using Microsoft.Win32;
下面的代码是用来添加对API的引用:
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/fc8a4bbb-9185-4aad-a7e7-4f62a0f915fc.gif)
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/de62d059-82ef-4c5f-8174-62d34d09ae1e.gif)
Dll Import#region Dll Import
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/26ab188c-f6ce-45c8-8c8c-e68eea92687e.gif)
[DllImport("User32.dll",EntryPoint="FindWindow")]
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/26ab188c-f6ce-45c8-8c8c-e68eea92687e.gif)
private static extern IntPtr FindWindow(string lpClassName,
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/26ab188c-f6ce-45c8-8c8c-e68eea92687e.gif)
string lpWindowName);
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/26ab188c-f6ce-45c8-8c8c-e68eea92687e.gif)
[DllImport("user32.dll",EntryPoint="FindWindowEx")]
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/26ab188c-f6ce-45c8-8c8c-e68eea92687e.gif)
private static extern IntPtr FindWindowEx(IntPtr hwndParent,
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/26ab188c-f6ce-45c8-8c8c-e68eea92687e.gif)
IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/26ab188c-f6ce-45c8-8c8c-e68eea92687e.gif)
[DllImport("User32.dll",EntryPoint="SendMessage")]
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/26ab188c-f6ce-45c8-8c8c-e68eea92687e.gif)
private static extern int SendMessage(IntPtr hWnd,
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/26ab188c-f6ce-45c8-8c8c-e68eea92687e.gif)
int Msg, IntPtr wParam, string lParam);
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/1c9ce14e-3324-49db-90c5-2a86dcb6781c.gif)
#endregion
主要用到的就是这三个方法,具体的我这里就不详细介绍了,请参考MSDN。
需要用到的一些参数:
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/4b40b32a-580c-40d0-949a-affbaf756cb8.gif)
const int WM_GETTEXT = 0x000D;
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/4b40b32a-580c-40d0-949a-affbaf756cb8.gif)
const int WM_SETTEXT = 0x000C;
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/4b40b32a-580c-40d0-949a-affbaf756cb8.gif)
const int WM_CLICK = 0x00F5;
从名称上应该就可以了解这些参数具体的含义是什么了,而且这些参数都可以通过VS附带的工具Spy ++查到。
下面是整个程序的核心部分,查找窗体并对它进行操作:
![](/Files/20114/fc8a4bbb-9185-4aad-a7e7-4f62a0f915fc.gif)
![](/Files/20114/de62d059-82ef-4c5f-8174-62d34d09ae1e.gif)
SearchWindow#region SearchWindow
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/26ab188c-f6ce-45c8-8c8c-e68eea92687e.gif)
private int SearchWindow()
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/a3f00230-a9e7-4cb7-b43e-e4f8deac5e9d.gif)
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/981205f7-f69f-4a8e-a679-8528939445b8.gif)
{
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/26ab188c-f6ce-45c8-8c8c-e68eea92687e.gif)
int retval = 0; //增加一个返回值用来判断操作是否成功
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/26ab188c-f6ce-45c8-8c8c-e68eea92687e.gif)
//下面的这些参数都可以用Spy++查到
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/26ab188c-f6ce-45c8-8c8c-e68eea92687e.gif)
string lpszParentClass = "#32770"; //整个窗口的类名
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/26ab188c-f6ce-45c8-8c8c-e68eea92687e.gif)
string lpszParentWindow = "本地连接"; //窗口标题
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/26ab188c-f6ce-45c8-8c8c-e68eea92687e.gif)
string lpszClass = "Edit"; //需要查找的子窗口的类名,也就是输入框
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/26ab188c-f6ce-45c8-8c8c-e68eea92687e.gif)
string lpszClass_Submit = "Button"; //需要查找的Button的类名
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/26ab188c-f6ce-45c8-8c8c-e68eea92687e.gif)
string lpszName_Submit = "确定"; //需要查找的Button的标题
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/26ab188c-f6ce-45c8-8c8c-e68eea92687e.gif)
string text = "";
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/26ab188c-f6ce-45c8-8c8c-e68eea92687e.gif)
IntPtr ParenthWnd = new IntPtr(0);
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/26ab188c-f6ce-45c8-8c8c-e68eea92687e.gif)
IntPtr EdithWnd = new IntPtr(0);
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/26ab188c-f6ce-45c8-8c8c-e68eea92687e.gif)
//查到窗体,得到整个窗体
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/26ab188c-f6ce-45c8-8c8c-e68eea92687e.gif)
ParenthWnd = FindWindow(lpszParentClass,lpszParentWindow);
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/26ab188c-f6ce-45c8-8c8c-e68eea92687e.gif)
//判断这个窗体是否有效
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/26ab188c-f6ce-45c8-8c8c-e68eea92687e.gif)
if (!ParenthWnd.Equals(IntPtr.Zero))
![](/Files/20114/a3f00230-a9e7-4cb7-b43e-e4f8deac5e9d.gif)
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/981205f7-f69f-4a8e-a679-8528939445b8.gif)
{
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/26ab188c-f6ce-45c8-8c8c-e68eea92687e.gif)
//得到User Name这个子窗体,并设置其内容
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/26ab188c-f6ce-45c8-8c8c-e68eea92687e.gif)
EdithWnd = FindWindowEx(ParenthWnd,EdithWnd,lpszClass,"");
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/26ab188c-f6ce-45c8-8c8c-e68eea92687e.gif)
if (!EdithWnd.Equals(IntPtr.Zero))
![](/Files/20114/a3f00230-a9e7-4cb7-b43e-e4f8deac5e9d.gif)
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/981205f7-f69f-4a8e-a679-8528939445b8.gif)
{
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/26ab188c-f6ce-45c8-8c8c-e68eea92687e.gif)
text = this.tbUserName.Text.Trim();
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/26ab188c-f6ce-45c8-8c8c-e68eea92687e.gif)
//调用SendMessage方法设置其内容
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/26ab188c-f6ce-45c8-8c8c-e68eea92687e.gif)
SendMessage(EdithWnd, WM_SETTEXT, (IntPtr)0, text);
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/26ab188c-f6ce-45c8-8c8c-e68eea92687e.gif)
retval ++;
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/5b1f669f-8b7a-4cdf-9cf9-ec805ea4f854.gif)
}
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/26ab188c-f6ce-45c8-8c8c-e68eea92687e.gif)
//得到Password这个子窗体,并设置其内容
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/26ab188c-f6ce-45c8-8c8c-e68eea92687e.gif)
EdithWnd = FindWindowEx(ParenthWnd,EdithWnd,lpszClass,"");
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/26ab188c-f6ce-45c8-8c8c-e68eea92687e.gif)
if (!EdithWnd.Equals(IntPtr.Zero))
![](/Files/20114/a3f00230-a9e7-4cb7-b43e-e4f8deac5e9d.gif)
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/981205f7-f69f-4a8e-a679-8528939445b8.gif)
{
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/26ab188c-f6ce-45c8-8c8c-e68eea92687e.gif)
text = this.tbPassword.Text.Trim();
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/26ab188c-f6ce-45c8-8c8c-e68eea92687e.gif)
SendMessage(EdithWnd, WM_SETTEXT, (IntPtr)0, text);
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/26ab188c-f6ce-45c8-8c8c-e68eea92687e.gif)
retval ++;
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/5b1f669f-8b7a-4cdf-9cf9-ec805ea4f854.gif)
}
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/26ab188c-f6ce-45c8-8c8c-e68eea92687e.gif)
//得到Domain这个子窗体,并设置其内容
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/26ab188c-f6ce-45c8-8c8c-e68eea92687e.gif)
EdithWnd = FindWindowEx(ParenthWnd,EdithWnd,lpszClass,"");
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/26ab188c-f6ce-45c8-8c8c-e68eea92687e.gif)
if (!EdithWnd.Equals(IntPtr.Zero))
![](/Files/20114/a3f00230-a9e7-4cb7-b43e-e4f8deac5e9d.gif)
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/981205f7-f69f-4a8e-a679-8528939445b8.gif)
{
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/26ab188c-f6ce-45c8-8c8c-e68eea92687e.gif)
text = this.tbDomain.Text.Trim();
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/26ab188c-f6ce-45c8-8c8c-e68eea92687e.gif)
SendMessage(EdithWnd, WM_SETTEXT, (IntPtr)0, text);
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/26ab188c-f6ce-45c8-8c8c-e68eea92687e.gif)
retval ++;
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/5b1f669f-8b7a-4cdf-9cf9-ec805ea4f854.gif)
}
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/26ab188c-f6ce-45c8-8c8c-e68eea92687e.gif)
//得到Button这个子窗体,并触发它的Click事件
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/26ab188c-f6ce-45c8-8c8c-e68eea92687e.gif)
EdithWnd = FindWindowEx(ParenthWnd,
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/26ab188c-f6ce-45c8-8c8c-e68eea92687e.gif)
EdithWnd,lpszClass_Submit,lpszName_Submit);
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/26ab188c-f6ce-45c8-8c8c-e68eea92687e.gif)
if (!EdithWnd.Equals(IntPtr.Zero))
![](/Files/20114/a3f00230-a9e7-4cb7-b43e-e4f8deac5e9d.gif)
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/981205f7-f69f-4a8e-a679-8528939445b8.gif)
{
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/26ab188c-f6ce-45c8-8c8c-e68eea92687e.gif)
SendMessage(EdithWnd,WM_CLICK,(IntPtr)0,"0");
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/26ab188c-f6ce-45c8-8c8c-e68eea92687e.gif)
retval ++;
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/5b1f669f-8b7a-4cdf-9cf9-ec805ea4f854.gif)
}
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/5b1f669f-8b7a-4cdf-9cf9-ec805ea4f854.gif)
}
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/26ab188c-f6ce-45c8-8c8c-e68eea92687e.gif)
return retval;
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/5b1f669f-8b7a-4cdf-9cf9-ec805ea4f854.gif)
}
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/1c9ce14e-3324-49db-90c5-2a86dcb6781c.gif)
#endregion
这里有一点需要说明的是,当一个窗体下面有几个类名相同的子窗体时,也就是说如果有三个输入框,这三个输入框的类名都是Edit,查找结果是依次从上往下的,最开始我不知道该怎么办才能分出具体的每个不同的输入框,后来只能这样一个一个来查找来试一下,没想到居然是对的。(有别的办法么?)
上面的这段代码也只适用于中文版的操作系统,因为不同的操作系统下同一个窗体的名称都是不一样的,我这里也没有英文版的系统,所以也没办法进行测试。
为了免去每次都让用户手动输入的烦恼,我需要把这些信息都保存到一个特定的文件里面去,当用户在第一次运行这个程序的时候,只需要输入一次,点下Save,先把这些信息保存到一个文件中,然后再把程序本身加载到系统启动项里去,这样下次开机的时候程序就可以自启动,然后从文件中读取信息完成以下的操作。
选择存放文件的路径:
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/4b40b32a-580c-40d0-949a-affbaf756cb8.gif)
private string UserPro =
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/4b40b32a-580c-40d0-949a-affbaf756cb8.gif)
System.Environment.GetEnvironmentVariable("USERPROFILE");
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/4b40b32a-580c-40d0-949a-affbaf756cb8.gif)
private string PATH = System.Environment.GetEnvironmentVariable("USERPROFILE") + @"\Local Settings\AutoLog.ini";
当用户点下Save按钮所触发的事件:
![](/Files/20114/fc8a4bbb-9185-4aad-a7e7-4f62a0f915fc.gif)
![](/Files/20114/de62d059-82ef-4c5f-8174-62d34d09ae1e.gif)
Button Submit Click#region Button Submit Click
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/26ab188c-f6ce-45c8-8c8c-e68eea92687e.gif)
private void btSubmit_Click(object sender, System.EventArgs e)
![](/Files/20114/a3f00230-a9e7-4cb7-b43e-e4f8deac5e9d.gif)
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/981205f7-f69f-4a8e-a679-8528939445b8.gif)
{
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/26ab188c-f6ce-45c8-8c8c-e68eea92687e.gif)
SaveData();
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/5b1f669f-8b7a-4cdf-9cf9-ec805ea4f854.gif)
}
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/26ab188c-f6ce-45c8-8c8c-e68eea92687e.gif)
private void SaveData()
![](/Files/20114/a3f00230-a9e7-4cb7-b43e-e4f8deac5e9d.gif)
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/981205f7-f69f-4a8e-a679-8528939445b8.gif)
{
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/26ab188c-f6ce-45c8-8c8c-e68eea92687e.gif)
try
![](/Files/20114/a3f00230-a9e7-4cb7-b43e-e4f8deac5e9d.gif)
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/981205f7-f69f-4a8e-a679-8528939445b8.gif)
{
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/26ab188c-f6ce-45c8-8c8c-e68eea92687e.gif)
//Save Data
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/26ab188c-f6ce-45c8-8c8c-e68eea92687e.gif)
FileInfo obj = new FileInfo(PATH);
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/26ab188c-f6ce-45c8-8c8c-e68eea92687e.gif)
if(obj.Exists)
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/26ab188c-f6ce-45c8-8c8c-e68eea92687e.gif)
obj.Delete();
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/26ab188c-f6ce-45c8-8c8c-e68eea92687e.gif)
FileStream ofile = new FileStream(PATH,FileMode.Create);
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/26ab188c-f6ce-45c8-8c8c-e68eea92687e.gif)
//Hidden the file
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/26ab188c-f6ce-45c8-8c8c-e68eea92687e.gif)
File.SetAttributes(PATH,FileAttributes.Hidden);
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/26ab188c-f6ce-45c8-8c8c-e68eea92687e.gif)
StreamWriter sw = new StreamWriter(ofile);
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/26ab188c-f6ce-45c8-8c8c-e68eea92687e.gif)
//把用户名密码和域信息写入文件
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/26ab188c-f6ce-45c8-8c8c-e68eea92687e.gif)
sw.WriteLine(this.tbUserName.Text);
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/26ab188c-f6ce-45c8-8c8c-e68eea92687e.gif)
sw.WriteLine(this.tbPassword.Text);
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/26ab188c-f6ce-45c8-8c8c-e68eea92687e.gif)
sw.WriteLine(this.tbDomain.Text);
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/26ab188c-f6ce-45c8-8c8c-e68eea92687e.gif)
sw.Flush();
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/26ab188c-f6ce-45c8-8c8c-e68eea92687e.gif)
sw.Close();
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/26ab188c-f6ce-45c8-8c8c-e68eea92687e.gif)
ofile.Close();
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/26ab188c-f6ce-45c8-8c8c-e68eea92687e.gif)
//把当前文件拷贝到指定位置,然后再添加到注册表的启动项里
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/26ab188c-f6ce-45c8-8c8c-e68eea92687e.gif)
string opath = Application.StartupPath + @"\Login.exe";
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/26ab188c-f6ce-45c8-8c8c-e68eea92687e.gif)
string tpath = UserPro + @"\Local Settings\Login.exe";
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/26ab188c-f6ce-45c8-8c8c-e68eea92687e.gif)
if(File.Exists(tpath))
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/26ab188c-f6ce-45c8-8c8c-e68eea92687e.gif)
File.Delete(tpath);
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/26ab188c-f6ce-45c8-8c8c-e68eea92687e.gif)
File.Copy(opath,tpath);
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/26ab188c-f6ce-45c8-8c8c-e68eea92687e.gif)
RegistryKey hklm = Registry.CurrentUser;
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/26ab188c-f6ce-45c8-8c8c-e68eea92687e.gif)
RegistryKey run =
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/26ab188c-f6ce-45c8-8c8c-e68eea92687e.gif)
hklm.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/26ab188c-f6ce-45c8-8c8c-e68eea92687e.gif)
run.SetValue("AutoLogin",tpath);
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/26ab188c-f6ce-45c8-8c8c-e68eea92687e.gif)
//最后程序退出
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/26ab188c-f6ce-45c8-8c8c-e68eea92687e.gif)
MessageBox.Show("OK","Information",
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/26ab188c-f6ce-45c8-8c8c-e68eea92687e.gif)
MessageBoxButtons.OK,MessageBoxIcon.Information);
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/26ab188c-f6ce-45c8-8c8c-e68eea92687e.gif)
Application.Exit();
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/5b1f669f-8b7a-4cdf-9cf9-ec805ea4f854.gif)
}
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/26ab188c-f6ce-45c8-8c8c-e68eea92687e.gif)
catch(Exception ex)
![](/Files/20114/a3f00230-a9e7-4cb7-b43e-e4f8deac5e9d.gif)
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/981205f7-f69f-4a8e-a679-8528939445b8.gif)
{
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/26ab188c-f6ce-45c8-8c8c-e68eea92687e.gif)
MessageBox.Show(ex.ToString(),"Error",
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/26ab188c-f6ce-45c8-8c8c-e68eea92687e.gif)
MessageBoxButtons.OK,MessageBoxIcon.Error);
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/5b1f669f-8b7a-4cdf-9cf9-ec805ea4f854.gif)
}
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/5b1f669f-8b7a-4cdf-9cf9-ec805ea4f854.gif)
}
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/1c9ce14e-3324-49db-90c5-2a86dcb6781c.gif)
#endregion
这样的话,程序就可以从文件中读取已经存放好的信息来进行验证了。最后要做的就是,需要单独开一个进程来循环执行上面的SearchWindow这个方法,直到找到符合条件的窗口并成功验证为止,并且这个进程需要随程序的启动而启动。
我们可以在构造函数中添加一个名为LoadData的方法,然后在这个方法中进行具体的读文件信息和启动进程的操作。
当然,先定义好这个进程:
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/4b40b32a-580c-40d0-949a-affbaf756cb8.gif)
private Thread thread;
然后是LoadData这个方法:
![](/Files/20114/fc8a4bbb-9185-4aad-a7e7-4f62a0f915fc.gif)
![](/Files/20114/de62d059-82ef-4c5f-8174-62d34d09ae1e.gif)
Load#region Load
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/26ab188c-f6ce-45c8-8c8c-e68eea92687e.gif)
private void LoadData()
![](/Files/20114/a3f00230-a9e7-4cb7-b43e-e4f8deac5e9d.gif)
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/981205f7-f69f-4a8e-a679-8528939445b8.gif)
{
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/26ab188c-f6ce-45c8-8c8c-e68eea92687e.gif)
//Load Data
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/26ab188c-f6ce-45c8-8c8c-e68eea92687e.gif)
FileStream ofile = new FileStream(PATH,FileMode.OpenOrCreate);
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/26ab188c-f6ce-45c8-8c8c-e68eea92687e.gif)
StreamReader sr = new StreamReader(ofile);
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/26ab188c-f6ce-45c8-8c8c-e68eea92687e.gif)
this.tbUserName.Text = sr.ReadLine();
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/26ab188c-f6ce-45c8-8c8c-e68eea92687e.gif)
this.tbPassword.Text = sr.ReadLine();
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/26ab188c-f6ce-45c8-8c8c-e68eea92687e.gif)
this.tbDomain.Text = sr.ReadLine();
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/26ab188c-f6ce-45c8-8c8c-e68eea92687e.gif)
sr.Close();
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/26ab188c-f6ce-45c8-8c8c-e68eea92687e.gif)
ofile.Close();
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/26ab188c-f6ce-45c8-8c8c-e68eea92687e.gif)
//Thread Start
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/26ab188c-f6ce-45c8-8c8c-e68eea92687e.gif)
thread = new Thread(new ThreadStart(Watch));
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/26ab188c-f6ce-45c8-8c8c-e68eea92687e.gif)
thread.IsBackground = true;
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/26ab188c-f6ce-45c8-8c8c-e68eea92687e.gif)
thread.Start();
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/5b1f669f-8b7a-4cdf-9cf9-ec805ea4f854.gif)
}
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/26ab188c-f6ce-45c8-8c8c-e68eea92687e.gif)
private void Watch()
![](/Files/20114/a3f00230-a9e7-4cb7-b43e-e4f8deac5e9d.gif)
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/981205f7-f69f-4a8e-a679-8528939445b8.gif)
{
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/26ab188c-f6ce-45c8-8c8c-e68eea92687e.gif)
//循环查找这个窗口,直到成功为止
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/26ab188c-f6ce-45c8-8c8c-e68eea92687e.gif)
while(true)
![](/Files/20114/a3f00230-a9e7-4cb7-b43e-e4f8deac5e9d.gif)
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/981205f7-f69f-4a8e-a679-8528939445b8.gif)
{
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/26ab188c-f6ce-45c8-8c8c-e68eea92687e.gif)
int i = this.SearchWindow();
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/26ab188c-f6ce-45c8-8c8c-e68eea92687e.gif)
if(i == 4)
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/26ab188c-f6ce-45c8-8c8c-e68eea92687e.gif)
break;
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/5b1f669f-8b7a-4cdf-9cf9-ec805ea4f854.gif)
}
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/26ab188c-f6ce-45c8-8c8c-e68eea92687e.gif)
//程序退出并释放资源
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/26ab188c-f6ce-45c8-8c8c-e68eea92687e.gif)
Application.Exit();
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/26ab188c-f6ce-45c8-8c8c-e68eea92687e.gif)
this.Dispose();
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/26ab188c-f6ce-45c8-8c8c-e68eea92687e.gif)
this.Close();
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/5b1f669f-8b7a-4cdf-9cf9-ec805ea4f854.gif)
}
![团购api调用,用C#调用Windows API和其它进程通信](/Files/20114/1c9ce14e-3324-49db-90c5-2a86dcb6781c.gif)
#endregion
好,到这里就介绍完毕了,当然
还有很多需要改进的地方,比如说密码存在本机上应该用一种加密的方式来保存等等。我还是第一次接触用C#调用Windows API这方面的东西,很多东西也都是从网上查资料才得到的,不足之处,恳请指出。
Demo Demo中包含很多例子或有疑问,可eMail:点击此处
顺便提下自己的网站http://www.elivn.com>elivn
延伸阅读
最新评论