源程序.docx
《源程序.docx》由会员分享,可在线阅读,更多相关《源程序.docx(88页珍藏版)》请在冰豆网上搜索。
源程序
面向3G无线网络的远程计算机控制
和维护装置部分源程序
二○一二年六月
广东广风隆电子科技有限公司
//客户端
#include
#include
#include
#include"string.h"
#include//MFCcoreandstandardcomponents
#include//MFCextensions
#include//MFCAutomationclasses
#include//MFCsupportforInternetExplorer4CommonControls
#include"Tlhelp32.h"
#defineMAX_LENGTH100
#pragmacomment(lib,"ws2_32.lib")
#include"afxsock.h"
#include
#pragmacomment(lib,"WINMM.LIB")
#include
#pragmacomment(lib,"wininet.LIB")
#include"Rpc.h"
#include
#pragmacomment(lib,"Rpcrt4.lib")
#include"psapi.h"
#pragmacomment(lib,"psapi.lib")
#include"Tlhelp32.h"
#include"Windows.h"
#include
#pragmacomment(lib,"Kernel32.lib")
#definem_hHost"192.168.0.50"//服务器IP
#definem_hPort1234//服务器端口
#defineMYPORT2345//本机开放的端口
#defineMAX_LEN1000
#defineCMD_GETHARDWAREINFO0x00000001//获得硬件信息命令格式--1:
标识:
0
#defineCMD_GETSOFTWAREINFO0x00000002//获得软件信息命令格式--2:
标识:
0
#defineCMD_INSTALLSOFTWARE0x00000003//安装软件命令格式--3:
标识:
安装包名称
#defineCMD_UNINSTALLSOFTWARE0x00000004//卸载软件命令格式--4:
标识:
软件名称
#defineCMD_GETPROCESSINFO0x00000005//获得进程信息命令格式--5:
标识:
0
#defineCMD_KILLPROCESS0x00000006//结束进程命令格式--6:
标识:
进程号
#defineCMD_GETSERVICEINFO0x00000007//获取服务信息命令格式--8:
标识:
0
#defineCMD_STOPSERVICE0x00000008//停止服务命令格式--7:
标识:
服务名称
#defineCMD_STARTSERVICE0x00000009//启动服务命令格式--9:
标识:
服务名称
#defineCMD_DELETESERVICE0x0000000A//删除服务命令格式--9:
标识:
服务名称
#definexunleiinsta1
#definexunleiuninsta1
SOCKETGlobalServerSocket;
typedefstruct_CMD_INFO
{
WORDmark;
DWORDm_cmd;
}CMD_INFO,*PCMD_INFO;//通知主机做好接受准备
typedefstruct_COMMAND_MSG
{
DWORDm_cmd;
WORDmark;
charwParam[255];
}COMMAND_MSG,*PCOMMAND_MSG;
typedefstruct_HOST_INFO
{
WORDmark;
//WORDgroup;
}HOST_INFO,*PHOST_INFO;
typedefstruct_HDWR_INFO
{
charosname[40];
charcpuname[50];
charmac[20];
charip[20];
charmem[20];
}HDWR_INFO,*PHDWR_INFO;
typedefstruct_SOFTINSTALL_INFO
{
charsoftinstallname[100];
WORDinstallstatus;
}SOFTINSTALL_INFO,*PSOFTINSTALL_INFO;
typedefstruct_PROCESSITEM
{
DWORDdwProcessID;//进程ID
charszExeFile[50];//进程名
DWORDdwMemoryUse;//进程使用的内存,以KB为单位
}PROCESSITEM,*PPROCESSITEM;
typedefstruct_PROCESSITEMPACK
{
DWORDnum;
PROCESSITEMpro[200];
}PROCESSITEMPACK,*PPROCESSITEMPACK;
typedefstruct_SOFTWAREITEM
{
charsoftname[100];
charsoftpub[50];
charsoftver[20];
charsoftins[20];
}SOFTWAREITEM,*PSOFTWAREITEM;
typedefstruct_SOFTWAREITEMPACK
{
DWORDnum;
SOFTWAREITEMsoft[200];
}SOFTWAREITEMPACK,*PSOFTWAREITEMPACK;
typedefstruct_SERVICEITEM
{
charsername[40];
charserdisname[100];
DWORDserstatype;
DWORDsersta;
}SERVICEITEM,*PSERVICEITEM;
typedefstruct_SERVICEITEMPACK
{
DWORDnum;
SERVICEITEMser[200];
}SERVICEITEMPACK,*PSERVICEITEMPACK;
//函数声明
voidAgainConnectServer(CMD_INFO*IdCmd);
voidConnectServer();
voidListenThread();
voidRecvCommand(SOCKETss);
//硬件信息相关函数声明
voidShowHardwareInfo(COMMAND_MSGmsg);
BOOLGetOsInfor(char*szOsName);
voidGetCPUInfo(intcpuinfo[4],inttp);
voidGetCPUInfoStr(char*sCpuname);
voidGetMemInfo(char*sMem);
CStringGetMac();
CStringGetIp();
//进程信息相关函数声明
DWORDGetProcessMemUse(DWORDdwProcessID);
voidShowProcessInfo(COMMAND_MSGmsg);
//软件信息相关函数声明
voidShowSoftwareInfo(COMMAND_MSGmsg);
BOOLEnableDebugPriv();
//voidDumpSoftware(LPCTSTRszKey,HKEYhParent,SOFTWAREITEM&info);
LRESULTGetValue_key(HKEYhKey,LPCTSTRname,LPTSTRvalue,LPLONGsize);
//服务信息相关函数声明
voidShowServiceInfo(COMMAND_MSGmsg);
voidStopService(COMMAND_MSGmsg);
voidStartService(COMMAND_MSGmsg);
voidDeleteService(COMMAND_MSGmsg);
//软件安装卸载相关函数声明
voidInstallSoft(COMMAND_MSGmsg);
voidUnInstallSoft(COMMAND_MSGmsg);
CStringGetFileTitleFromFileName(CStringFileName,BOOLExt);
char*urlload(char*u);
//voidDeleteDirectory(CStringstrDir);
CStringGetWinRARPath();
//函数定义
/**************************************************软件安装卸载begin***************************************************/
CStringGetWinRARPath()
{
HKEYhKEY;
LPCTSTRRgspath=_T("Software\\WinRAR");
LONGret=RegOpenKeyEx(HKEY_LOCAL_MACHINE,Rgspath,0,KEY_READ,&hKEY);
if(ret!
=ERROR_SUCCESS)
{
//MessageBox(NULL,_T("获取WinRAR安装路径失败!
"),_T("出错啦~!
"),MB_OK);
RegCloseKey(hKEY);
return_T("");
}
//读取键值内容
DWORDtype=REG_SZ;
BYTEPathInfo[MAX_PATH];
DWORDdwInfoSize=MAX_PATH;
ret=RegQueryValueEx(hKEY,_T("exe32"),NULL,&type,PathInfo,&dwInfoSize);
if(ret!
=ERROR_SUCCESS)
{
//MessageBox(NULL,_T("没有安装WinRar解压缩软件,请先安装WinRar解压缩软件"),_T("出错啦~!
"),MB_OK);
RegCloseKey(hKEY);
return_T("");
}
CStringPath=(wchar_t*)PathInfo;
returnPath;
}
voidInstallSoft(COMMAND_MSGmsg)
{
SOFTINSTALL_INFOinfo;
memset(&info,0,sizeof(SOFTINSTALL_INFO));
charbuf[255]={0};
memcpy(buf,msg.wParam,255);
strcat(buf,".rar");
chara[100]="http:
//192.168.0.46:
8081/";
strcat(a,buf);//获得安装包下载路径
system("rd/s/q\"C:
\\WINDOWS\\system32\\soft\"");//删除目录
system("mkdir\"C:
\\WINDOWS\\system32\\soft\"");//创建目录
//charc[100]="http:
//192.168.0.55:
8081/InstallPath.txt";//安装路径---配置文件下载路径
charpath[100]={0};
strcpy(path,urlload(a));//rar下载路径
CStringPluginName;
PluginName.Format("%s",buf);
CStringPluginPath=_T("C:
\\WINDOWS\\system32\\soft\\")+PluginName;
CStringWinRARParam=_T("e-o+")+PluginPath+_T("")+_T("C:
\\WINDOWS\\system32\\soft");
CStringCommend=_T("C:
\\ProgramFiles\\WinRAR\\Rar.exe")+WinRARParam;
STARTUPINFOsi={sizeof(si)};
PROCESS_INFORMATIONpi;
BOOLbRet=CreateProcess(NULL,Commend.GetBuffer(Commend.GetLength()),NULL,NULL,FALSE,0,NULL,NULL,&si,&pi);
if(bRet)
{
if(!
WaitForSingleObject(pi.hProcess,INFINITE))
{
/*charold[100]={0};
strcpy(old,urlload(c));
charfilenew[]="C:
\\WINDOWS\\system32\\InstallPath.ini";
rename(old,filenew);
DeleteFile(old);//删除原来的txt文件,不知道为什么tcp传输不了ini文件
*/
info.installstatus=1;
strcpy(info.softinstallname,msg.wParam);
//CStringintallpath=_T("C:
\\WINDOWS\\system32\\soft\\");
char*intallpath=(char*)malloc(sizeof(char)*256);
strcpy(intallpath,"C:
\\WINDOWS\\system32\\soft\\");
strcat(intallpath,msg.wParam);
strcat(intallpath,"_install.exe");
PROCESS_INFORMATIONPI;
STARTUPINFOSI;
memset(&SI,0,sizeof(SI));
SI.cb=sizeof(SI);
intret;
ret=CreateProcess(intallpath,NULL,NULL,NULL,FALSE,NORMAL_PRIORITY_CLASS,NULL,NULL,&SI,&PI);
if(ret)
send(GlobalServerSocket,(char*)(&info),sizeof(SOFTINSTALL_INFO),0);
//system("rd/s/q\"C:
\\WINDOWS\\system32\\soft\"");//删除目录
}
}
//WinExec("C:
\\ProgramFiles\\WinRAR\\Rar.exee-O+C:
\\WINDOWS\\system32\\soft\\ThunderMini.rarC:
\\WINDOWS\\system32\\soft",SW_SHOW);
}
voidUnInstallSoft(COMMAND_MSGmsg)
{
SOFTINSTALL_INFOinfo;
memset(&info,0,sizeof(SOFTINSTALL_INFO));
info.installstatus=0;
strcpy(info.softinstallname,msg.wParam);
char*intallpath=(char*)malloc(sizeof(char)*256);
strcpy(intallpath,"C:
\\WINDOWS\\system32\\soft\\");
strcat(intallpath,msg.wParam);
strcat(intallpath,"_uninstall.exe");
PROCESS_INFORMATIONPI;
STARTUPINFOSI;
memset(&SI,0,sizeof(SI));
SI.cb=sizeof(SI);
send(GlobalServerSocket,(char*)(&info),sizeof(SOFTINSTALL_INFO),0);
intret;
ret=CreateProcess(intallpath,NULL,NULL,NULL,FALSE,NORMAL_PRIORITY_CLASS,NULL,NULL,&SI,&PI);
if(!
ret)
printf("卸载失败!
");
//system("rd/s/q\"C:
\\WINDOWS\\system32\\soft\"");//删除目录
}
char*urlload(char*u)//"http:
//192.168.0.55:
8081/xunlei/thunder_install.exe"
{charurl[255]={0};
strcpy(url,u);
charseps[]="/";
char*token;
char*file;
charmyURL[MAX_PATH]={0};
//charmyFILE[MAX_PATH]={0};
char*myFILE=(char*)malloc(sizeof(char)*256);
strcpy(myURL,url);
token=strtok(myURL,seps);
while(token!
=NULL)
{
file=token;
token=strtok(NULL,seps);
}
GetSystemDirectory(myFILE,MAX_PATH);
strcat(myFILE,"\\");
strcat(myFILE,"soft\\");
strcat(myFILE,file);
printf(url);
printf(myFILE);
HRESULThr;
hr=URLDownloadToFile(0,url,myFILE,0,0);
DeleteUrlCacheEntry(url);
if(hr!
=S_OK)
{
printf("下载不成功");
returnNULL;
}
printf("下载成功");
returnmyFILE;
}
CStringGetFileTitleFromFileName(CStringFileName,BOOLExt)
{
intWhere;
Where=FileName.ReverseFind('\\');
if(Where==-1)
Where=FileName.ReverseFind('/');
CStringFileTitle=FileName.Right(FileName.GetLength()-1-Where);
if(!
Ext)
{
intWhich=FileTitle.ReverseFind('.');
if(Which!
=-1)
FileTitle=FileTitle.Left(Which);
}
returnFileTitle;
}
/**************************************************软件安装卸载over***************************************************/
/**************************************************获取服务信息begin***************************************************/
voidStopService(COMMAND_MSGmsg)
{
SC_HANDLEscm;
SC_HANDLEservice;
if((scm=OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS))==NULL)
{
printf("OpenSCManagerError/n");
}
service=OpenService(scm,msg.wParam,SERVICE_QUERY_CONFIG|SERVICE_ALL_ACCESS);
if(!
service)
{
printf("OpenServiceerror!
\n");
}
SERVICE_STATUSstatus;
BOOLisSuccess=QueryServiceStatus(service,&status);
if(!
isSuccess)
{
printf("QueryServiceStatuserror!
/n");
}
if(status.dwCurrentState!
=SERVICE_STOPPED)
{
isSuccess=ControlService(service,SERVICE_CONTROL_STOP,&status);
if(!
isSuccess)
{
printf("停止服务失败!
");
}
else
{
printf("停止服务成功!
");
send(GlobalServerSocket,msg.wParam,sizeof(msg.wParam),0);
}
}
}
voidStartService(COMMAND_MSGmsg)
{
SC_HANDLEscm;
SC_HANDLEservice;
if((scm=OpenSCManager(NULL,NULL