| View previous topic :: View next topic |
| Author |
Message |
Heartless I post too much
Reputation: 0
Joined: 03 Dec 2006 Posts: 2436
|
Posted: Sat Jan 26, 2008 1:58 pm Post subject: [C++]Handle is always 0 |
|
|
| Code: |
#include <iostream>
#include <windows.h>
using namespace std;
int MemVar1 = 1 ;
LONG MemAddress1 = 0x100579C ;
HWND hwnd;
HANDLE phandle;
DWORD pid;
void MemWrite()
{
hwnd = FindWindow( NULL,"C:\\Documents and Settings\\Computer\\Desktop\\C++ Projects\\App\\Debug\\App.exe");
if (hwnd != 0)
{
cout << "You caught the window!"<<endl;
pid = GetWindowThreadProcessId(hwnd, &pid);
phandle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);
}
if ( phandle != 0 )
cout << "phandle is not 0";
}
void main()
{
MemWrite();
}
|
Why is my phandle always 0? The PID is not 0, I don't see what the problem is.
_________________
What dosen't kill you, usually does the second time.
Last edited by Heartless on Sat Jan 26, 2008 2:03 pm; edited 1 time in total |
|
| Back to top |
|
 |
hcavolsdsadgadsg I'm a spammer
Reputation: 26
Joined: 11 Jun 2007 Posts: 5801
|
Posted: Sat Jan 26, 2008 2:00 pm Post subject: |
|
|
| You aren't finding a window with that FindWindow.
|
|
| Back to top |
|
 |
appalsap Moderator
Reputation: 0
Joined: 27 Apr 2006 Posts: 6753 Location: Pakistan
|
Posted: Sat Jan 26, 2008 2:00 pm Post subject: |
|
|
GetWindowThreadProcessId returns the process id in "pid", but you immediately overwrite "pid" with the return value of GetWindowThreadProcessId which returns the thread ID of the window which you cannot pass to OpenProcess.
| slovach wrote: | | You aren't finding a window with that FindWindow. |
That's what I thought too, at a first glance, but MSVC console projects by default (run in the IDE) run in a console window that has the full path to the executable as the title. Plus, since he reports phandle == 0 he already got a window handle.
|
|
| Back to top |
|
 |
Symbol I'm a spammer
Reputation: 0
Joined: 18 Apr 2007 Posts: 5094 Location: Israel.
|
Posted: Sat Jan 26, 2008 2:30 pm Post subject: |
|
|
Edit: Didn't see appalsap's post, so forget about the FindWindow.
You don't find the window, you find the path, and even if you will type App.exe its not the window name, its the process name, the window's name is the text in the title bar.
Also, GetWindowThreadProcessId(int hWnd, out int pID) sets the value of the Process ID of hWnd (the window with that handle) in pID (in - get, out - set) and returns another value, not sure what, but since it first sets pID to the process ID and only then returns the value, it overwrites pID with the return value.
|
|
| Back to top |
|
 |
Heartless I post too much
Reputation: 0
Joined: 03 Dec 2006 Posts: 2436
|
Posted: Mon Jan 28, 2008 4:39 pm Post subject: |
|
|
Here is my new code, but it still can't get the handle.
| Code: |
#include <windows.h>
#include <iostream>
using namespace std;
int main()
{
LONG Address = 0xDFB610C;
int myvalue2 = 1;
DWORD pId;
DWORD dwOldProtect = 0;
HANDLE pHandle;
HWND hWnd;
hWnd = FindWindow(0, "Minesweeper");
if (hWnd != 0)
{
cout << "Minesweeper was found" << endl;
GetWindowThreadProcessId(hWnd, &pId);
if ( pId != 0 )
cout << pId << endl;
else
cout << "pId was not found" << endl;
pHandle = OpenProcess(PROCESS_ALL_ACCESS, 0, pId);
if ( pHandle != 0 )
cout << "It worked for the first time" << endl;
else
{
cout << "It failed again" << endl;
cout << (DWORD)pHandle << endl;
cout << hWnd << endl;
}
}
else
cout<<"Minesweeper window could not be found.";
CloseHandle(pHandle);
} |
Here is my output
| Code: |
Minesweeper was found
404
It failed again
0
000500F2
Press any key to continue . . .
|
Why can't I get the handle to not be 0! This is pissing me off!
_________________
What dosen't kill you, usually does the second time. |
|
| Back to top |
|
 |
appalsap Moderator
Reputation: 0
Joined: 27 Apr 2006 Posts: 6753 Location: Pakistan
|
Posted: Mon Jan 28, 2008 4:45 pm Post subject: |
|
|
| HornyAZNBoy wrote: | | Why can't I get the handle to not be 0! This is pissing me off! |
Do you run around in circles every time an API fails? You're supposed to call GetLastError() and see what the operating system reports.
|
|
| Back to top |
|
 |
hcavolsdsadgadsg I'm a spammer
Reputation: 26
Joined: 11 Jun 2007 Posts: 5801
|
Posted: Mon Jan 28, 2008 5:10 pm Post subject: |
|
|
| Code: | #include <windows.h>
#include <iostream>
int main(void)
{
DWORD pid = 0;
HANDLE pHandle = NULL;
HWND hWnd = NULL;
hWnd = FindWindow("minesweeper", NULL);
if (!hWnd){
std::cout << "Didn't find squat." << std::endl;
}
else{
GetWindowThreadProcessId(hWnd, &pid);
pHandle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);
if (pHandle != NULL){
std::cout << "It worked for the first time" << std::endl;
}
}
CloseHandle(pHandle);
} |
Also GetLastError() is your friend.
e: took out kind of a redundant bit.
Last edited by hcavolsdsadgadsg on Mon Jan 28, 2008 5:12 pm; edited 1 time in total |
|
| Back to top |
|
 |
