본문 바로가기

C/C++

[winapi] Windows XP에서의 OpenProcess 에러

Windows XP에서의 OpenProcess 에러

 

 

OpenProcess()를 Windows 7 환경에서 컴파일한 후에, Windows XP에서 실행시켰더니,

Windows 7에서 잘 돌아가는 프로그램이 XP에서는 OpenProcess()에서 NULL을 리턴해 버리네요

 

구글링을 해보니, 아래와 같은 버그(?)가 있나 봅니다.

결론 먼저 쓰자면, 첫번째 파라미터로 넘겨주는 access 권한을 PROCESS_ALL_ACCESS로 넘기면 안됩니다.

 

 

msdn에서 OpenProcess의 프로토타입을 보면 아래와 같습니다.

HANDLE WINAPI OpenProcess(
  __in          DWORD dwDesiredAccess,
  __in          BOOL bInheritHandle,
  __in          DWORD dwProcessId
);

 

첫번째 파라미터인 dwDesiredAccess 는 프로세스 접근 권한을 넘겨줍니다.

그 값으로, PROCESS_VM_READ / PROCESS_VM_WRITE  / PROCESS_TERMINATE  등등 여러가지 값이 있으며,

모든 접근 권한이 가능한 PROCESS_ALL_ACCESS 값이 있어요.

 

[1]의 링크를 따라가서 보면 어느 누군가가 쓴 글이 있네요

Windows Server 2003 and Windows XP/2000:   The size of the PROCESS_ALL_ACCESS flag increased on Windows Server 2008 and Windows Vista. If an application compiled for Windows Server 2008 and Windows Vista is run on Windows Server 2003 or Windows XP/2000, the PROCESS_ALL_ACCESS flag is too large and the function specifying this flag fails with ERROR_ACCESS_DENIED. To avoid this problem, specify the minimum set of access rights required for the operation. If PROCESS_ALL_ACCESS must be used, set _WIN32_WINNT to the minimum operating system targeted by your application (for example,#define _WIN32_WINNT _WIN32_WINNT_WINXP). For more information, see Using the Windows Headers

 

PROCESS_ALL_ACCESS 플래그의 사이즈가 문제가 된다고 하네요

즉, 7에서 컴파일 된 PROCESS_ALL_ACCESS 사이즈가 XP환경에서는 너무 크다는 겁니다.

 

해결책은 PROCESS_ALL_ACCESS 로 하지 말고,

read권한이 필요하면 PROCESS_VM_READ, write권한이 필요하면 PROCESS_VM_WRITE , read/write 권한이 필요하면 PROCESS_VM_READ | PROCESS_VM_WRITE  이런식으로 쓰면 됩니다.

 

 

<출처>

1. http://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/eeb93be6-872c-4028-b0ae-cd873e089825