简单的CreateRemoteThread例程-初学者必看

  1. // _remotethreaddemo.cpp : Defines the entry point for the console application.
  2. // Author:秋镇菜
  3. #include "stdafx.h"
  4. #include "windows.h"
  5. // ========== 定义一个代码结构,本例为一个对话框============
  6. struct MyData
  7. {
  8.  char sz[64]; // 对话框显示内容
  9.  DWORD dwMessageBox; // 对话框的地址
  10. };
  11. // ========== 远程线程的函数 ==============================
  12. DWORD __stdcall RMTFunc(MyData *pData)
  13. {
  14.  typedef int(__stdcall*MMessageBox)(HWND,LPCTSTR,LPCTSTR,UINT);
  15.  MMessageBox MsgBox = (MMessageBox)pData->dwMessageBox;
  16.  MsgBox(NULL, pData->sz, NULL, MB_OK);
  17.  return 0;
  18. }
  19. int main(int argc, char* argv[])
  20. {
  21. // ===== 获得需要创建REMOTETHREAD的进程句柄 ===============================
  22.  HWND hWnd = FindWindow("notepad", NULL); // 以NOTEPAD为例
  23.  DWORD dwProcessId;
  24.  ::GetWindowThreadProcessId(hWnd, &dwProcessId);
  25.  HANDLE hProcess = OpenProcess(
  26.         PROCESS_ALL_ACCESS,
  27.         FALSE,
  28.         dwProcessId);
  29. // ========= 代码结构 ================================================
  30.  MyData data;
  31.  ZeroMemory(&data, sizeof (MyData));
  32.  strcat(data.sz, "对话框的内容.");
  33.  HINSTANCE hUser = LoadLibrary("user32.dll");
  34.  if (! hUser)
  35.  {
  36.   printf("Can not load library./n");
  37.   return 0;
  38.  }
  39.  data.dwMessageBox = (DWORD)GetProcAddress(hUser, "MessageBoxA");
  40.  FreeLibrary(hUser);
  41.  if (! data.dwMessageBox)
  42.   return 0;
  43. // ======= 分配空间 ===================================================
  44.  void *pRemoteThread
  45.   = VirtualAllocEx(hProcess, 0,
  46.       1024*4, MEM_COMMIT|MEM_RESERVE,
  47.       PAGE_EXECUTE_READWRITE);
  48.  if (! pRemoteThread)
  49.   return 0;
  50.  if (! WriteProcessMemory(hProcess, pRemoteThread, &RMTFunc, 1024*4, 0))
  51.   return 0;
  52.  MyData *pData
  53.   = (MyData*)VirtualAllocEx(hProcess, 0,
  54.       sizeof (MyData), MEM_COMMIT,
  55.       PAGE_READWRITE);
  56.  if (!pData)
  57.   return 0;
  58.  if (! WriteProcessMemory(hProcess, pData, &data, sizeof (MyData), 0))
  59.   return 0;
  60. // =========== 创建远程线程 ===========================================
  61.  HANDLE hThread
  62.   = CreateRemoteThread(hProcess, 0,
  63.        0, (LPTHREAD_START_ROUTINE)pRemoteThread,
  64.        pData, 0, 0);
  65.  if (! hThread)
  66.  {
  67.   printf("远程线程创建失败");
  68.   return 0;
  69.  }
  70.  CloseHandle(hThread);
  71.  VirtualFreeEx(hProcess, pRemoteThread, 1024*3, MEM_RELEASE);
  72.  VirtualFreeEx(hProcess, pData, sizeof (MyData), MEM_RELEASE);
  73.  CloseHandle(hProcess);
  74.  printf("Hello World!/n");
  75.  return 0;
  76. }

DEBUG版本可能会导致程序崩溃,

编译成RELEASE版本就不会出错了,主要是DEBUG版本加了一个__chkesp的函数导致调用了非法地址

 

http://www.cnblogs.com/i_like_cpp/archive/2005/06/02/166459.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值