title: WinApi OpenProcess error 87
body: <p>Im trying to open process using Wisual Studio 2012, c++ console application and get 87 error:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;tchar.h&gt;
  #include &lt;Windows.h&gt;
  
  #define EXTERN_DLL_EXPORT extern "C" __declspec(dllexport)
  
  EXTERN_DLL_EXPORT int setLastObject(LPCWSTR wname) {
      LPCWSTR str = _TEXT("Name of window");
      HWND wnd = FindWindow(NULL,wname);
  
      LPDWORD cpid = 0;
      DWORD pid = GetWindowThreadProcessId(wnd,cpid);
  
      HANDLE proc = OpenProcess(PROCESS_ALL_ACCESS,false,pid);
      int err = GetLastError();
      return err;
  }
  </code></pre>
  
  <p>P.S. i get the pid correctly</p>