首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ Builder >

关于 VirtualAlloc 与 SendMessage 在外部窗口ListView 控件的应用的有关问题,附代码

2012-02-05 
关于VirtualAlloc 与 SendMessage 在外部窗口ListView 控件的应用的问题,附代码代码如下 :#includewindow

关于 VirtualAlloc 与 SendMessage 在外部窗口ListView 控件的应用的问题,附代码
代码如下 :
#include<windows.h>
#include<commctrl.h>
#include<iostream.h>
HWND MyFindWindow()
{
  const int MyMaxParentWinCount = 3;0
  char *A_szClassName[MyMaxParentWinCount] =
  {
  "CabinetWClass",
  "SHELLDLL_DefView",
  "SysListView32"
  };
  char *A_szWinName[MyMaxParentWinCount] =
  {
  "我的电脑",
  "",
  "FolderView"
  };
  HWND hLastWin = FindWindow(A_szClassName[0], A_szWinName[0]);
  for(int i=1; i<MyMaxParentWinCount; i++)
  {
  hLastWin = FindWindowEx(hLastWin, NULL,
  A_szClassName[i], A_szWinName[i]);
  }
  return hLastWin;
}
// 举例: HWND hLastWin = MyFindWindow();

int Main()
{
  HWND hwnd;
  int iItem=0;
  LVITEM lvitem, *plvitem;
  char ItemBuf[512],*pItem;
  DWORD PID;
  HANDLE hProcess;
  RECT rc1,*Prc1;
  char ItemBuf1[4],ItemBuf2[4],ItemBuf3[4],ItemBuf[4];

  /*
  hwnd=FindWindow("#32770","Windows 任务管理器");
  hwnd=FindWindowEx(hwnd,0,"#32770",0);
  hwnd=FindWindowEx(hwnd,0,"SysListView32",0);
  */
  hwnd = MyFindWindow();
  if (!hwnd)
  {
  MessageBox(NULL,"[Windows 任务管理器] 尚未启动!","错误!",NULL);
  }
  Else
  {

  //itoa(iItem,temp,10);
  //MessageBox(NULL,temp,"iItem",NULL);
  GetWindowThreadProcessId(hwnd, &PID);
  hProcess=OpenProcess(PROCESS_ALL_ACCESS,false,PID);
   
  if (!hProcess)
  {
  MessageBox(NULL,"获取进程句柄操作失败!","错误!",NULL);
  }
  Else
  {
  Prc1 = (RECT*)VirtualAllocEx(hProcess, NULL, sizeof(RECT), MEM_COMMIT, PAGE_READWRITE);
   
  //plvitem=(LVITEM*)VirtualAllocEx(hProcess, NULL, sizeof(LVITEM), MEM_COMMIT, PAGE_READWRITE);
  //pItem=(char*)VirtualAllocEx(hProcess, NULL, 512, MEM_COMMIT, PAGE_READWRITE);
  if ((!Prc1))
  {
  MessageBox(NULL,"无法分配内存!","错误!",NULL);
  }
  Else
  {
  /*
  lvitem.cchTextMax=512;
  lvitem.iSubItem=0; //ProcessName
  lvitem.pszText=pItem;
  */
   
  WriteProcessMemory(hProcess, Prc1, &rc1, sizeof(RECT), NULL);
  LRESULT lr1 = SendMessage(hwnd, LVM_GETITEMRECT, &0, (LPARAM)Prc1);
cout<<"是否成功:"<<lr1<<endl;//这里返回1代表成功 

  ReadProcessMemory(hProcess, Prc1, ItemBuf1, sizeof(long), NULL);
cout<<ItemBuf1<<endl;
  ReadProcessMemory(hProcess, Prc1 + sizeof(long) * 1 , ItemBuf2, sizeof(long), NULL);
cout<<ItemBuf2<<endl;
  ReadProcessMemory(hProcess, Prc1 + sizeof(long) * 2 , ItemBuf3, sizeof(long),
NULL);  
cout<<ItemBuf3<<endl
  ReadProcessMemory(hProcess, Prc1 + sizeof(long) * 3 , ItemBuf4, sizeof(long), NULL);
cout<<ItemBuf4<<endl;

cout<<"Top:"<<rc1.Top<<endl;


cout<<"Bottom:"<<rc1.Bottom<<endl;
cout<<"Left:"<<rc1.Left<<endl;
cout<<"Right:"<<rc1.Right<<endl;
cout<<"高为:"<<rc1.Bottom - rc1.Top<<endl;
cout<<"宽为:"<<rc1.Right - rc1.Right<<endl;
  }
  }
   
  }
  //释放内存
  VirtualFreeEx(hProcess, plvitem, 0, MEM_RELEASE);
  VirtualFreeEx(hProcess, pItem, 0, MEM_RELEASE);
  CloseHandle(hwnd);
  CloseHandle(hProcess);
  return 0;
}

//以下这段代是为了获取我的电脑窗体中,ListView 的第一个元素的 RECT 结构体 
//目标是为了从元素的高与宽 得知窗体的查看方式(缩略图,平铺,图标,列表,详细信息)
//这段代码是在记事本里面写的,可能会有错误,但是流程大概是这样,现在我遇到的问题是,SendMessage成功,
//但是返回的RECT结构体 读不到值,或者不知道怎么把值读出来, 请各位帮助一下
//或者大家有什么其他的方法得到我的电脑的查看方式,也可以贴出来

[解决办法]
你这么做其实是想从explorer。exe的内存空间获取数据。你用PROCESS_ALL_ACCESS这个参数OpenProcess是成功的。
我觉得你这种方法获取查看方式是错误的,看看注册表有没有相应的键值吧

热点排行