Avatar billede riversen Nybegynder
02. april 2003 - 08:28 Der er 18 kommentarer og
1 løsning

Få fat i kørende processer i Windows

Hvordan får jeg fat i hvilke processer der på et givent tidspunkt kører i windows...evt. bare udskrivning af disse.

Der må gerne være kommentarer tilknyttet koden, og helst ikke for meget tågesnak ;-).
Avatar billede arne_v Ekspert
02. april 2003 - 08:31 #1
/*********************
EnumProc.h
*********************/

#include <windows.h>

typedef BOOL (CALLBACK *PROCENUMPROC)( DWORD, WORD, LPSTR, LPARAM ) ;

BOOL WINAPI EnumProcs( PROCENUMPROC lpProc, LPARAM lParam ) ;
Avatar billede arne_v Ekspert
02. april 2003 - 08:32 #2
/*********************
EnumProc.c (or .cpp)
*********************/

#include "EnumProc.h"
#include <tlhelp32.h>
#include <vdmdbg.h>

typedef struct
{
  DWORD          dwPID ;
  PROCENUMPROC  lpProc ;
  DWORD          lParam ;
  BOOL          bEnd ;
} EnumInfoStruct ;



BOOL WINAPI Enum16( DWORD dwThreadId, WORD hMod16, WORD hTask16,
                    PSZ pszModName, PSZ pszFileName, LPARAM lpUserDefined ) ;

// The EnumProcs function takes a pointer to a callback function
// that will be called once per process in the system providing
// process EXE filename and process ID.
// Callback function definition:
// BOOL CALLBACK Proc( DWORD dw, LPCSTR lpstr, LPARAM lParam ) ;
//
// lpProc -- Address of callback routine.
//
// lParam -- A user-defined LPARAM value to be passed to
//          the callback routine.

