![]() |
| | #1 |
| HelpingYouHelpUsHelpUsAll Join Date: Dec 2007 Location: In your nightmares
Posts: 223
| Cast from hwnd Code: int getProcesses(HWND hwnd) {
HMODULE hModule;
char szProcessName[MAX_PATH] = {0};
DWORD dwProcesses[1024], cbNeeded, cProcesses;
unsigned int i;
char PIDbuf[128];
if (!EnumProcesses(dwProcesses, sizeof(dwProcesses), &cbNeeded))
return -1;
cProcesses = cbNeeded / sizeof(DWORD);
for (i = 0; i < cProcesses; i++)
if(dwProcesses[i] != 0)
{
HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION |
PROCESS_VM_READ, FALSE, dwProcesses[i]);
if (NULL != hProcess)
{
strcpy(szProcessName, "System");
if (EnumProcessModules(hProcess, &hModule, sizeof(hModule),
&cbNeeded))
{
GetModuleBaseName(hProcess, hModule, szProcessName,
sizeof(szProcessName)/sizeof(CHAR));
}
sprintf(PIDbuf, "%d : %s", (int)hProcess, szProcessName);
SendDlgItemMessage(hwnd, 1000, LB_ADDSTRING, 0, (LPARAM)PIDbuf);
}
CloseHandle(hProcess);
}
return cProcesses;
}
__________________ long time no C; //seige You miss 100% of the people you don't C; Code: if (language != LANG_C && language != LANG_CPP)
drown(language);
|
| P4R4N01D is offline | |
| | #2 |
| Rampaging 35 Stone Welsh Join Date: Apr 2007
Posts: 2,927
| Code: #include <windows.h>
#include <stdio.h>
#include <tchar.h>
#include <psapi.h>
void PrintProcessNameAndID( DWORD processID )
{
TCHAR szProcessName[MAX_PATH] = TEXT("<unknown>");
// Get a handle to the process.
HANDLE hProcess = OpenProcess( PROCESS_QUERY_INFORMATION |
PROCESS_VM_READ,
FALSE, processID );
// Get the process name.
if (NULL != hProcess )
{
HMODULE hMod;
DWORD cbNeeded;
if ( EnumProcessModules( hProcess, &hMod, sizeof(hMod),
&cbNeeded) )
{
GetModuleBaseName( hProcess, hMod, szProcessName,
sizeof(szProcessName)/sizeof(TCHAR) );
}
}
// Print the process name and identifier.
_tprintf( TEXT("%s (PID: %u)\n"), szProcessName, processID );
CloseHandle( hProcess );
}
void main( )
{
// Get the list of process identifiers.
DWORD aProcesses[1024], cbNeeded, cProcesses;
unsigned int i;
if ( !EnumProcesses( aProcesses, sizeof(aProcesses), &cbNeeded ) )
return;
// Calculate how many process identifiers were returned.
cProcesses = cbNeeded / sizeof(DWORD);
// Print the name and process identifier for each process.
for ( i = 0; i < cProcesses; i++ )
if( aProcesses[i] != 0 )
PrintProcessNameAndID( aProcesses[i] );
}
__________________ He is free, you say. Ah! That is his misfortune… These men… [have] the most terrible, the most imperious of masters, that is, need. … They must therefore find someone to hire them, or die of hunger. Is that to be free? - Simon Linguet |
| abachler is offline | |
| | #3 |
| HelpingYouHelpUsHelpUsAll Join Date: Dec 2007 Location: In your nightmares
Posts: 223
| Sorry I want to print out the Window handle, not the Process ID, Spy and Spy++ both do this, printing the window handle (hwnd) in hex.
__________________ long time no C; //seige You miss 100% of the people you don't C; Code: if (language != LANG_C && language != LANG_CPP)
drown(language);
|
| P4R4N01D is offline | |
| | #4 | |
| Mysterious C++ User Join Date: Oct 2007
Posts: 14,099
| abachler, void main? What's the reason for void main?
__________________ Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System I dedicated my life to helping others. This is only a small sample of what they said: "Thanks Elysia. You're a programming master! How the hell do you know every thing?" Quoted... at least once. Quote:
| |
| Elysia is offline | |
![]() |
| Tags |
| hwnd |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| How to fix misaligned assignment statements in the source code? | biggyK | C++ Programming | 28 | 07-16-2006 11:35 PM |
| Button handler | Nephiroth | Windows Programming | 8 | 03-12-2006 06:23 AM |
| My first "real" windows app | JoshR | Windows Programming | 2 | 07-28-2005 07:40 AM |
| opengl program as win API menu item | SAMSAM | Game Programming | 1 | 03-03-2003 07:48 PM |