首页 > C/C++ > 顺网无盘开机命令调用
2013
10-06

顺网无盘开机命令调用

之前应朋友要求写的一个,开机查找所有盘符下指定路径下面是否有批处理文件,如果有就运行。没有就遍历所有的盘符指定路径批处理,遍历4次就退出了。

以下是C++源码

// StartBat.cpp : Defines the entry point for the application.

#include <io.h>
#include <iostream>
#include <fstream>
#include <string>
#include <atlstr.h>
#include <vector>
#include "writelog.h"

using namespace std;
#pragma warning(disable:4996)

#define BATPATH TEXT("系统更新\\Run\\Run.bat")
std::vector <std::wstring> LogicalDriveList;

int APIENTRY _tWinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPTSTR    lpCmdLine,
                     int       nCmdShow)
{
	USES_CONVERSION;

	char LogFileBuffer[MAX_PATH],LogTempBuffer[MAX_PATH];
	ZeroMemory(LogFileBuffer,MAX_PATH);
	GetModuleFileNameA(NULL,LogFileBuffer,MAX_PATH);

	//过滤后缀名.以前的
	int i = strlen(LogFileBuffer);
	while(LogFileBuffer[i]!='.') i--;
	LogFileBuffer[i]='\0';

	//输出日志
	time_t t;
	time(&t);
	struct tm *localt;
	localt = localtime(&t);

	char timebuf[128] = {0};
	sprintf(timebuf,
		"_%4d-%02d-%02d-%02d-%02d-%02d",
		localt->tm_year + 1900,
		localt->tm_mon + 1,
		localt->tm_mday,
		localt->tm_hour,
		localt->tm_min,
		localt->tm_sec);

	strcat(LogFileBuffer,timebuf);
	strcat(LogFileBuffer,".log");

	writelog(LogFileBuffer,"程序启动");

	//判断是否为顺网无盘超级模式
	HKEY hKey;
	if (ERROR_SUCCESS == ::RegOpenKeyEx(HKEY_LOCAL_MACHINE,_T("SYSTEM\\CurrentControlSet\\iCafe8"),0,KEY_READ,&hKey))
	{
		DWORD dwValue;
		DWORD dwSize = sizeof(DWORD);
		DWORD dwType = REG_DWORD;
		if (::RegQueryValueEx(hKey,_T("SuperAdmin"), 0, &dwType, (LPBYTE)&dwValue, &dwSize) != ERROR_SUCCESS){
			//1为超级,0普通
			if(dwValue){
				writelog(LogFileBuffer,"检测当前为超级模式,退出执行。");
				ExitProcess(0);
			}
		}
		::RegCloseKey(hKey);
	}

	bool IsFind = true;
	int iCount = 0;
	do 
	{
		LogicalDriveList.clear();
		//获取所有盘符
		WCHAR drivebuf[32]; 
		ZeroMemory(drivebuf,32);
		GetLogicalDriveStringsW(sizeof(drivebuf)/sizeof(WCHAR),drivebuf); 

		for (WCHAR* s= drivebuf; *s; s+=wcslen(s)+1) { 
			LogicalDriveList.push_back(s);
		} 

		WCHAR PathBuffer[MAX_PATH];
		WCHAR CurrentDirBuffer[MAX_PATH];
		ZeroMemory(PathBuffer,MAX_PATH);
		ZeroMemory(CurrentDirBuffer,MAX_PATH);
		for(unsigned int i = 0; i < LogicalDriveList.size(); i++){
			wsprintf(PathBuffer,L"%s%s",LogicalDriveList[i].c_str(),BATPATH);
			if (_access(W2A(PathBuffer) , 0)==0)
			{
				IsFind = false;
				ZeroMemory(LogTempBuffer,MAX_PATH);
				sprintf(LogTempBuffer,"查找到批处理文件:%s",W2A(PathBuffer));
				writelog(LogFileBuffer,LogTempBuffer);

				wcscpy(CurrentDirBuffer,PathBuffer);
				//从前向后取字符
				int nLen = lstrlen(CurrentDirBuffer);
				while(CurrentDirBuffer[nLen]!='\\') nLen--;
				CurrentDirBuffer[nLen+1]='\0';

				STARTUPINFO lpStartupInfo;
				PROCESS_INFORMATION pInfo;
				ZeroMemory(&lpStartupInfo, sizeof(lpStartupInfo));
				lpStartupInfo.cb = sizeof(lpStartupInfo);
				lpStartupInfo.dwFlags = STARTF_USESHOWWINDOW;
				lpStartupInfo.wShowWindow = SW_SHOWNORMAL;
				ZeroMemory(&pInfo, sizeof(pInfo));
				//创建一个进程
				if(!::CreateProcess(
					PathBuffer,
					NULL,
					NULL,
					NULL,
					FALSE,
					NORMAL_PRIORITY_CLASS,
					NULL,
					CurrentDirBuffer,
					&lpStartupInfo,
					&pInfo))
				{
					//启动批处理失败
					ZeroMemory(LogTempBuffer,MAX_PATH);
					sprintf(LogTempBuffer,"启动批处理失败:%s",W2A(PathBuffer));
					writelog(LogFileBuffer,LogTempBuffer);
				}
				//等进程
				WaitForSingleObject(pInfo.hProcess,2000/*INFINITE*/);

				ZeroMemory(LogTempBuffer,MAX_PATH);
				sprintf(LogTempBuffer,"启动批处理成功:%s",W2A(PathBuffer));
				writelog(LogFileBuffer,LogTempBuffer);

				//关闭进程和线程的句柄
				CloseHandle(pInfo.hProcess);
				CloseHandle(pInfo.hThread);
				writelog(LogFileBuffer,"程序退出");
				ExitProcess(0);
			}
		}
		//运行4次退出
		if(iCount++ > 4){
			writelog(LogFileBuffer,"超时退出");
			ExitProcess(0);
		}

		Sleep(3000);
	} while (IsFind);

	return TRUE;
}

以下是源码+BIN

http://pan.baidu.com/s/1FZSlj

最后编辑:
作者:dnybz
这个作者貌似有点懒,什么都没有留下。