BOOL WINAPI EnumProcs( PROCENUMPROC lpProc, LPARAM lParam )
{
  OSVERSIONINFO  osver ;
  HINSTANCE      hInstLib ;
  HINSTANCE      hInstLib2 ;
  HANDLE        hSnapShot ;
  PROCESSENTRY32 procentry ;
  BOOL          bFlag ;
  LPDWORD        lpdwPIDs ;
  DWORD          dwSize, dwSize2, dwIndex ;
  HMODULE        hMod ;
  HANDLE        hProcess ;
  char          szFileName[ MAX_PATH ] ;
  EnumInfoStruct sInfo ;

  // ToolHelp Function Pointers.
  HANDLE (WINAPI *lpfCreateToolhelp32Snapshot)(DWORD,DWORD) ;
  BOOL (WINAPI *lpfProcess32First)(HANDLE,LPPROCESSENTRY32) ;
  BOOL (WINAPI *lpfProcess32Next)(HANDLE,LPPROCESSENTRY32) ;

  // PSAPI Function Pointers.
  BOOL (WINAPI *lpfEnumProcesses)( DWORD *, DWORD cb, DWORD * );
  BOOL (WINAPI *lpfEnumProcessModules)( HANDLE, HMODULE *, DWORD, LPDWORD );
  DWORD (WINAPI *lpfGetModuleFileNameEx)( HANDLE, HMODULE, LPTSTR, DWORD );

  // VDMDBG Function Pointers.
  INT (WINAPI *lpfVDMEnumTaskWOWEx)( DWORD, TASKENUMPROCEX  fp, LPARAM );

  // Check to see if were running under Windows95 or
  // Windows NT.
  osver.dwOSVersionInfoSize = sizeof( osver ) ;
  if( !GetVersionEx( &osver ) )
  {
      return FALSE ;
  }

  // If Windows NT:
  if( osver.dwPlatformId == VER_PLATFORM_WIN32_NT )
  {
      // Load library and get the procedures explicitly. We do
      // this so that we don't have to worry about modules using
      // this code failing to load under Windows 95, because
      // it can't resolve references to the PSAPI.DLL.
      hInstLib = LoadLibraryA( "PSAPI.DLL" ) ;
      if( hInstLib == NULL )
        return FALSE ;
      hInstLib2 = LoadLibraryA( "VDMDBG.DLL" ) ;
      if( hInstLib2 == NULL )
        return FALSE ;

      // Get procedure addresses.
      lpfEnumProcesses = (BOOL(WINAPI *)(DWORD *,DWORD,DWORD*))
      GetProcAddress( hInstLib, "EnumProcesses" ) ;
      lpfEnumProcessModules = (BOOL(WINAPI *)(HANDLE, HMODULE *, DWORD, LPDWORD))
                              GetProcAddress( hInstLib, "EnumProcessModules" ) ;
      lpfGetModuleFileNameEx =(DWORD (WINAPI *)(HANDLE, HMODULE, LPTSTR, DWORD ))
                              GetProcAddress( hInstLib, "GetModuleFileNameExA" ) ;
      lpfVDMEnumTaskWOWEx = (INT(WINAPI *)( DWORD, TASKENUMPROCEX, LPARAM))
                            GetProcAddress( hInstLib2, "VDMEnumTaskWOWEx" );
      if( lpfEnumProcesses == NULL ||
          lpfEnumProcessModules == NULL ||
          lpfGetModuleFileNameEx == NULL ||
          lpfVDMEnumTaskWOWEx == NULL)
      {
        FreeLibrary( hInstLib ) ;
        FreeLibrary( hInstLib2 ) ;
        return FALSE ;
      }

      // Call the PSAPI function EnumProcesses to get all of the
      // ProcID's currently in the system.
      // NOTE: In the documentation, the third parameter of
      // EnumProcesses is named cbNeeded, which implies that you
      // can call the function once to find out how much space to
      // allocate for a buffer and again to fill the buffer.
      // This is not the case. The cbNeeded parameter returns
      // the number of PIDs returned, so if your buffer size is
      // zero cbNeeded returns zero.
      // NOTE: The "HeapAlloc" loop here ensures that we
      // actually allocate a buffer large enough for all the
      // PIDs in the system.
      dwSize2 = 256 * sizeof( DWORD ) ;
      lpdwPIDs = NULL ;
      do
      {
        if( lpdwPIDs )
        {
            HeapFree( GetProcessHeap(), 0, lpdwPIDs ) ;
            dwSize2 *= 2 ;
        }
        lpdwPIDs = HeapAlloc( GetProcessHeap(), 0, dwSize2 );
        if( lpdwPIDs == NULL )
        {
            FreeLibrary( hInstLib ) ;
            FreeLibrary( hInstLib2 ) ;
            return FALSE ;
        }
        if( !lpfEnumProcesses( lpdwPIDs, dwSize2, &dwSize ) )
        {
            HeapFree( GetProcessHeap(), 0, lpdwPIDs ) ;
            FreeLibrary( hInstLib ) ;
            FreeLibrary( hInstLib2 ) ;
            return FALSE ;
        }
      } while( dwSize == dwSize2 ) ;

      // How many ProcID's did we get?
      dwSize /= sizeof( DWORD ) ;

      // Loop through each ProcID.
      for( dwIndex = 0 ; dwIndex < dwSize ; dwIndex++ )
      {
        szFileName[0] = 0 ;
        // Open the process (if we can... security does not
        // permit every process in the system).
        hProcess = OpenProcess( PROCESS_QUERY_INFORMATION | PROCESS_VM_READ,
                                FALSE, lpdwPIDs[ dwIndex ] ) ;
        if( hProcess != NULL )
        {
            // Here we call EnumProcessModules to get only the
            // first module in the process this is important,
            // because this will be the .EXE module for which we
            // will retrieve the full path name in a second.
            if( lpfEnumProcessModules( hProcess, &hMod, sizeof( hMod ), &dwSize2 ) )
            {
              // Get Full pathname:
              if( !lpfGetModuleFileNameEx( hProcess, hMod, szFileName, sizeof( szFileName ) ) )
              {
                  szFileName[0] = 0 ;
              }
            }
            CloseHandle( hProcess ) ;
        }

        // Regardless of OpenProcess success or failure, we
        // still call the enum func with the ProcID.
        if(!lpProc( lpdwPIDs[dwIndex], 0, szFileName, lParam))
            break ;

        // Did we just bump into an NTVDM?
        if( _stricmp( szFileName+(strlen(szFileName)-9), "NTVDM.EXE")==0)
        {
            // Fill in some info for the 16-bit enum proc.
            sInfo.dwPID = lpdwPIDs[dwIndex] ;
            sInfo.lpProc = lpProc ;
            sInfo.lParam = lParam ;
            sInfo.bEnd = FALSE ;
            // Enum the 16-bit stuff.
            lpfVDMEnumTaskWOWEx( lpdwPIDs[dwIndex], (TASKENUMPROCEX) Enum16, (LPARAM) &sInfo);

            // Did our main enum func say quit?
            if(sInfo.bEnd)
              break ;
        }
      }

      HeapFree( GetProcessHeap(), 0, lpdwPIDs ) ;
      FreeLibrary( hInstLib2 ) ;

  }
  // If Windows 95:
  else if( osver.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS )
  {
      hInstLib = LoadLibraryA( "Kernel32.DLL" ) ;
      if( hInstLib == NULL )
        return FALSE ;

      // Get procedure addresses.
      // We are linking to these functions of Kernel32
      // explicitly, because otherwise a module using
      // this code would fail to load under Windows NT,
      // which does not have the Toolhelp32
      // functions in the Kernel 32.
      lpfCreateToolhelp32Snapshot= (HANDLE(WINAPI *)(DWORD,DWORD))
                                  GetProcAddress( hInstLib, "CreateToolhelp32Snapshot" ) ;
      lpfProcess32First= (BOOL(WINAPI *)(HANDLE,LPPROCESSENTRY32))
                        GetProcAddress( hInstLib, "Process32First" ) ;
      lpfProcess32Next= (BOOL(WINAPI *)(HANDLE,LPPROCESSENTRY32))
                        GetProcAddress( hInstLib, "Process32Next" ) ;
      if( lpfProcess32Next == NULL ||
          lpfProcess32First == NULL ||
          lpfCreateToolhelp32Snapshot == NULL )
      {
        FreeLibrary( hInstLib ) ;
        return FALSE ;
      }

      // Get a handle to a Toolhelp snapshot of the systems
      // processes.
      hSnapShot = lpfCreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 ) ;
      if( hSnapShot == INVALID_HANDLE_VALUE )
      {
        FreeLibrary( hInstLib ) ;
        return FALSE ;
      }

      // Get the first process' information.
      procentry.dwSize = sizeof(PROCESSENTRY32) ;
      bFlag = lpfProcess32First( hSnapShot, &procentry ) ;

      // While there are processes, keep looping.
      while( bFlag )
      {
        // Call the enum func with the filename and ProcID.
        if(lpProc( procentry.th32ProcessID, 0, procentry.szExeFile, lParam ))
        {
          procentry.dwSize = sizeof(PROCESSENTRY32) ;
          bFlag = lpfProcess32Next( hSnapShot, &procentry );
        }
        else
          bFlag = FALSE ;
      }
  }
  else
      return FALSE ;

  // Free the library.
  FreeLibrary( hInstLib ) ;

  return TRUE ;
}

