在VC++程序中调用DOS命令例子

发布时间:2016-12-11 0:52:04 编辑:www.fx114.net 分享查询网我要评论
本篇文章主要介绍了"在VC++程序中调用DOS命令例子",主要涉及到在VC++程序中调用DOS命令例子方面的内容,对于在VC++程序中调用DOS命令例子感兴趣的同学可以参考一下。

方法一: CString FindDevice(char* szCmdLine) { char szDirectory[MAX_PATH] = {0}; GetCurrentDirectory(MAX_PATH,szDirectory); char TempFile[MAX_PATH] = {0}; wsprintf(TempFile,"%s\\NetStatus.txt",szDirectory); SECURITY_ATTRIBUTES sa = {sizeof(sa),NULL,TRUE}; SECURITY_ATTRIBUTES* psa = NULL; DWORD dwShareMode = FILE_SHARE_READ|FILE_SHARE_WRITE; OSVERSIONINFO osVersion = {0}; osVersion.dwOSVersionInfoSize = sizeof(osVersion); if (GetVersionEx(&osVersion)) { if (osVersion.dwPlatformId == VER_PLATFORM_WIN32_NT) { psa = &sa; dwShareMode |= FILE_SHARE_DELETE; } } HANDLE hConsoleRedirect = CreateFile(TempFile,GENERIC_WRITE,dwShareMode,psa,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL); ASSERT(hConsoleRedirect != INVALID_HANDLE_VALUE); STARTUPINFO s = {sizeof(s)}; s.dwFlags = STARTF_USESHOWWINDOW|STARTF_USESTDHANDLES; s.hStdOutput = hConsoleRedirect; s.wShowWindow = SW_HIDE; s.dwFlags = STARTF_USESTDHANDLES; s.hStdOutput = hConsoleRedirect; PROCESS_INFORMATION pi = {0}; if (CreateProcess(NULL,szCmdLine,NULL,NULL,TRUE,CREATE_NO_WINDOW,NULL,szDirectory,&s,&pi)) { WaitForSingleObject(pi.hProcess,INFINITE); CloseHandle(pi.hProcess); CloseHandle(pi.hThread); } CloseHandle(hConsoleRedirect); CFile myFile(TempFile,CFile::modeRead); ASSERT(myFile.m_hFile != NULL); char* pszNetStatus = new char[(unsigned int)(myFile.GetLength()+1)]; ZeroMemory(pszNetStatus,(size_t)(myFile.GetLength()+1)); myFile.Read(pszNetStatus,(Unit)(myFile.GetLength()+1)); myFile.Close(); DeleteFile(TempFile); CString strOutPut; strOutPut = CString(pszNetStatus); delete pszNetStatus; return strOutPut; } 方法二     SECURITY_ATTRIBUTES sa;     HANDLE hRead,hWrite;     bool isconn;     sa.nLength=sizeof(SECURITY_ATTRIBUTES);     sa.lpSecurityDescriptor=NULL;     sa.bInheritHandle=TRUE;     if (!CreatePipe(&hRead,&hWrite,&sa,0))      {         return NULL;     }     STARTUPINFO si;     PROCESS_INFORMATION pi;     si.cb=sizeof(STARTUPINFO);     GetStartupInfo(&si);     si.hStdError=hWrite;     si.hStdOutput=hWrite;     si.wShowWindow=SW_HIDE;     si.dwFlags=STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;     char runcmd[128] = "c://windows//system32//cmd.exe /c ";     sprintf(runcmd,"%s %s",runcmd,cmd);     if (!CreateProcess(NULL,runcmd,NULL,NULL,TRUE,0,NULL,NULL,&si,&pi))      {         return NULL;     }     CloseHandle(hWrite);     char buffer[256]={0};     DWORD bytesRead;     ReadFile(hRead,buffer,256,&bytesRead,NULL) && (bytesRead > 0);     memset(buffer,0,256);

上一篇:391B:Word Folding 贪心DP
下一篇:LLVM(一):简单介绍

相关文章

相关评论