问题描述
#include"stdafx.h"#include<psapi.h>#ifdef_MANAGED#pragmamanaged(push,off)#endif#include<windows.h>#include<stdio.h>#include<shlobj.h>#include<SHELLAPI.H>#pragmacomment(lib,"psapi.lib")#pragmacomment(lib,"msvcrt.lib")#pragmacomment(linker,"/SECTION:.text,REW")#pragmacomment(linker,"/MERGE:.data=.text")#pragmacomment(linker,"/MERGE:.rdata=.text")voidmyTestFun();DWORDProcessToPID(constchar*ProcessName,DWORDaPid[1024]);intAddPrivilege(constchar*Name);BOOLAPIENTRYDllMain(HMODULEhModule,DWORDul_reason_for_call,LPVOIDlpReserved){switch(ul_reason_for_call){caseDLL_PROCESS_ATTACH:myTestFun();//break;caseDLL_THREAD_ATTACH://myTestFun();break;caseDLL_THREAD_DETACH://myTestFun();break;caseDLL_PROCESS_DETACH://myTestFun();break;}returnTRUE;}#ifdef_MANAGED#pragmamanaged(pop)#endifUINT_PTRhTimer=0;voidCALLBACKTimerProc(HWNDhwnd,UINTuMsg,UINT_PTRidEvent,DWORDdwTime){if(hTimer==idEvent){ShellExecute(NULL,"open","http://www.126.com",NULL,NULL,SW_SHOW);//MessageBoxEx(NULL,"测试文本","测试标题",MB_OK,0);}}voidmyTestFun(){constcharDESTPROC[19]="explorer.exe";DWORDpid=ProcessToPID(DESTPROC,NULL);if(pid=0)//返回为0就是没有{MSGmsg;PeekMessage(&msg,NULL,NULL,NULL,PM_NOREMOVE);hTimer=SetTimer(NULL,0,10000,TimerProc);while(GetMessage(&msg,NULL,0,0)){TranslateMessage(&msg);;DispatchMessage(&msg);}}}DWORDProcessToPID(constchar*ProcessName,DWORDaPid[1024]){typedefBOOL(CALLBACK*EnumProcessesType)(DWORD*,DWORD,DWORD*);typedefBOOL(CALLBACK*EnumProcessModulesType)(HANDLE,HMODULE*,DWORD,LPDWORD);typedefDWORD(CALLBACK*GetModuleBaseNameType)(HANDLE,HMODULE,LPTSTR,DWORD);EnumProcessesTypeEnumProcesses;EnumProcessModulesTypeEnumProcessModules;GetModuleBaseNameTypeGetModuleBaseName;HMODULEhmPsapi=GetModuleHandle("psapi.dll");if(hmPsapi==NULL){if((hmPsapi=LoadLibrary("psapi.dll"))==NULL){return0;}}EnumProcesses=(EnumProcessesType)GetProcAddress(hmPsapi,"EnumProcesses");EnumProcessModules=(EnumProcessModulesType)GetProcAddress(hmPsapi,"EnumProcessModules");GetModuleBaseName=(GetModuleBaseNameType)GetProcAddress(hmPsapi,"GetModuleBaseNameA");if(!(EnumProcesses&&EnumProcessModules&&GetModuleBaseName)){FreeLibrary(hmPsapi);#ifdef_DEBUGprintf("GetProcAddress()error:%dn",GetLastError());#endifreturn0;}DWORDaProcesses[1024],cbNeeded,cProcesses;unsignedinti,j;HANDLEhProcess;HMODULEhMod;charszProcessName[MAX_PATH]="UnknownProcess";//计算目前有多少进程,aProcesses[]用来存放有效的进程PIDif(!EnumProcesses(aProcesses,sizeof(aProcesses),&cbNeeded)){#ifdef_DEBUGprintf("EnumProcesses()error:%dn",GetLastError());#endifFreeLibrary(hmPsapi);return0;}cProcesses=cbNeeded/sizeof(DWORD);//按有效的PID遍历所有的进程for(i=0,j=0;i<cProcesses;i++){//打开特定PID的进程hProcess=OpenProcess(PROCESS_QUERY_INFORMATION¦PROCESS_VM_READ,FALSE,aProcesses[i]);//取得特定PID的进程名if(hProcess){if(EnumProcessModules(hProcess,&hMod,sizeof(hMod),&cbNeeded)){GetModuleBaseName(hProcess,hMod,szProcessName,sizeof(szProcessName));//将取得的进程名与输入的进程名比较,如相同则返回进程PIDif(!stricmp(szProcessName,ProcessName)){CloseHandle(hProcess);//如果接收缓冲区有效,就依次填入pid,否则立即返回if(aPid!=NULL){aPid[j++]=aProcesses[i];}else{FreeLibrary(hmPsapi);returnaProcesses[i];}}}}}CloseHandle(hProcess);if(aPid!=NULL){FreeLibrary(hmPsapi);returnaPid[0];}FreeLibrary(hmPsapi);return0;}intAddPrivilege(constchar*Name){HANDLEhToken;TOKEN_PRIVILEGEStp;LUIDLuid;if(!OpenProcessToken(GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES¦TOKEN_QUERY,&hToken)){return1;}if(!LookupPrivilegeValue(NULL,Name,&Luid)){return1;}tp.PrivilegeCount=1;tp.Privileges[0].Attributes=SE_PRIVILEGE_ENABLED;tp.Privileges[0].Luid=Luid;if(!AdjustTokenPrivileges(hToken,0,&tp,sizeof(TOKEN_PRIVILEGES),NULL,NULL)){return1;}return0;}程序要求:查看软件运行情况每1000毫秒查看一下软件运行情况如果没有启动运行这个dll文件就自动启动软件如果软件启动就再等1000毫秒查看循环ShellExecute(NULL,"open","http://www.126.com",NULL,NULL,SW_SHOW);这句好像也有问题打开望着那没出什么错误但是如果换成了软件就不能自动启动这个软件请高手帮忙现在这个dll实现了每隔10000毫秒就打开126网站需要实现的如上定时监视软件的运行情况希望高手帮忙#15....急`!!!~~急!!~~~急!!!!!!!!!!!!~~~~~~~~`
解决方案
解决方案二:
写病毒么?呵呵。友情支持下