C Board  

Go Back   C Board > Platform Specific Boards > Windows Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 05-10-2008, 08:32 PM   #1
HelpingYouHelpUsHelpUsAll
 
Join Date: Dec 2007
Location: In your nightmares
Posts: 223
Cast from hwnd

I want to loop trough all the windows running and get their hwnd and convert it to an int. Here is the function I have that attempts to do this (very similar to code i have previously posted):
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;
}
The problem is that all the hwnds are the same (1952) and using hModule doesn't work either. I am pretty sure that hProcess is the hwnd. All the process names work fine and the ListView works.
__________________
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   Reply With Quote
Old 05-10-2008, 10:06 PM   #2
Rampaging 35 Stone Welsh
 
abachler's Avatar
 
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   Reply With Quote
Old 05-10-2008, 11:07 PM   #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   Reply With Quote
Old 05-11-2008, 01:46 AM   #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:
Originally Posted by cpjust
If C++ is 2 steps forward from C, then I'd say Java is 1 step forward and 2 steps back.
Elysia is offline   Reply With Quote
Reply

Tags
hwnd

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 05:56 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22