我该如何解决? Hproc未在此范围内声明? [英] How do I fix. Hproc not declared in this scope?

查看:71
本文介绍了我该如何解决? Hproc未在此范围内声明?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include <iostream>
#include <windows.h>
#include "psapi.h"

using namespace std;
DWORD GetModuleBase(HANDLE hProc, string &sModuleName)
{
   HMODULE *hModules;
   char szBuf[50];
   DWORD cModules;
   DWORD dwBase = -1;
   //------

   EnumProcessModules(hProc, hModules, 0, &cModules);
   hModules = new HMODULE[cModules/sizeof(HMODULE)];

   if(EnumProcessModules(hProc, hModules, cModules/sizeof(HMODULE), &cModules)) {
      for(int i = 0; i < cModules/sizeof(HMODULE); i++) {
         if(GetModuleBaseName(hProc, hModules[i], szBuf, sizeof(szBuf))) {
            if(sModuleName.compare(szBuf) == 0) {
               dwBase = (DWORD)hModules[i];
               break;
            }
         }
      }
   }

   delete[] hModules;

   return dwBase;
}

int main()
{
    ---------------->>>>GetModuleBase(hProc, string("winmine.exe"));
}





我的尝试:





What I have tried:

#include <iostream>
#include <windows.h>
#include "psapi.h"

using namespace std;
DWORD GetModuleBase(HANDLE hProc, string &sModuleName)
{
   HMODULE *hModules;
   char szBuf[50];
   DWORD cModules;
   DWORD dwBase = -1;
   //------

   EnumProcessModules(hProc, hModules, 0, &cModules);
   hModules = new HMODULE[cModules/sizeof(HMODULE)];

   if(EnumProcessModules(hProc, hModules, cModules/sizeof(HMODULE), &cModules)) {
      for(int i = 0; i < cModules/sizeof(HMODULE); i++) {
         if(GetModuleBaseName(hProc, hModules[i], szBuf, sizeof(szBuf))) {
            if(sModuleName.compare(szBuf) == 0) {
               dwBase = (DWORD)hModules[i];
               break;
            }
         }
      }
   }

   delete[] hModules;

   return dwBase;
}

int main()
{
    -------------->>>>GetModuleBase(hProc, string("winmine.exe"));
}

推荐答案

那么,hProc在哪里宣布?我看到的唯一一个是GetModuleBase()的参数,它不在main的范围内。你需要在main中声明它才能在main中访问它。



还有一件事,为了调试我喜欢在看到时使用临时变量重复的表达。我也认为调用变量cModules会产生误导。它实际上是保存所有数据所需的内存量(以字节为单位)。我会这样做:
Well, where is hProc declared? The only one I see is the argument to GetModuleBase() and that is not in main's scope. You need to declare it within main for it to be accessible in main.

One more thing, for the sake of debugging I like to use a temporary variable when I see a repeated expression. I also think calling the variable cModules is misleading. It is actually the amount of memory in bytes required to hold all of the data. I would do the following :
DWORD memoryNeeded = 0;
EnumProcessModules( hProc, hModules, 0, &memoryNeeded );
DWORD moduleCount = memoryNeeded / sizeof( HMODULE );

然后我会在后面的代码中使用变量moduleCount。

and then I would use the variable moduleCount in the later code.


这篇关于我该如何解决? Hproc未在此范围内声明?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