BOOL WINAPI Enum16( DWORD dwThreadId, WORD hMod16, WORD hTask16,
                    PSZ pszModName, PSZ pszFileName, LPARAM lpUserDefined )
{
  BOOL bRet ;

  EnumInfoStruct *psInfo = (EnumInfoStruct *)lpUserDefined ;

  bRet = psInfo->lpProc( psInfo->dwPID, hTask16, pszFileName, psInfo->lParam ) ;

  if(!bRet)
  {
      psInfo->bEnd = TRUE ;
  }

    return !bRet;

}
Avatar billede arne_v Ekspert
02. april 2003 - 08:32 #3
#include <stdio.h>

#include "EnumProc.h"

BOOL __stdcall Proc( DWORD dw, WORD alwaysZero, LPSTR lpstr, LPARAM lParam )
{
  printf( "%s\n", lpstr);
  return 1;
}

int main()
{
    EnumProcs( Proc, 0 );
    return 0;
}
Avatar billede arne_v Ekspert
02. april 2003 - 08:33 #4
De 2 første stumper er fundet på nettet (jeg kan godt finde en URL, hvis du vil).

Den sidste er hjemme-brygget.

Jeg formoder at du ike behøver kommentarer til den.
Avatar billede riversen Nybegynder
02. april 2003 - 08:41 #5
får denne fejl i vc++

