用C#调用Windows API和其它进程通信

作者:边城浪子◎2005-01-16

关键字:
 C#,API,FindWindow,FindWindowEx,SendMessage,进程,注册表

 设计初衷:
 公司为了便于网络管理,使用了IEEE 802.1X的网络访问控制,这样每次开机需要输入两次登录密码,于是我就研究了一下用C#来帮我输入第二此登录的密码

 设计思想:
 主要是通过调用Windows API中的一些方法,主要使用的也就是FindWindow,FindWindowEx和SendMessage这三个函数,循环遍历当前的所有窗口,找到目标窗口和进程以后把保存在特定位置的用户名密码以及域信息自动填入输入框中,然后再触发一下button事件,最后程序本身退出。

 环境:
 在Windows 2000中文版 + sp4,VS.net 2003中文版下开发
 在Windows 2000中文版下测试通过

程序截图:
login.gif

具体设计这个Form的代码就略过不详细说了

 为了使用Win32 API,需要先引入下面这个命名空间:

None.gif using  System.Runtime.InteropServices;

另外还需要用到进程和注册表,所以还需要引入下面的两个命名空间:

None.gif using  System.Threading;
None.gif
using  Microsoft.Win32;

下面的代码是用来添加对API的引用:

ContractedBlock.gif ExpandedBlockStart.gif      Dll Import #region Dll Import
InBlock.gif
InBlock.gif    [DllImport(
"User32.dll",EntryPoint="FindWindow")]
InBlock.gif    
private static extern IntPtr FindWindow(string lpClassName,
InBlock.gif
string lpWindowName);
InBlock.gif
InBlock.gif    [DllImport(
"user32.dll",EntryPoint="FindWindowEx")]
InBlock.gif    
private static extern IntPtr FindWindowEx(IntPtr hwndParent,
InBlock.gifIntPtr hwndChildAfter, 
string lpszClass, string lpszWindow);
InBlock.gif
InBlock.gif    [DllImport(
"User32.dll",EntryPoint="SendMessage")]
InBlock.gif    
private static extern int SendMessage(IntPtr hWnd,
InBlock.gif
int Msg, IntPtr wParam, string lParam);
InBlock.gif
ExpandedBlockEnd.gif    
#endregion

None.gif

主要用到的就是这三个方法,具体的我这里就不详细介绍了,请参考MSDN。

 需要用到的一些参数:

None.gif      const   int  WM_GETTEXT  =   0x000D ;
None.gif    
const   int  WM_SETTEXT  =   0x000C ;
None.gif    
const   int  WM_CLICK  =   0x00F5 ;
None.gif

从名称上应该就可以了解这些参数具体的含义是什么了,而且这些参数都可以通过VS附带的工具Spy ++查到。

 下面是整个程序的核心部分,查找窗体并对它进行操作:

ContractedBlock.gif ExpandedBlockStart.gif SearchWindow #region SearchWindow
InBlock.gif    
private int SearchWindow()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
int retval = 0//增加一个返回值用来判断操作是否成功
InBlock.gif
InBlock.gif        
//下面的这些参数都可以用Spy++查到
InBlock.gif
        string lpszParentClass = "#32770"//整个窗口的类名
InBlock.gif
        string lpszParentWindow = "本地连接"//窗口标题
InBlock.gif
        string lpszClass = "Edit"//需要查找的子窗口的类名,也就是输入框
InBlock.gif
        string lpszClass_Submit = "Button"//需要查找的Button的类名
InBlock.gif
        string lpszName_Submit = "确定"//需要查找的Button的标题
InBlock.gif
        string text = "";
InBlock.gif
InBlock.gif        IntPtr ParenthWnd 
= new IntPtr(0);
InBlock.gif        IntPtr EdithWnd 
= new IntPtr(0);
InBlock.gif
InBlock.gif        
//查到窗体,得到整个窗体
InBlock.gif
        ParenthWnd = FindWindow(lpszParentClass,lpszParentWindow);
