回 帖 发 新 帖 刷新版面

主题:GetCurrentProcessId 函数问题?求教?

///////////////////////////////////////////////////////////////
// 01FirstApp.cpp文件

#include "stdafx.h"    
#include <windows.h>    
#include <tlhelp32.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>


int main(int argc, char* argv[])
{
    // 调用API函数MessageBox
int nSelect = ::MessageBox(NULL, "Hello, Windows XP", "Greetings",   MB_OKCANCEL);
           PROCESSENTRY32 pe;
    pe.dwSize=sizeof(pe);
    
    HANDLE hCallerPro = ::CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,NULL);

    if(hCallerPro == INVALID_HANDLE_VALUE)
        return;
    //遍历进程,得到当前进程的PROCESSENTRY32结构

    BOOL bMore = ::Process32First(hCallerPro, &pe);
    while(bMore)
    {
    //用模块名称来鉴别是不是当前执行的进程
    if(strcmp(pe.szExeFile,"01FirstApp.exe"))
        {
            ::OutputDebugString("Get Window\n");
            //将GetGetCurrentProcessId得到的进程ID和这个ID比较
            //相同则程序结束
            if(pe.th32ProcessID == ::GetCurrentProcessId())
            {
            //为什么程序总是不能到达这个位置
            ::OutputDebugString("Id is equal\n");
                
            return;
            }
        }
        bMore = ::Process32Next(hCallerPro, &pe);
    }
    ::CloseHandle(hCallerPro);

    if(nSelect == IDOK)
        printf(" 用户选择了“确定”按钮 \n");
    else
        printf(" 用户选择了“取消”按钮 \n");
    return;
    
}
------------------------------------------------------------------------------------
我想知道为什么下面这两个进程ID总是不相等??
pe.th32ProcessID == ::GetCurrentProcessId()

回复列表 (共1个回复)

沙发

if(strcmp(pe.szExeFile,"01FirstApp.exe"))
应该改为
if(strcmp(pe.szExeFile,"01FirstApp.exe") == 0)
否则进入if的都是不为01FirstApp.exe的进程,proccessID怎么可能会相等呢?

我来回复

您尚未登录,请登录后再回复。点此登录或注册