Hi. How can i find how much windows are running at the same time? For example i need to find how much Calcultators are running. I tried using FindWindow() but it always returns me same handle. Any ideas?
Thank you

Hi here is a function that will do this for you

CString YOURCLASS::GetEXEName(DWORD dwProcessID)
{
    DWORD aProcesses [1024], cbNeeded, cProcesses;
    unsigned int i;

    //Enumerate all processes
    if (!EnumProcesses(aProcesses, sizeof(aProcesses), &cbNeeded))
        return NULL;

    // Calculate how many process identifiers were returned.
    cProcesses = cbNeeded / sizeof(DWORD);

    TCHAR szEXEName[MAX_PATH];
    //Loop through all process to find the one that matches
    //the one we are looking for
    for (i = 0; i < cProcesses; i++)
    {
        if (aProcesses [i] == dwProcessID)
        {
            // Get a handle to the process
            HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION |
                            PROCESS_VM_READ, FALSE, dwProcessID);

            // Get the process name
            if (NULL != hProcess)
            {
                HMODULE hMod;
                DWORD cbNeeded;

                if(EnumProcessModules(hProcess, &hMod, sizeof(hMod), &cbNeeded))
                {
                    //Get the name of the exe file
                    GetModuleBaseName(hProcess, hMod, szEXEName, 
                        sizeof(szEXEName)/sizeof(TCHAR));

                    return CString (szEXEName);
                }
            }
        }   
    }

    return NULL;
}

BOOL CALLBACK YOURCLASS::EnumWindowsProc (HWND hWnd, LPARAM lParam)
{
    YOURCLASS *pDlg = (YOURCLASS *)lParam;

    int nItem = 0;

    //Make sure that the window is visible
    TCHAR szWindowText [MAX_PATH];
    //if (!::IsWindowVisible (hWnd))
    //  return TRUE;

    //Get the text on the title bar
    ::GetWindowText (hWnd, szWindowText, MAX_PATH);

    //If the window is Process Manager than don't display it
    if (_tcsstr (_T("Program Manager"), szWindowText))
        return TRUE;

    //Get process ID
    DWORD dwProcessID;
    GetWindowThreadProcessId (hWnd, &dwProcessID);

    //Get the name of the executable file
    CString strEXEName = pDlg->GetEXEName (dwProcessID);

    //Add the info to the list control
    //nItem = pDlg->m_List.InsertItem (0, szWindowText);
    //pDlg->m_List.SetItemText (nItem, 1, strEXEName);  
    if(strEXEName==_T("caculater.exe"))
    {
      //Do what you need here as you now have the caculater.exe     
    }

    return TRUE;
}

Call the function like this EnumWindows (EnumWindowsProc, (LPARAM)this/*pointer to the dialog box*/);

in you header file put this

private:
    static BOOL CALLBACK EnumWindowsProc (HWND hwnd, LPARAM lParam);
    CString GetEXEName(DWORD dwProcessID);

I'm getting error
C2668: 'ATL::CStringT<BaseType,StringTraits>::CStringT' : ambiguous call to overloaded function
in return NULL Any ideas? Maybe i need to include diferent headers? Using mfc

OK. I changed

return NULL

to

return "\0"

i think its same? and now i recieve following errors:

error LNK2019: unresolved external symbol _GetModuleBaseNameA@16 referenced in function "private: class ATL::CStringT<char,class StrTraitMFC<char,class ATL::ChTraitsCRT<char> > > __thiscall CMyApp::GetEXEName(unsigned long)" (?GetEXEName@CMyApp@@AAE?AV?$CStringT@DV?$StrTraitMFC@DV?$ChTraitsCRT@D@ATL@@@@@ATL@@K@Z)

error LNK2019: unresolved external symbol _EnumProcessModules@16 referenced in function "private: class ATL::CStringT<char,class StrTraitMFC<char,class ATL::ChTraitsCRT<char> > > __thiscall CMyApp::GetEXEName(unsigned long)" (?GetEXEName@CMyApp@@AAE?AV?$CStringT@DV?$StrTraitMFC@DV?$ChTraitsCRT@D@ATL@@@@@ATL@@K@Z)

error LNK2019: unresolved external symbol _EnumProcesses@12 referenced in function "private: class ATL::CStringT<char,class StrTraitMFC<char,class ATL::ChTraitsCRT<char> > > __thiscall CMyApp::GetEXEName(unsigned long)" (?GetEXEName@CMyApp@@AAE?AV?$CStringT@DV?$StrTraitMFC@DV?$ChTraitsCRT@D@ATL@@@@@ATL@@K@Z)

fatal error LNK1120: 3 unresolved externals

...Any ideas?

P.S. I need to include psapi.h right? I already did that...

Hi sorry yes you need to add this #include "psapi.h"
#pragma comment(lib, "psapi.lib")

Should work fine after that also you shouldn't need to chage the return NULL either or add psapi.lib to you linker options what version of visual studio are you using

your error is a linking problem caused by not including the psapi.lib file

Visual Studio 2005

ok it will work fine with 2005 i have it working under 2010 make sure you have windows sdk installed also then it will definity compile for you

It's working :) thank you very much

if you comment out these two lines you will enumerate through every process that is running including system owned process and user owned process
//if (_tcsstr (_T("Program Manager"), szWindowText))
//return TRUE;

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.