编译环境是 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;
}