title: VirtualAllocEx success return a base address,but then WriteProcess fails,and error is 487
body: <p>My code.</p>
  
  <pre><code>int main()
  {
  HANDLE hTargetHandle;
  int TargetProcessId=GetTargetProcessId();
  
  
  hTargetHandle=OpenProcess(PROCESS_ALL_ACCESS,FALSE,TargetProcessId);
  
  if(hTargetHandle==INVALID_HANDLE_VALUE)
  {
      DWORD d=GetLastError();
      printf("openprocess fail\n");
      printf("the TargetProcessId is: %d\n",TargetProcessId);
  
      printf("the result of getlast is: %d\n",d);
      system("PAUSE");
      exit(-1);
  }
  
  DWORD dwBufferSize=(DWORD)GetTargetProcessId-(DWORD)create;
  
  DWORD dwProcBaseAddress=(DWORD)VirtualAllocEx(hTargetHandle,NULL,1024*1024,MEM_COMMIT,PAGE_EXECUTE_READWRITE);     //the size here is 1024*1024,I don't know the size of the function I am going to inject to another process,so I use 1024*1024 here.I have ever tried to use 1024*1024*10,but it is the same error.
  
  if(dwProcBaseAddress==NULL)
  {
      DWORD d=GetLastError();
      printf("virtualallocex has fail\n");
      printf("the last error is:%d\n",d);
      system("PAUSE");
      exit(-1);
  }
  
  int a=WriteProcessMemory(hTargetHandle,&amp;dwProcBaseAddress,create,1024*1024,NULL);             //create is a function I defined,used to inject to another process.
  if(a==0)
  {
      DWORD d=GetLastError();
      printf("writeprocessmemory has fail\n");
      printf("the last error is %d\n",d);
      system("PAUSE");
      exit(-1);
  }
  
  DWORD dwThreadId;
  
  HANDLE hRemoteThreadHandle=CreateRemoteThread(hTargetHandle,NULL,NULL,(LPTHREAD_START_ROUTINE)dwProcBaseAddress,NULL,0,&amp;dwThreadId);
  
  if(hRemoteThreadHandle!=INVALID_HANDLE_VALUE)
  {
  
      printf("succeed\n");
      system("PAUSE");
      exit(-1);
  }
  
  
  system("PAUSE");
  return 1;
  }
  </code></pre>
  
  <p>Below is the result. <code>WriteProcessMemory</code> function fails,with the error 487,which means the address is invalid.</p>
  
  <p><img src="https://i.stack.imgur.com/1C6Aw.png" alt="Error message"></p>
  
  <p>But the address is the return value of <code>VirtualAllocEx</code> function,the return value is not null, so the address should be available.  I don't know where the problem is.</p>