InBlock.gif
InBlock.gif        
//判断这个窗体是否有效
InBlock.gif
        if (!ParenthWnd.Equals(IntPtr.Zero))
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//得到User Name这个子窗体,并设置其内容
InBlock.gif
            EdithWnd = FindWindowEx(ParenthWnd,EdithWnd,lpszClass,"");
InBlock.gif            
if (!EdithWnd.Equals(IntPtr.Zero))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                text 
= this.tbUserName.Text.Trim();
InBlock.gif                
//调用SendMessage方法设置其内容
InBlock.gif
                SendMessage(EdithWnd, WM_SETTEXT, (IntPtr)0, text);
InBlock.gif                retval 
++;
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
//得到Password这个子窗体,并设置其内容
InBlock.gif
            EdithWnd = FindWindowEx(ParenthWnd,EdithWnd,lpszClass,"");
InBlock.gif            
if (!EdithWnd.Equals(IntPtr.Zero))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                text 
= this.tbPassword.Text.Trim();
InBlock.gif                SendMessage(EdithWnd, WM_SETTEXT, (IntPtr)
0, text);
InBlock.gif                retval 
++;
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
//得到Domain这个子窗体,并设置其内容
InBlock.gif
            EdithWnd = FindWindowEx(ParenthWnd,EdithWnd,lpszClass,"");
InBlock.gif            
if (!EdithWnd.Equals(IntPtr.Zero))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                text 
= this.tbDomain.Text.Trim();
InBlock.gif                SendMessage(EdithWnd, WM_SETTEXT, (IntPtr)
0, text);
InBlock.gif                retval 
++;
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
//得到Button这个子窗体,并触发它的Click事件
InBlock.gif
            EdithWnd = FindWindowEx(ParenthWnd,
InBlock.gif                EdithWnd,lpszClass_Submit,lpszName_Submit);
InBlock.gif            
if (!EdithWnd.Equals(IntPtr.Zero))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                SendMessage(EdithWnd,WM_CLICK,(IntPtr)
0,"0");
InBlock.gif                retval 
++;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
return retval;
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif    
#endregion

None.gif

这里有一点需要说明的是,当一个窗体下面有几个类名相同的子窗体时,也就是说如果有三个输入框,这三个输入框的类名都是Edit,查找结果是依次从上往下的,最开始我不知道该怎么办才能分出具体的每个不同的输入框,后来只能这样一个一个来查找来试一下,没想到居然是对的。(有别的办法么?)

 上面的这段代码也只适用于中文版的操作系统,因为不同的操作系统下同一个窗体的名称都是不一样的,我这里也没有英文版的系统,所以也没办法进行测试。

 为了免去每次都让用户手动输入的烦恼,我需要把这些信息都保存到一个特定的文件里面去,当用户在第一次运行这个程序的时候,只需要输入一次,点下Save,先把这些信息保存到一个文件中,然后再把程序本身加载到系统启动项里去,这样下次开机的时候程序就可以自启动,然后从文件中读取信息完成以下的操作。

 选择存放文件的路径:

None.gif private   string  UserPro  =  
None.gif        System.Environment.GetEnvironmentVariable(
" USERPROFILE " );
None.gif    
private   string  PATH  =  System.Environment.GetEnvironmentVariable("USERPROFILE") +   @" \Local Settings\AutoLog.ini " ;
None.gif

当用户点下Save按钮所触发的事件:

