管道通信

本文介绍了一种使用C++程序进行内存分析的方法,通过创建管道、启动子进程并读取其输出来获取内存使用信息。
 
void __fastcall TMake::AddMemorySizeInfo(void){
    
if(CheckExFile() && (ErrorCount==0&& !SrcCompile && isMake) {
    
        
char cppdllpath[MAX_PATH+1],setuppath[MAX_PATH+1],mapfilepath[MAX_PATH+1],exfilepath[MAX_PATH+1],appname[FIX_BufSize_M+1];

        BuildSocket
->DFSendMessage(MSG_GetApplicationName, NULL, appname, FIX_BufSize_M);
        
if(GetSetupDirectory(appname, cppdllpath, MAX_PATH)==NULL){
            
return;
        }

        strcat(cppdllpath,
"/bin/");
        
        
if(!CheckExFile(exfilepath)){
            
return;
        }

        
        BuildSocket
->GetMapFile(mapfilepath, MAX_PATH);
        
if(mapfilepath == NULL || mapfilepath[0== 0 ){
            
return;
        }

        
if( FileExists(AnsiString(cppdllpath)+MAPANA3SNAME) && CheckTimeExWithMapFile(mapfilepath,exfilepath) ){
            AnsiString tst
="";
            
// •W€o—Í—pƒpƒCƒv
            HANDLE hReadPipe = NULL, hWritePipe = NULL;
            
// ƒGƒ‰[o—Í—pƒpƒCƒv
            HANDLE hErrReadPipe = NULL, hErrWritePipe = NULL;
            
// ƒZƒLƒ…ƒŠƒeƒB‘®«(ƒnƒ“ƒhƒ‹Œp³‚ðŽw’è)
            SECURITY_ATTRIBUTES sa;
            sa.nLength 
= sizeof(SECURITY_ATTRIBUTES);
            sa.lpSecurityDescriptor 
= NULL;
            sa.bInheritHandle 
= TRUE;

            
// •W€o—̓pƒCƒv‚̍쐬
            ::CreatePipe(&hReadPipe, &hWritePipe, &sa, 8192);
            
// ƒGƒ‰[o—̓pƒCƒv‚̍쐬
            ::CreatePipe(&hErrReadPipe, &hErrWritePipe, &sa, 8192);

            STARTUPINFO StartupInfo;
            PROCESS_INFORMATION ProcessInfo;
            ::ZeroMemory(
&StartupInfo, sizeof(STARTUPINFO));
            StartupInfo.cb 
= sizeof(STARTUPINFO);
            
// ƒnƒ“ƒhƒ‹‚ÌŒp³‚ðŽw’è
            StartupInfo.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
            
// DOS‘‹‚𕎦‚µ‚È‚¢
            StartupInfo.wShowWindow = SW_HIDE;//SW_HIDE;
            
// •W€o—̓nƒ“ƒhƒ‹‚ƃGƒ‰[o—̓nƒ“ƒhƒ‹‚ðÝ’è
            StartupInfo.hStdOutput = hWritePipe;
            StartupInfo.hStdError  
= hErrWritePipe;

            AnsiString str
="";
            str 
= str + """ + mapfilepath + """;
            str.Insert(AnsiString(cppdllpath)
+MAPANA3SNAME+" ",0);
            
// ƒRƒ“ƒ[ƒ‹ƒAƒvƒŠ‹N“®
            if (::CreateProcess(
                NULL,
                str.c_str(),
                
&sa,
                NULL,
                TRUE,    
// ƒnƒ“ƒhƒ‹‚ÌŒp³
                0,//DETACHED_PROCESS, // DOS‘‹‚𕎦‚µ‚È‚¢‚½‚߂̎w’è
                NULL,
                NULL,
                
&StartupInfo, &ProcessInfo))
            
{
                
// ƒpƒCƒv“à—eŽó‚¯Žæ‚è—pƒoƒbƒtƒ@
                char bufStdOut[8192], bufErrOut[8192];
                DWORD dwStdOut 
= 0, dwErrOut = 0;
                DWORD dwRet;

                
// ƒvƒƒZƒX‹N“®’†‚̃pƒCƒv“à—eŽó‚¯Žæ‚菈—
                while ( (dwRet = ::WaitForSingleObject(ProcessInfo.hProcess, 0)) !=
                    WAIT_ABANDONED)
                
{
                    memset(bufStdOut, 
0sizeof(bufStdOut));
                    memset(bufErrOut, 
0sizeof(bufErrOut));

                    
// •W€o—̓pƒCƒv‚Ì“à—e‚𒲂ׂé
                    ::PeekNamedPipe(hReadPipe, NULL, 0, NULL, &dwStdOut, NULL);
                    
if (dwStdOut > 0){
                         
// “à—e‚ª‘¶Ý‚·‚ê‚΁A“ǂݎæ‚é
                         ::ReadFile(hReadPipe, bufStdOut, sizeof(bufStdOut) - 1&dwStdOut,
                            NULL);
                        
// result_list->Add(bufStdOut);
                         tst += bufStdOut;
                         
// bufStdOut ‚ɃpƒCƒvo—Í‚ª“ü‚é
                    }

                    
// “¯—l‚ɃGƒ‰[o—͂̏ˆ—
                    ::PeekNamedPipe(hErrReadPipe, NULL, 0, NULL, &dwErrOut, NULL);
                    
if (dwErrOut > 0){
                         ::ReadFile(hErrReadPipe, bufErrOut, 
sizeof(bufErrOut) - 1&dwErrOut,
                            NULL);
                         
//result_list->Add(bufErrOut);
                         tst += bufErrOut;
                         
// bufErrOut ‚ɃGƒ‰[o—Í‚ª“ü‚é
                    }

                    
// ƒƒbƒZ[ƒWƒLƒ…[‚ðŽæ“¾‚µA‘¶Ý‚·‚ê‚΁Aˆ—‚𑣂·
                    MSG msg;
                    
if (::PeekMessage(&msg, NULL, 00, PM_REMOVE)){
                         ::TranslateMessage(
&msg);
                         ::DispatchMessage(
&msg);
                    }

                    
// ƒvƒƒZƒXI—¹‚È‚çAƒ‹[ƒv‚𔲂¯‚é
                    if (dwRet == WAIT_OBJECT_0)
                         
break;
                }

                
// ƒvƒƒZƒXƒnƒ“ƒhƒ‹‚ƃXƒŒƒbƒhƒnƒ“ƒhƒ‹‚ð•‚¶‚é
                DWORD res;
                ::GetExitCodeProcess(ProcessInfo.hProcess, 
&res);
                CloseHandle(ProcessInfo.hProcess);
                CloseHandle(ProcessInfo.hThread);
            }

            
// ‚·‚ׂẴpƒCƒv‚ð•‚¶‚é
            ::CloseHandle(hWritePipe);
            ::CloseHandle(hReadPipe);
            ::CloseHandle(hErrWritePipe);
            ::CloseHandle(hErrReadPipe);
            tst.Insert(
"0,",0);
            BuildSocket
->DFSendMessage(MSG_AddBuildMessage, tst.c_str());
        }

    }

    isMake
=false;
}

 

先贴上代码 心得以后再加。

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值