C/C++ system使用的若干坑

1、在实战项目中 system在Windows下类似于向dos发送命令作用
2、在MFC下面 CFileDialg使用之后,IO访问层面的当前路径就会被修改,使用相对路径会受到影响
3、管道技术:在跨进程间的通讯是个不错的方法。以本次项目为例;Exe发送dos命令
//管??道???命??令??
void Pipe ( char * strExe , char * szArgman , CString & strOuter )
{
        CString   strCmdLine( "" );  
       strCmdLine. Format ( "%s %s" , strExe, szArgman);  //strCmd = "ipconfig.exe" 
        //创???建??管??道??? 
        HANDLE                    hReadPipe;   
        HANDLE                    hWritePipe; 
        SECURITY_ATTRIBUTES       sat;                            //安?2全??属??性?结??构1 
       sat. nLength                  = sizeof ( SECURITY_ATTRIBUTES );  //结??构1体??大???小? 
       sat. bInheritHandle          = true ;                         //指?出?安?2全??描??述??符??的??对?象??能??否??被??新?进?程??继??承D 
       sat. lpSecurityDescriptor = NULL ;                         //安?2全??描??述??符??,NULL: 使?1用??默?认??的??  
        if ( ! CreatePipe ( &hReadPipe, &hWritePipe, &sat, NULL ) )   
       {   
              strOuter = _T ( "Create Pipe Error!" );
               return ;    
       } 
        //创???建??进?程?? 
        STARTUPINFO                  startupinfo;             //进?程??信?息?? 
        PROCESS_INFORMATION    pinfo; 
       startupinfo. cb                  = sizeof ( STARTUPINFO ); //结??构1体??大???小?  
        GetStartupInfo ( &startupinfo );                  //获?取??当???前??进?程??的??信?息?? 
       startupinfo. hStdError         = hWritePipe;   
       startupinfo. hStdOutput      = hWritePipe;   
       startupinfo. dwFlags           = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES ;   
       startupinfo. wShowWindow = SW_HIDE ;     
        if ( ! CreateProcess ( NULL , strCmdLine. GetBuffer (0), NULL , NULL
               TRUE , NULL , NULL , NULL , &startupinfo, &pinfo) )   
       {    
              strOuter = _T ( "CreateProcess " ); 
               return ;    
       }   
        CloseHandle ( hWritePipe );  
        //获?取??管??道???信?息?? 
        BYTE        buffer [1024]; 
        DWORD     byteRead; 
        while ( true )   
       {   
               RtlZeroMemory ( buffer , 1024 );  
               if ( ReadFile ( hReadPipe, buffer , 1023, &byteRead, NULL ) == NULL
              {   printf ( "%s" , buffer );
                      break ;    
              }
               printf ( "\r\n" );
         strOuter = buffer ;
       }   
        CloseHandle ( hReadPipe );  
       strCmdLine. ReleaseBuffer (); 
}
4、调整IO对应的根目录
    #include <stdlib.h>
    #include <direct.h>
    _chdir(char szPath);    //  设置IO的当前路径

猜你喜欢

转载自blog.csdn.net/www1157763637qqcom/article/details/80279544