enumproc.cpp(123) : error C2440: '=' : cannot convert from 'void *' to 'unsigned long *'
        Conversion from 'void*' to pointer to non-'void' requires an explicit cast
Avatar billede arne_v Ekspert
02. april 2003 - 08:45 #6
Sært - jeg kan compile fint med VC++ 6.0:

C:\>cl/c EnumProc.c
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 12.00.8804 for 80x86
Copyright (C) Microsoft Corp 1984-1998. All rights reserved.

EnumProc.c

C:\>
Avatar billede arne_v Ekspert
02. april 2003 - 08:47 #7
Linie 123 er:

lpdwPIDs = HeapAlloc( GetProcessHeap(), 0, dwSize2 );

gad vide om ikke den skal type castes til:

lpdwPIDs = (LPDWORD)HeapAlloc( GetProcessHeap(), 0, dwSize2 );
Avatar billede riversen Nybegynder
02. april 2003 - 08:52 #8
ja, det hjalp...

skal jeg oprette det som console application?
Avatar billede riversen Nybegynder
02. april 2003 - 08:56 #9
det kører nu :-)

kan du også fortælle, hvordan jeg får fat i memoryforbrug, antal tråde og cpu forbrug...
Avatar billede arne_v Ekspert
02. april 2003 - 08:59 #10
Min lille demo kode er en console app.

Så hvis du vil bruge den skal det være console app.

Men du kan da sagtens skrive en rigtig winn app som kalder
EnumProc på tilsvarende vis.
Avatar billede arne_v Ekspert
02. april 2003 - 09:01 #11
De oplysninger må også være tilgængelige.

Du har PID og så må du kunne slå de oplysninger op.

[jeg har aldrig prøvet]
Avatar billede riversen Nybegynder
02. april 2003 - 09:03 #12
PID? jeg er totalt noob her :-)
Avatar billede arne_v Ekspert
02. april 2003 - 09:08 #13
PID = Process ID

(første argumnet til callback funktionen Proc skulle indeholde
PID)
Avatar billede jpk Nybegynder
02. april 2003 - 09:59 #14
//Memory (Windows)
MEMORYSTATUS MS;
GlobalMemoryStatus(&MS);
m_AvailMem = MS.dwMemoryLoad;                    //percent of memory in use
m_PhysMem = MS.dwTotalPhys/1048576;                //Mbytes of physical memory
m_FreePhysMem = MS.dwAvailPhys/1048576;            //free physical memory Mbytes
m_PageFile = MS.dwTotalPageFile/1048576;        //Mbytes of paging file
m_FreePageFile = MS.dwAvailPageFile/1048576;    //free Mbytes of paging file
m_AdrSpace = MS.dwTotalVirtual/1048576;            //user Mbytes of address space
m_UserBytes = MS.dwAvailVirtual/1048576;        //free user Mbytes
Avatar billede arne_v Ekspert
02. april 2003 - 10:49 #15
Prøv evt. at kigge lidt på funktionerne her:
  http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/process_and_thread_functions.asp

Det ligner noget der kan bruges til at hente information
om de enkelte processer.
Avatar billede riversen Nybegynder
02. april 2003 - 14:14 #16
jpk: det var ment som memoryforbrug pr process
Avatar billede jakobdo Ekspert
18. december 2003 - 10:20 #17
Arne, den der __stdcall, er det noget specifikt VC++??
Når jeg compiler i Borland C++ Compiler, får jeg følgende fejl:

Error: Unresolved external '__stdcall EnumProcs(int __stdcall (*)(unsigned long,
unsigned short, char *, long), long)' referenced from C:\ENUMPROCS.OBJ
Avatar billede arne_v Ekspert
18. december 2003 - 10:53 #18
Nej.

Men fejlen antyder at du ikke får linket måd EnumProcs.obj (som er
oversat fra EnumProcs.c/cpp)
Avatar billede jakobdo Ekspert
18. december 2003 - 11:19 #19
/*********************
EnumProcs.cpp
*********************/

#include <stdio.h>

#include "EnumProc.h"

bool __stdcall Proc( DWORD dw, WORD alwaysZero, LPSTR lpstr, LPARAM lParam )
{
  printf( "%s\n", lpstr);
  return 1;
}

