问题描述
程序编译通不过,请高人指点!有一个错误,我用vc++6.0测试的#include<stdio.h>#include<stdlib.h>#include<Windows.h>#include<tlhelp32.h>/*PROCESSENTRY32pr32={0};HANDLEhSnap;*/wchar_tLogFile[256]={0};//dll入口BOOLAPIENTRYDllMain(HANDLEhModule,DWORDul_reason_for_call,LPVOIDlpReserved){returntrue;}voidLogToFile(char*lpFmt,...){charbuff[1024];va_listarglist;va_start(arglist,lpFmt);_vsnprintf(buff,sizeof(buff),lpFmt,arglist);va_end(arglist);/*MessageBoxA(NULL,"BegainWideCharToMultiByte","FileName",MB_OK);DWORDdwTextLen=WideCharToMultiByte(CP_ACP,0,LogFile,-1,NULL,0,NULL,NULL);MessageBoxA(NULL,"DoneWideCharToMultiByte","FileName",MB_OK);char*szFileName=newchar[dwTextLen+1];memset((void*)szFileName,0,dwTextLen+1);WideCharToMultiByte(CP_ACP,0,LogFile,-1,szFileName,dwTextLen,NULL,NULL);*//*MessageBox(NULL,LogFile,L"FileName",MB_OK);MessageBoxA(NULL,buff,"Log",MB_OK);*/HANDLEhFile;__try{hFile=CreateFileW(LogFile,GENERIC_WRITE,FILE_SHARE_READ,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);DWORDdwWritten=0;WriteFile(hFile,buff,strlen(buff),&dwWritten,NULL);}__except(EXCEPTION_EXECUTE_HANDLER){//charerror[256]={0};//MessageBoxA(NULL,itoa(GetLastError(),error,10),"Error",MB_OK);}//MessageBoxA(NULL,"Afterfopen","FileName",MB_OK);/*FILE*fp=fopen("e:\1.txt","w");MessageBoxA(NULL,"Afterfopen","FileName",MB_OK);if(fp){MessageBoxA(NULL,buff,"Log",MB_OK);fprintf(fp,"%sn",buff);fclose(fp);}else{MessageBoxA(NULL,"openfileerror","Log",MB_OK);}*/}/*//列举进程BOOLListProcess(){pr32.dwSize=sizeof(PROCESSENTRY32);hSnap=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);if(hSnap==(HANDLE)-1){returnfalse;}if(Process32First(hSnap,&pr32)==FALSE){CloseHandle(hSnap);returnfalse;}returntrue;}//查找父进程的PID,失败返回-1DWORDFindParentProcID(){ListProcess();DWORDdwCurrentID=GetCurrentProcessId();do{if(pr32.th32ProcessID==dwCurrentID){CloseHandle(hSnap);returnpr32.th32ParentProcessID;}}while(Process32Next(hSnap,&pr32));CloseHandle(hSnap);return-1;}typedefstructtagWNDINFO{DWORDdwProcessId;HWNDhWnd;}WNDINFO,*LPWNDINFO;BOOLCALLBACKYourEnumProc(HWNDhWnd,LPARAMlParam){DWORDdwProcessId;GetWindowThreadProcessId(hWnd,&dwProcessId);LPWNDINFOpInfo=(LPWNDINFO)lParam;if(dwProcessId==pInfo->dwProcessId){pInfo->hWnd=hWnd;returnFALSE;}returnTRUE;}//找到进程主窗口的hWnd,如果没有,返回NULLHWNDGetProcessMainWnd(DWORDdwProcessId){WNDINFOwi;wi.dwProcessId=dwProcessId;wi.hWnd=NULL;EnumWindows(YourEnumProc,(LPARAM)&wi);returnwi.hWnd;}//向父窗口写数据voidSendBufferToConsole(char*szBuffer){DWORDdwProcID;HWNDhWnd;dwProcID=FindParentProcID();if(dwProcID==-1){return;}hWnd=GetProcessMainWnd(dwProcID);if(hWnd==NULL){return;}for(inti=0;i<strlen(szBuffer);i++){if(szBuffer[i]=='n'||szBuffer[i]=='r'){szBuffer[i]='';}SendMessageW(hWnd,WM_CHAR,WPARAM(szBuffer[i]),NULL);}SendMessageW(hWnd,WM_CHAR,WPARAM('r'),NULL);SendMessageW(hWnd,WM_CHAR,WPARAM('n'),NULL);}*/voidRunExe(HWNDhwnd,HINSTANCEhinst,LPSTRszCmdLine,intnCmdShow){if(szCmdLine==NULL){return;}DWORDdwNum=MultiByteToWideChar(CP_ACP,0,szCmdLine,-1,NULL,0);wchar_t*wszCmdLine=newwchar_t[dwNum];if(!wszCmdLine){return;}MultiByteToWideChar(CP_ACP,0,szCmdLine,-1,wszCmdLine,dwNum);//MessageBox(NULL,wszCmdLine,L"CommandLine",MB_OK);intargc;LPWSTR*argv=CommandLineToArgvW(wszCmdLine,&argc);wchar_tCmd[256]={0};wchar_tArgs[1024]={0};//最少要两个参数,exe程序名和回显log文件名if(argc<2){return;}wcsncpy(LogFile,argv[0],sizeof(LogFile)-1);wcsncpy(Cmd,argv[1],sizeof(Cmd)-1);//MessageBox(NULL,LogFile,L"LogFile",MB_OK);//MessageBox(NULL,Cmd,L"Command",MB_OK);//还有其它提供给exe程序的参数if(argc>2){for(intindex=2;index<argc;index++){wcscat(wcscat(Args,L""),argv[index]);}}//MessageBox(NULL,Args,L"Arguments",MB_OK);SECURITY_ATTRIBUTESsa;HANDLEhRead,hWrite;sa.nLength=sizeof(SECURITY_ATTRIBUTES);sa.lpSecurityDescriptor=NULL;sa.bInheritHandle=TRUE;if(!CreatePipe(&hRead,&hWrite,&sa,0)){return;}//MessageBox(NULL,L"Createpipeok",L"pipe",MB_OK);STARTUPINFOsi;memset((void*)&si,0,sizeof(STARTUPINFOA));GetStartupInfoW(&si);si.dwFlags=STARTF_USESHOWWINDOW|STARTF_USESTDHANDLES;si.cb=sizeof(STARTUPINFO);si.wShowWindow=SW_HIDE;si.hStdError=hWrite;si.hStdOutput=hWrite;PROCESS_INFORMATIONprocessInfo;memset((void*)&processInfo,0,sizeof(PROCESS_INFORMATION));//建立进程boolret=CreateProcessW(Cmd,Args,&sa,NULL,1,0,NULL,NULL,&si,&processInfo);if(!ret){return;}//MessageBox(NULL,L"Createprocessok",L"process",MB_OK);charszBuffer[4096]={0};DWORDdwRead=0;while(true){//测试管道是否可读,20次尝试还没有数据,直接跳出for(inti=0;i<60;i++){dwRead=0;PeekNamedPipe(hRead,szBuffer,4095,&dwRead,NULL,NULL);//无数据等待1秒后重试if(dwRead<=0){//MessageBox(NULL,L"Nodatacanread",L"PeekNamedPipe",MB_OK);Sleep(1000);}//有数据则跳出else{//MessageBox(NULL,L"Somedatacanread",L"PeekNamedPipe",MB_OK);break;}}//判断是有数据跳出,还是无数据尝试超过最大次数而跳出if(dwRead<=0){//MessageBox(NULL,L"Nodatacanreadfinally",L"PeekNamedPipe",MB_OK);break;}//执行到此处,说明有数据可读memset(szBuffer,0,4096);if(!ReadFile(hRead,szBuffer,4095,&dwRead,NULL)){//MessageBox(NULL,L"ReadFilefailed",L"ReadFile",MB_OK);break;}//MessageBox(NULL,szBuffer,L"ReadFile",MB_OK);LogToFile(szBuffer);/*boolbRunning=false;ListProcess();do{if(pr32.th32ProcessID==processInfo.dwProcessId){bRunning=true;break;}}while(Process32Next(hSnap,&pr32));if(!bRunning){break;}*/}//MessageBoxA(NULL,"willterminateprocess","Main",MB_OK);if(processInfo.hProcess){TerminateProcess(processInfo.hProcess,0);}}
解决方案
解决方案二:
对了,这个程序我想生成DLL文件,想用rundll32dllname,functionname[arguments]这个命令执行程序
解决方案三:
STARTUPINFOWsi;
解决方案四:
怎么不缩进一下啊?看着太费劲了…
解决方案五:
具体怎么修改呢
解决方案六:
cplusplus好像放错地方啦呵呵
解决方案七:
高人帮我调试成功好吗?求求你拉