ContractedBlock.gif ExpandedBlockStart.gif Button Submit Click #region Button Submit Click
InBlock.gif    
private void btSubmit_Click(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        SaveData();
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
private void SaveData()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
try
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//Save Data
InBlock.gif
            FileInfo obj = new FileInfo(PATH);
InBlock.gif            
if(obj.Exists)
InBlock.gif                obj.Delete();
InBlock.gif
InBlock.gif            FileStream ofile 
= new FileStream(PATH,FileMode.Create);
InBlock.gif            
//Hidden the file
InBlock.gif
            File.SetAttributes(PATH,FileAttributes.Hidden);
InBlock.gif            StreamWriter sw 
= new StreamWriter(ofile);
InBlock.gif            
//把用户名密码和域信息写入文件
InBlock.gif
            sw.WriteLine(this.tbUserName.Text);
InBlock.gif            sw.WriteLine(
this.tbPassword.Text);
InBlock.gif            sw.WriteLine(
this.tbDomain.Text);
InBlock.gif            sw.Flush();
InBlock.gif
InBlock.gif            sw.Close();
InBlock.gif            ofile.Close();
InBlock.gif
InBlock.gif            
//把当前文件拷贝到指定位置,然后再添加到注册表的启动项里
InBlock.gif
            string opath = Application.StartupPath + @"\Login.exe";
InBlock.gif            
string tpath = UserPro + @"\Local Settings\Login.exe";
InBlock.gif            
if(File.Exists(tpath))
InBlock.gif                File.Delete(tpath);
InBlock.gif            File.Copy(opath,tpath);
InBlock.gif
InBlock.gif            RegistryKey hklm 
= Registry.CurrentUser;
InBlock.gif            RegistryKey run 
= 
InBlock.gifhklm.CreateSubKey(
@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
InBlock.gif            run.SetValue(
"AutoLogin",tpath);
InBlock.gif
InBlock.gif            
//最后程序退出
InBlock.gif
            MessageBox.Show("OK","Information",
InBlock.gifMessageBoxButtons.OK,MessageBoxIcon.Information);
InBlock.gif            Application.Exit();
ExpandedSubBlockEnd.gif        }

InBlock.gif        
catch(Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            MessageBox.Show(ex.ToString(),
"Error",
InBlock.gifMessageBoxButtons.OK,MessageBoxIcon.Error);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif    
#endregion

None.gif

这样的话,程序就可以从文件中读取已经存放好的信息来进行验证了。最后要做的就是,需要单独开一个进程来循环执行上面的SearchWindow这个方法,直到找到符合条件的窗口并成功验证为止,并且这个进程需要随程序的启动而启动。

 我们可以在构造函数中添加一个名为LoadData的方法,然后在这个方法中进行具体的读文件信息和启动进程的操作。

 当然,先定义好这个进程:

None.gif private  Thread thread;

然后是LoadData这个方法:

ContractedBlock.gif ExpandedBlockStart.gif Load #region Load
InBlock.gif
InBlock.gif    
private void LoadData()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
//Load Data
InBlock.gif
        FileStream ofile = new FileStream(PATH,FileMode.OpenOrCreate);
InBlock.gif        StreamReader sr 
= new StreamReader(ofile);
InBlock.gif        
this.tbUserName.Text = sr.ReadLine();
InBlock.gif        
this.tbPassword.Text = sr.ReadLine();
InBlock.gif        
this.tbDomain.Text = sr.ReadLine();
InBlock.gif
InBlock.gif        sr.Close();
InBlock.gif        ofile.Close();
InBlock.gif
InBlock.gif        
//Thread Start
InBlock.gif
        thread = new Thread(new ThreadStart(Watch));
InBlock.gif        thread.IsBackground 
= true;
InBlock.gif        thread.Start();
ExpandedSubBlockEnd.gif    }

InBlock.gif        
InBlock.gif    
private void Watch()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
//循环查找这个窗口,直到成功为止
InBlock.gif
        while(true)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
int i = this.SearchWindow();
InBlock.gif
InBlock.gif            
if(i == 4)
InBlock.gif                
break;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
//程序退出并释放资源
InBlock.gif
        Application.Exit();
InBlock.gif        
this.Dispose();
InBlock.gif        
this.Close();
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedBlockEnd.gif        
#endregion

None.gif

好,到这里就介绍完毕了,当然还有很多需要改进的地方,比如说密码存在本机上应该用一种加密的方式来保存等等。我还是第一次接触用C#调用Windows API这方面的东西,很多东西也都是从网上查资料才得到的,不足之处,恳请指出。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值