int main()
{
    EnumProcs( Proc, 0 );
    return 0;
}

/*********************
    EnumProc.h
*********************/
#include <windows.h>

typedef BOOL (CALLBACK *PROCENUMPROC)( DWORD, WORD, LPSTR, LPARAM ) ;

BOOL WINAPI EnumProcs( PROCENUMPROC lpProc, LPARAM lParam ) ;

/*********************
    EnumProc.c (or .cpp)
*********************/
#include "EnumProc.h"
#include <tlhelp32.h>
#include <vdmdbg.h>

typedef struct
{
    DWORD          dwPID ;
    PROCENUMPROC  lpProc ;
    DWORD          lParam ;
    BOOL          bEnd ;
} EnumInfoStruct ;

BOOL WINAPI Enum16( DWORD dwThreadId, WORD hMod16, WORD hTask16, PSZ pszModName, PSZ pszFileName, LPARAM lpUserDefined ) ;

// The EnumProcs function takes a pointer to a callback function
// that will be called once per process in the system providing
// process EXE filename and process ID.
// Callback function definition:
// BOOL CALLBACK Proc( DWORD dw, LPCSTR lpstr, LPARAM lParam ) ;
//
// lpProc -- Address of callback routine.
//
// lParam -- A user-defined LPARAM value to be passed to
//          the callback routine.
BOOL WINAPI EnumProcs( PROCENUMPROC lpProc, LPARAM lParam )
{
    OSVERSIONINFO  osver ;
    HINSTANCE      hInstLib ;
    HINSTANCE      hInstLib2 ;
    HANDLE        hSnapShot ;
    PROCESSENTRY32 procentry ;
    BOOL          bFlag ;
    LPDWORD        lpdwPIDs ;
    DWORD          dwSize, dwSize2, dwIndex ;
    HMODULE        hMod ;
    HANDLE        hProcess ;
    char          szFileName[ MAX_PATH ] ;
    EnumInfoStruct sInfo ;
   
    // ToolHelp Function Pointers.
    HANDLE (WINAPI *lpfCreateToolhelp32Snapshot)(DWORD,DWORD) ;
    BOOL (WINAPI *lpfProcess32First)(HANDLE,LPPROCESSENTRY32) ;
    BOOL (WINAPI *lpfProcess32Next)(HANDLE,LPPROCESSENTRY32) ;
   
    // PSAPI Function Pointers.
    BOOL (WINAPI *lpfEnumProcesses)( DWORD *, DWORD cb, DWORD * );
    BOOL (WINAPI *lpfEnumProcessModules)( HANDLE, HMODULE *, DWORD, LPDWORD );
    DWORD (WINAPI *lpfGetModuleFileNameEx)( HANDLE, HMODULE, LPTSTR, DWORD );
   
    // VDMDBG Function Pointers.
    INT (WINAPI *lpfVDMEnumTaskWOWEx)( DWORD, TASKENUMPROCEX  fp, LPARAM );
   
   
    // Check to see if were running under Windows95 or
    // Windows NT.
    osver.dwOSVersionInfoSize = sizeof( osver ) ;
    if( !GetVersionEx( &osver ) )
    {
        return FALSE ;
    }
   
    // If Windows NT:
    if( osver.dwPlatformId == VER_PLATFORM_WIN32_NT )
    {
   
        // Load library and get the procedures explicitly. We do
        // this so that we don't have to worry about modules using
        // this code failing to load under Windows 95, because
        // it can't resolve references to the PSAPI.DLL.
        hInstLib = LoadLibraryA( "PSAPI.DLL" ) ;
        if( hInstLib == NULL )
            return FALSE ;
       
        hInstLib2 = LoadLibraryA( "VDMDBG.DLL" ) ;
        if( hInstLib2 == NULL )
            return FALSE ;
       
        // Get procedure addresses.
        lpfEnumProcesses = (BOOL(WINAPI *)(DWORD *,DWORD,DWORD*)) GetProcAddress( hInstLib, "EnumProcesses" ) ;
        lpfEnumProcessModules = (BOOL(WINAPI *)(HANDLE, HMODULE *, DWORD, LPDWORD)) GetProcAddress( hInstLib, "EnumProcessModules" ) ;
        lpfGetModuleFileNameEx =(DWORD (WINAPI *)(HANDLE, HMODULE, LPTSTR, DWORD )) GetProcAddress( hInstLib, "GetModuleFileNameExA" ) ;
        lpfVDMEnumTaskWOWEx =(INT(WINAPI *)( DWORD, TASKENUMPROCEX, LPARAM))GetProcAddress( hInstLib2, "VDMEnumTaskWOWEx" );
        if( lpfEnumProcesses == NULL || lpfEnumProcessModules == NULL || lpfGetModuleFileNameEx == NULL || lpfVDMEnumTaskWOWEx == NULL)
        {
            FreeLibrary( hInstLib ) ;
            FreeLibrary( hInstLib2 ) ;
            return FALSE ;
        }
       
        // Call the PSAPI function EnumProcesses to get all of the
        // ProcID's currently in the system.
        // NOTE: In the documentation, the third parameter of
        // EnumProcesses is named cbNeeded, which implies that you
        // can call the function once to find out how much space to
        // allocate for a buffer and again to fill the buffer.
        // This is not the case. The cbNeeded parameter returns
        // the number of PIDs returned, so if your buffer size is
        // zero cbNeeded returns zero.
        // NOTE: The "HeapAlloc" loop here ensures that we
        // actually allocate a buffer large enough for all the
        // PIDs in the system.
        dwSize2 = 256 * sizeof( DWORD ) ;
        lpdwPIDs = NULL ;
        do
        {
            if( lpdwPIDs )
            {
                HeapFree( GetProcessHeap(), 0, lpdwPIDs ) ;
                dwSize2 *= 2 ;
            }
            lpdwPIDs = (LPDWORD)HeapAlloc( GetProcessHeap(), 0, dwSize2 );
            if( lpdwPIDs == NULL )
            {
                FreeLibrary( hInstLib ) ;
                FreeLibrary( hInstLib2 ) ;
                return FALSE ;
            }
            if( !lpfEnumProcesses( lpdwPIDs, dwSize2, &dwSize ) )
            {
                HeapFree( GetProcessHeap(), 0, lpdwPIDs ) ;
                FreeLibrary( hInstLib ) ;
                FreeLibrary( hInstLib2 ) ;
                return FALSE ;
            }
        }while( dwSize == dwSize2 ) ;
       
        // How many ProcID's did we get?
        dwSize /= sizeof( DWORD ) ;
       
        // Loop through each ProcID.
        for( dwIndex = 0 ; dwIndex < dwSize ; dwIndex++ )
        {
            szFileName[0] = 0 ;
            // Open the process (if we can... security does not
            // permit every process in the system).
            hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, lpdwPIDs[ dwIndex ] ) ;
            if( hProcess != NULL )
            {
                // Here we call EnumProcessModules to get only the
                // first module in the process this is important,
                // because this will be the .EXE module for which we
                // will retrieve the full path name in a second.
                if( lpfEnumProcessModules( hProcess, &hMod,
                  sizeof( hMod ), &dwSize2 ) )
                {
                    // Get Full pathname:
                    if( !lpfGetModuleFileNameEx( hProcess, hMod, szFileName, sizeof( szFileName ) ) )
                    {
                        szFileName[0] = 0 ;
                    }
                }
                CloseHandle( hProcess ) ;
            }
            // Regardless of OpenProcess success or failure, we
            // still call the enum func with the ProcID.
            if(!lpProc( lpdwPIDs[dwIndex], 0, szFileName, lParam))
                break ;
           
            // Did we just bump into an NTVDM?
            if( _stricmp( szFileName+(strlen(szFileName)-9), "NTVDM.EXE")==0)
            {
                // Fill in some info for the 16-bit enum proc.
                sInfo.dwPID = lpdwPIDs[dwIndex] ;
                sInfo.lpProc = lpProc ;
                sInfo.lParam = lParam ;
                sInfo.bEnd = FALSE ;
                // Enum the 16-bit stuff.
                lpfVDMEnumTaskWOWEx( lpdwPIDs[dwIndex], (TASKENUMPROCEX) Enum16, (LPARAM) &sInfo);
           
                // Did our main enum func say quit?
                if(sInfo.bEnd)
                    break ;
            }
        }
       
        HeapFree( GetProcessHeap(), 0, lpdwPIDs ) ;
        FreeLibrary( hInstLib2 ) ;
       
    // If Windows 95:
    }
    else if( osver.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS )
    {
        hInstLib = LoadLibraryA( "Kernel32.DLL" ) ;
        if( hInstLib == NULL )
            return FALSE ;
   
        // Get procedure addresses.
        // We are linking to these functions of Kernel32
        // explicitly, because otherwise a module using
        // this code would fail to load under Windows NT,
        // which does not have the Toolhelp32
        // functions in the Kernel 32.
        lpfCreateToolhelp32Snapshot = (HANDLE(WINAPI *)(DWORD,DWORD)) GetProcAddress( hInstLib, "CreateToolhelp32Snapshot" ) ;
        lpfProcess32First = (BOOL(WINAPI *)(HANDLE,LPPROCESSENTRY32)) GetProcAddress( hInstLib, "Process32First" ) ;
        lpfProcess32Next = (BOOL(WINAPI *)(HANDLE,LPPROCESSENTRY32)) GetProcAddress( hInstLib, "Process32Next" ) ;
        if( lpfProcess32Next == NULL || lpfProcess32First == NULL || lpfCreateToolhelp32Snapshot == NULL )
        {
            FreeLibrary( hInstLib ) ;
            return FALSE ;
        }
   
        // Get a handle to a Toolhelp snapshot of the systems
        // processes.
        hSnapShot = lpfCreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0 ) ;
        if( hSnapShot == INVALID_HANDLE_VALUE )
        {
            FreeLibrary( hInstLib ) ;
            return FALSE ;
        }
   
        // Get the first process' information.
        procentry.dwSize = sizeof(PROCESSENTRY32) ;
        bFlag = lpfProcess32First( hSnapShot, &procentry ) ;
   
        // While there are processes, keep looping.
        while( bFlag )
        {
            // Call the enum func with the filename and ProcID.
            if(lpProc( procentry.th32ProcessID, 0, procentry.szExeFile, lParam ))
            {
                procentry.dwSize = sizeof(PROCESSENTRY32) ;
                bFlag = lpfProcess32Next( hSnapShot, &procentry );
            }
            else
                bFlag = FALSE ;
        }
    }
    else
        return FALSE ;
   
    // Free the library.
    FreeLibrary( hInstLib ) ;
   
    return TRUE ;
}

