自动联网

编译环境是 Visual Studio 10 Express


#include "windows.h"
#include "commctrl.h"
#include "sensapi.h"
#include "string.h"
#pragma comment(lib,"Sensapi.lib")
using namespace std;

void runAtWinStart()
{
	char pszFileName[MAX_PATH];	
	const char *pszToReg;				//最终要写到注册表的字符串
	HKEY hKey;
	DWORD lpType;
	BYTE lpData[MAX_PATH];
	DWORD lpcbData[MAX_PATH/4];

	GetModuleFileName(GetModuleHandle(0),pszFileName,MAX_PATH);
	string strToReg(pszFileName);
	strToReg.push_back('\"');
	strToReg.insert(0,1,'\"');
	pszToReg = strToReg.c_str();

	//Check Registry
	RegOpenKeyEx(HKEY_CURRENT_USER,"Software\\Microsoft\\Windows\\CurrentVersion\\Run",0,KEY_ALL_ACCESS,&hKey);

	LONG ret = RegQueryValueEx(hKey,"ConnectNet",0,&lpType,&lpData[0],&lpcbData[0]);

	if(ret != ERROR_SUCCESS || strcmp((const char *)&lpData[0],pszToReg) != 0){
		//#define REG_SZ 1
		RegSetValueEx(hKey,"ConnectNet",0,REG_SZ,(const unsigned char *)pszToReg,strlen(pszToReg)+1);
	}

	RegCloseKey(hKey);
}

int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPTSTR    lpCmdLine,
                     int       nCmdShow)
{
	HWND h1,h2,h3,h4;

	runAtWinStart();

	Sleep(5000);

	while(1){
		h1 = FindWindow("#32770","拨号连接");

		if(h1 != NULL){
			h2 = FindWindowEx(h1,0,"Button","连接(&C)");
			if(h2 != NULL){
				while(IsWindowEnabled(h1)){
					PostMessage(h2,BM_CLICK,0,0);
					Sleep(1000);
				}
			}
		}
		break;

		Sleep(1000);
		
	}
	while(1){
		h3 = FindWindow("#32770","连接 宽带连接");
		//h3 = FindWindowEx(h1,0,"#32770","连接 宽带连接");
		if(h3 != NULL){
			h4 = FindWindowEx(h3,0,"Button","连接(&C)");
			if(h4 != NULL){
				while(IsWindowVisible(h3)){
					PostMessage(h4,BM_CLICK,0,0);
					Sleep(1000);
				}
			}
			break;
		}
		Sleep(1000);
	}

	while(1){
		DWORD dw;
		if(IsNetworkAlive(&dw)) break;
		Sleep(1000);
	}

	WinExec("C:\\Users\\Administrator\\AppData\\Roaming\\Tencent\\QPlus\\QPlus.exe",SW_NORMAL);

	return 1;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值