Flyte Peanuts!!!!
Reputation: 6
Joined: 19 Apr 2006 Posts: 1887 Location: Canada
|
Posted: Mon Jan 28, 2008 5:10 pm Post subject: |
|
|
| Code: | #include <windows.h>
#include <iostream>
int main()
{
DWORD pId; HWND hWnd; HANDLE hMine;
do {
hWnd = FindWindow(0, "Minesweeper");
Sleep(10);
} while(hWnd == NULL);
std::cout << "Minesweeper was found." << std::endl;
GetWindowThreadProcessId(hWnd, &pId);
if(pId != 0) {
hMine = OpenProcess(PROCESS_ALL_ACCESS, 0, pId);
if(hMine != NULL) {
std::cout << "Opened the process." << std::endl;
CloseHandle(hMine);
}
} else {
std::cout << "Failed to find pID." << std::endl;
}
} |
|
|
| Back to top |
|
 |
Heartless I post too much
Reputation: 0
Joined: 03 Dec 2006 Posts: 2436
|
Posted: Mon Jan 28, 2008 9:06 pm Post subject: |
|
|
| appalsap wrote: | | HornyAZNBoy wrote: | | Why can't I get the handle to not be 0! This is pissing me off! |
Do you run around in circles every time an API fails? You're supposed to call GetLastError() and see what the operating system reports. |
Yes I do. Then my mom comes in and tells me to shut up.
I tried using GetLastError() and all I keep getting is ERROR 5. I don't know hwat the fuck that means...
_________________
What dosen't kill you, usually does the second time. |
|
| Back to top |
|
 |
Renkokuken GO Moderator
Reputation: 4
Joined: 22 Oct 2006 Posts: 3249
|
Posted: Mon Jan 28, 2008 9:07 pm Post subject: |
|
|
| HornyAZNBoy wrote: | | appalsap wrote: | | HornyAZNBoy wrote: | | Why can't I get the handle to not be 0! This is pissing me off! |
Do you run around in circles every time an API fails? You're supposed to call GetLastError() and see what the operating system reports. |
Yes I do. Then my mom comes in and tells me to shut up.
I tried using GetLastError() and all I keep getting is ERROR 5. I don't know hwat the fuck that means... | Access is denied. Debug privileges are needed for that process.
|
|
| Back to top |
|
 |
Flyte Peanuts!!!!
Reputation: 6
Joined: 19 Apr 2006 Posts: 1887 Location: Canada
|
Posted: Mon Jan 28, 2008 9:24 pm Post subject: |
|
|
| Renkokuken wrote: | | Access is denied. Debug privileges are needed for that process. |
Taken from my dll injector:
| Code: | //main.h: AdjustTokenToDebug function
BOOL AdjustTokenToDebug()
{
//Define variables.
LUID tLUID; HANDLE hToken; TOKEN_PRIVILEGES tTP, tTPOld;
DWORD lengthReturned; BOOL ret = TRUE;
//Fill the tLUID struct with our privilage info.
if(LookupPrivilegeValue(NULL,SE_DEBUG_NAME,&tLUID)) {
//Open the token up.
if(OpenProcessToken(GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY,&hToken)) {
//Modify it so we become teh debuggzors.
tTP.PrivilegeCount=1;
tTP.Privileges->Attributes=SE_PRIVILEGE_ENABLED;
tTP.Privileges->Luid.HighPart=tLUID.HighPart;
tTP.Privileges->Luid.LowPart=tLUID.LowPart;
//Make the changes and check for errors.
if(!AdjustTokenPrivileges(hToken,0,&tTP,sizeof(tTP),&tTPOld,&lengthReturned))
ret = FALSE; //Bad
CloseHandle(hToken);
} else ret = FALSE; //Bad
} else ret = FALSE; //Bad
return ret;
} |
|
|
| Back to top |
|
 |
atom0s Moderator
Reputation: 205
Joined: 25 Jan 2006 Posts: 8587 Location: 127.0.0.1
|
Posted: Mon Jan 28, 2008 10:34 pm Post subject: |
|
|
| HornyAZNBoy wrote: | | appalsap wrote: | | HornyAZNBoy wrote: | | Why can't I get the handle to not be 0! This is pissing me off! |
Do you run around in circles every time an API fails? You're supposed to call GetLastError() and see what the operating system reports. |
Yes I do. Then my mom comes in and tells me to shut up.
I tried using GetLastError() and all I keep getting is ERROR 5. I don't know hwat the fuck that means... |
If you cared to look up GetLastError() you'd land up finding the MSDN entry about the API. They suggest combining this API with FormatMessage() to obtain the error description.
GetLastError
http://msdn2.microsoft.com/en-us/library/ms679360.aspx
| Quote: | | To obtain an error string for system error codes, use the FormatMessage function. For a complete list of error codes provided by the operating system, see System Error Codes. |
FormatMessage
http://msdn2.microsoft.com/en-us/library/ms679351(VS.85).aspx
_________________
- Retired. |
|
| Back to top |
|
 |
hcavolsdsadgadsg I'm a spammer
Reputation: 26
Joined: 11 Jun 2007 Posts: 5801
|
Posted: Mon Jan 28, 2008 10:37 pm Post subject: |
|
|
| HornyAZNBoy wrote: | | I tried using GetLastError() and all I keep getting is ERROR 5. I don't know hwat the fuck that means... |
MSDN MSDN MSDN
Did I mention that MSDN tends to be a rather good place to look things up when you're stuck?
e: argh wiccaan beat me, and had the audacity to actually provide a nice, informative message while he was at it.
|
|
| Back to top |
|
 |
|