BOOL WINAPI Enum16( DWORD dwThreadId, WORD hMod16, WORD hTask16, PSZ pszModName, PSZ pszFileName, LPARAM lpUserDefined )
{
    BOOL bRet ;
   
    EnumInfoStruct *psInfo = (EnumInfoStruct *)lpUserDefined ;
   
    bRet = psInfo->lpProc( psInfo->dwPID, hTask16, pszFileName, psInfo->lParam ) ;
   
    if(!bRet)
    {
        psInfo->bEnd = TRUE ;
    }
   
    return !bRet;
}

Jeg compiler på følgende måde:

C:\Tools>bcc32 enumprocs.cpp<br>
Borland C++ 5.6 for Win32 Copyright (c) 1993, 2002 Borland<br>
enumprocs.cpp:<br>
Warning W8057 enumprocs.cpp 9: Parameter 'dw' is never used in function __stdcall Proc(unsigned long,unsigned short,char *,long)<br>
Warning W8057 enumprocs.cpp 9: Parameter 'alwaysZero' is never used in function__stdcall Proc(unsigned long,unsigned short,char *,long)<br>
Warning W8057 enumprocs.cpp 9: Parameter 'lParam' is never used in function __stdcall Proc(unsigned long,unsigned short,char *,long)<br>
Error E2034 enumprocs.cpp 13: Cannot convert 'bool (__stdcall *)(unsigned long,unsigned short,char *,long)' to 'int (__stdcall *)(unsigned long,unsigned short,char *,long)' in function main()<br>
Error E2342 enumprocs.cpp 13: Type mismatch in parameter 'lpProc' (wanted 'int (__stdcall *)(unsigned long,unsigned short,char *,long)', got 'bool (__stdcall *)(unsigned long,unsigned short,char *,long)') in function main()<br>
*** 2 errors in Compile ***
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Kurser inden for grundlæggende programmering

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester