获取进程的地址,奇怪的错误

时间:2017-01-16 14:32:28

标签: c++

我试图将我的应用程序连接到另一个应用程序(一个游戏,4Story)。但我得到一个错误:

  

在查找预编译时,错误C1010意外结束文件   头。您是否忘了添加' #include" stdafx.h"'你的来源?

如果我添加stdafx.h,我将得到104错误,如

  

错误C3861' CloseHandle':未找到标识符

     

错误C2039' cout':不是' std'的成员...

#define _CRT_SECURE_NO_WARNINGS
#define UNINITIALIZED 0xFFFFFFFF
#include <iostream>
#include <iomanip>
#include <Windows.h>
#include <TlHelp32.h> //PROCESSENTRY

/* The name of the process */
const char* processName_ = "TClient.exe";

int main() {
    DWORD  processID_ = 0;
    DWORD  processBaseAddress_ = UNINITIALIZED;

    /* Get the process ID  */
    {
        PROCESSENTRY32 processEntry_; // Entry into process you wish to inject to
        HANDLE hProcSnapshot_ = NULL;
        /* Takes a snapshot of the system's processes */
        hProcSnapshot_ = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); //?

    /* While process has not been found, keep looking for it */
        while (!processID_)
        {
            /* If a process on the system exists */
            if (Process32First(hProcSnapshot_, &processEntry_)) //?
            {
                /* Check all processes in the system's processes snapshot */
                do
                {
                    std::wstring s(processEntry_.szExeFile);
                    const WCHAR * ProcessName_ = s.c_str();

                    /* Compare the name of the process to the one we want */
                    if (!wcscmp( processEntry_.szExeFile , ProcessName_))
                    //if (!strcmp(processEntry_.szExeFile, processName_)) //?
                    {
                        /* Save the processID and break out */
                        processID_ = processEntry_.th32ProcessID;
                        break;
                    }
                } while (Process32Next(hProcSnapshot_, &processEntry_));
            }

            /* Didnt find process, sleep for a bit */
            if (!processID_)
            {
                system("CLS");
                std::cout << "Make sure " << processName_ << " is running." << std::endl;
                Sleep(200);
            }
        }

        /* Process found */
        std::cout << "Found Process: " << processName_ << std::endl;
    }


    /* Find Base Address of process */
    {
        HANDLE moduleSnapshotHandle_ = INVALID_HANDLE_VALUE;
        MODULEENTRY32 moduleEntry_;

        /* Take snapshot of all the modules in the process */
        moduleSnapshotHandle_ = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, processID_);

        /* Snapshot failed */
        if (moduleSnapshotHandle_ == INVALID_HANDLE_VALUE)
        {
            std::cout << "Module Snapshot error" << std::endl;
            return 0;
        }

        /* Size the structure before usage */
        moduleEntry_.dwSize = sizeof(MODULEENTRY32);

        /* Retrieve information about the first module */
        if (!Module32First(moduleSnapshotHandle_, &moduleEntry_))
        {
            std::cout << "First module not found" << std::endl;
            CloseHandle(moduleSnapshotHandle_);
            return 0;
        }

        /* Find base address */
        while (processBaseAddress_ == UNINITIALIZED)
        {
            /* Find module of the executable */
            do
            {
                std::wstring s(moduleEntry_.szModule );
                const WCHAR * ProcessName_ = s.c_str();

                /* Compare the name of the process to the one we want */
                if (!wcscmp(moduleEntry_.szModule, ProcessName_)) //?
                {
                    /* Save the processID and break out */
                    processBaseAddress_ = (unsigned int)moduleEntry_.modBaseAddr;
                    break;
                }

            } while (Module32Next(moduleSnapshotHandle_, &moduleEntry_));


            if (processBaseAddress_ == UNINITIALIZED)
            {
                system("CLS");
                std::cout << "Failed to find module" << processName_ << std::endl;
                Sleep(200);
            }
        }

        /* Found module and base address successfully */
        std::cout << "Base Address: " << std::hex << processBaseAddress_ << std::dec << std::endl;
        CloseHandle(moduleSnapshotHandle_);
    }
}

0 个答案:

没有答案