IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Discussion :

Compiler du code venant de MSDN

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre averti
    Profil pro
    Inscrit en
    Mars 2009
    Messages
    15
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2009
    Messages : 15
    Par défaut Compiler du code venant de MSDN
    Bonjour, je souhaite utiliser l'API CreateToolhelp32Snapshot dans un programme Qt. Je compte utiliser cette API puisque je n'ai rien trouvé dans Qt pour lister les processus. Pour l'instant je me suis contenté de copier-coller le code de cette page : http://msdn.microsoft.com/en-us/libr...01(VS.85).aspx. J'utilise Qt Creator pour le compiler. Voilà le code que j'ai :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    #include <QtCore/QCoreApplication>
     
    #include <cstdlib>
    #include <iostream>
    #include <windows.h>
    #include <tlhelp32.h>
    #include <tchar.h>
    #include <stdio.h>
     
    //  Forward declarations:
    BOOL GetProcessList( );
    BOOL ListProcessModules( DWORD dwPID );
    BOOL ListProcessThreads( DWORD dwOwnerPID );
    void printError( TCHAR* msg );
     
     
     
    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
     
        GetProcessList();
        system("PAUSE");
     
        return a.exec();
    }
     
    BOOL GetProcessList( )
    {
      HANDLE hProcessSnap;
      HANDLE hProcess;
      PROCESSENTRY32 pe32;
      DWORD dwPriorityClass;
     
      // Take a snapshot of all processes in the system.
      hProcessSnap = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 );
      if( hProcessSnap == INVALID_HANDLE_VALUE )
      {
        printError( TEXT("CreateToolhelp32Snapshot (of processes)") );
        return( FALSE );
      }
     
      // Set the size of the structure before using it.
      pe32.dwSize = sizeof( PROCESSENTRY32 );
     
      // Retrieve information about the first process,
      // and exit if unsuccessful
      if( !Process32First( hProcessSnap, &pe32 ) )
      {
        printError( TEXT("Process32First") ); // show cause of failure
        CloseHandle( hProcessSnap );          // clean the snapshot object
        return( FALSE );
      }
     
      // Now walk the snapshot of processes, and
      // display information about each process in turn
      do
      {
        printf( "\n\n=====================================================" );
        _tprintf( TEXT("\nPROCESS NAME:  %s"), pe32.szExeFile );
        printf( "\n-----------------------------------------------------" );
     
        // Retrieve the priority class.
        dwPriorityClass = 0;
        hProcess = OpenProcess( PROCESS_ALL_ACCESS, FALSE, pe32.th32ProcessID );
        if( hProcess == NULL )
          printError( TEXT("OpenProcess") );
        else
        {
          dwPriorityClass = GetPriorityClass( hProcess );
          if( !dwPriorityClass )
            printError( TEXT("GetPriorityClass") );
          CloseHandle( hProcess );
        }
     
        printf( "\n  Process ID        = 0x%08X", pe32.th32ProcessID );
        printf( "\n  Thread count      = %d",   pe32.cntThreads );
        printf( "\n  Parent process ID = 0x%08X", pe32.th32ParentProcessID );
        printf( "\n  Priority base     = %d", pe32.pcPriClassBase );
        if( dwPriorityClass )
          printf( "\n  Priority class    = %d", dwPriorityClass );
     
        // List the modules and threads associated with this process
        ListProcessModules( pe32.th32ProcessID );
        ListProcessThreads( pe32.th32ProcessID );
     
      } while( Process32Next( hProcessSnap, &pe32 ) );
     
      CloseHandle( hProcessSnap );
      return( TRUE );
    }
     
     
    BOOL ListProcessModules( DWORD dwPID )
    {
      HANDLE hModuleSnap = INVALID_HANDLE_VALUE;
      MODULEENTRY32 me32;
     
      // Take a snapshot of all modules in the specified process.
      hModuleSnap = CreateToolhelp32Snapshot( TH32CS_SNAPMODULE, dwPID );
      if( hModuleSnap == INVALID_HANDLE_VALUE )
      {
        printError( TEXT("CreateToolhelp32Snapshot (of modules)") );
        return( FALSE );
      }
     
      // Set the size of the structure before using it.
      me32.dwSize = sizeof( MODULEENTRY32 );
     
      // Retrieve information about the first module,
      // and exit if unsuccessful
      if( !Module32First( hModuleSnap, &me32 ) )
      {
        printError( TEXT("Module32First") );  // show cause of failure
        CloseHandle( hModuleSnap );           // clean the snapshot object
        return( FALSE );
      }
     
      // Now walk the module list of the process,
      // and display information about each module
      do
      {
        _tprintf( TEXT("\n\n     MODULE NAME:     %s"),   me32.szModule );
        _tprintf( TEXT("\n     Executable     = %s"),     me32.szExePath );
        printf( "\n     Process ID     = 0x%08X",         me32.th32ProcessID );
        printf( "\n     Ref count (g)  = 0x%04X",     me32.GlblcntUsage );
        printf( "\n     Ref count (p)  = 0x%04X",     me32.ProccntUsage );
        printf( "\n     Base address   = 0x%08X", (DWORD) me32.modBaseAddr );
        printf( "\n     Base size      = %d",             me32.modBaseSize );
     
      } while( Module32Next( hModuleSnap, &me32 ) );
     
      CloseHandle( hModuleSnap );
      return( TRUE );
    }
     
    BOOL ListProcessThreads( DWORD dwOwnerPID )
    {
      HANDLE hThreadSnap = INVALID_HANDLE_VALUE;
      THREADENTRY32 te32;
     
      // Take a snapshot of all running threads
      hThreadSnap = CreateToolhelp32Snapshot( TH32CS_SNAPTHREAD, 0 );
      if( hThreadSnap == INVALID_HANDLE_VALUE )
        return( FALSE );
     
      // Fill in the size of the structure before using it.
      te32.dwSize = sizeof(THREADENTRY32);
     
      // Retrieve information about the first thread,
      // and exit if unsuccessful
      if( !Thread32First( hThreadSnap, &te32 ) )
      {
        printError( TEXT("Thread32First") ); // show cause of failure
        CloseHandle( hThreadSnap );          // clean the snapshot object
        return( FALSE );
      }
     
      // Now walk the thread list of the system,
      // and display information about each thread
      // associated with the specified process
      do
      {
        if( te32.th32OwnerProcessID == dwOwnerPID )
        {
          printf( "\n\n     THREAD ID      = 0x%08X", te32.th32ThreadID );
          printf( "\n     Base priority  = %d", te32.tpBasePri );
          printf( "\n     Delta priority = %d", te32.tpDeltaPri );
        }
      } while( Thread32Next(hThreadSnap, &te32 ) );
     
      CloseHandle( hThreadSnap );
      return( TRUE );
    }
     
    void printError( TCHAR* msg )
    {
      DWORD eNum;
      TCHAR sysMsg[256];
      TCHAR* p;
     
      eNum = GetLastError( );
      FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
             NULL, eNum,
             MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
             sysMsg, 256, NULL );
     
      // Trim the end of the line and terminate it with a null
      p = sysMsg;
      while( ( *p > 31 ) || ( *p == 9 ) )
        ++p;
      do { *p-- = 0; } while( ( p >= sysMsg ) &&
                              ( ( *p == '.' ) || ( *p < 33 ) ) );
     
      // Display the message
      _tprintf( TEXT("\n  WARNING: %s failed with error %d (%s)"), msg, eNum, sysMsg );
    }
    Le problème est que je n'arrive à compiler avec Qt creator, mais j'arrive pourtant à le compiler avec Dev-C++. J'ai ce genre d'erreurs
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    C:/Documents and Settings/***/Mes documents/C++/console/main.cpp:39: error: cannot convert `const char*' to `TCHAR*' for argument `1' to `void printError(TCHAR*)'
    Je comprends bien qu'il n'arrive pas à faire la conversion, mais alors, pourquoi ça marche quand je le compile avec Dev-C++ et pas avec Qt Creator ??! C'est bizarre non ? ou c'est normal ?

    (J'ai Qt Creator 1.2.1 et Qt 4.5.2.)

  2. #2
    Inactif  
    Avatar de Mac LAK
    Profil pro
    Inscrit en
    Octobre 2004
    Messages
    3 893
    Détails du profil
    Informations personnelles :
    Âge : 51
    Localisation : France, Haute Garonne (Midi Pyrénées)

    Informations forums :
    Inscription : Octobre 2004
    Messages : 3 893
    Par défaut
    Tu as essayé en modifiant l'ordre des #include de ton programme, notamment en mettant en premier <windows.h> et <tchar.h> ?
    Essaie aussi de vérifier si la macro "_UNICODE" est définie ou pas.
    Mac LAK.
    ___________________________________________________
    Ne prenez pas la vie trop au sérieux, de toutes façons, vous n'en sortirez pas vivant.

    Sources et composants Delphi sur mon site, L'antre du Lak.
    Pas de question technique par MP : posez-la dans un nouveau sujet, sur le forum adéquat.

    Rejoignez-nous sur : Serveur de fichiers [NAS] Le Tableau de bord projets Le groupe de travail ICMO

  3. #3
    Membre averti
    Profil pro
    Inscrit en
    Mars 2009
    Messages
    15
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2009
    Messages : 15
    Par défaut
    J'ai essayé de changer l'ordre des #includes, mais ça n'a rien changé.
    Concernant la macro _UNICODE, je ne sais pas si j'ai bien fait pour tester son existance, j'ai écris dans le main() : _UNICODE;. Mais en faisant comme ça, ça me dit /ListeProcessus/main.cpp:16: error: `_UNICODE' was not declared in this scope. J'en conclu qu'elle n'est pas déclarée.

Discussions similaires

  1. le code tiré de MSDN ne compile pas
    Par oliv23 dans le forum VC++ .NET
    Réponses: 5
    Dernier message: 10/11/2006, 11h59
  2. Compiler du code Java via Java
    Par sozie9372 dans le forum Langage
    Réponses: 2
    Dernier message: 12/01/2006, 10h14
  3. Probleme en compilant le code avec C2.EXE ?!?!?
    Par angelevil dans le forum VB 6 et antérieur
    Réponses: 4
    Dernier message: 17/11/2005, 16h13
  4. [Servlet][Compilation] Erreur de compil dans code servlet
    Par gandalf_le_blanc dans le forum Servlets/JSP
    Réponses: 3
    Dernier message: 13/05/2004, 10h17
  5. Compiler un code sous windows
    Par KORTA dans le forum Choisir un environnement de développement
    Réponses: 2
    Dernier message: 30/09/2003, 16h53

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo