2011-10-07 3 views
0

J'essaie d'ouvrir un processus avec mon débogueur en utilisant CreateProcess avec les drapeaux DEBUG_PROCESS et DEBUG_ONLY_THIS_PROCESS et le processus est ouvert, mais quand j'essaie d'appeler SymInitialize avec le handle que je reçois , il échoue.Problèmes avec l'ouverture d'un processus avec des drapeaux DEBUG

Ceci est mon code:

#include <windows.h> 
#include <stdio.h> 
#include <dbghelp.h> 
#pragma (lib, "dbghelp.lib"); 

bool EnablePrivilege(LPCTSTR lpszPrivilegeName, BOOL bEnable) 
{ 
    HANDLE hToken; 
    TOKEN_PRIVILEGES tp; 
    LUID luid; 
    bool ret; 

    if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY | TOKEN_READ, &hToken)) 
     return FALSE; 

    if (!LookupPrivilegeValue(NULL, lpszPrivilegeName, &luid)) 
     return FALSE; 

    tp.PrivilegeCount   = 1; 
    tp.Privileges[0].Luid  = luid; 
    tp.Privileges[0].Attributes = bEnable ? SE_PRIVILEGE_ENABLED : 0; 

    ret = AdjustTokenPrivileges(hToken, FALSE, &tp, sizeof(tp), NULL, NULL); 
    CloseHandle(hToken); 

    return ret; 
} 

void main() 
{ 
    EnablePrivilege(SE_DEBUG_NAME, TRUE); 

    STARTUPINFOA startInfo; 
    PROCESS_INFORMATION processInfo; 
    ZeroMemory(&startInfo, sizeof(startInfo)); 
    startInfo.cb = sizeof(startInfo); 
    ZeroMemory(&processInfo, sizeof(processInfo)); 
    DWORD creationFlags = DEBUG_PROCESS | DEBUG_ONLY_THIS_PROCESS | PROCESS_VM_READ | PROCESS_QUERY_INFORMATION; 
    const char* comLine = "Some process path and name"; 

//  Start the child process. 
    if(CreateProcessA(NULL, // No module name (use command line) 
     (LPSTR)comLine, //argv[1],  // Command line 
     NULL,   // Process handle not inheritable 
     NULL,   // Thread handle not inheritable 
     FALSE,   // Set handle inheritance to FALSE 
     creationFlags,    // No creation flags 
     NULL,   // Use parent's environment block 
     NULL,   // Use parent's starting directory 
     &startInfo,   // Pointer to STARTUPINFO structure 
     &processInfo)   // Pointer to PROCESS_INFORMATION structure 
    == false) 
    { 
     printf("FAIL!"); 
return; 
    } 

    SetLastError(0); 
    bool ok = SymInitialize(processInfo.hProcess, NULL, true); 
    int err = GetLastError(); 

} 

Si j'appelle CreateProcess sans drapeaux de création, réussir SymInitialize.
Qu'est-ce que je fais de mal?

+0

J'ai également essayé de supprimer les indicateurs: PROCESS_VM_READ et PROCESS_QUERY_INFORMATION mais cela n'a pas aidé. – Idov

+0

Lorsque l'appel à SymInitialize échoue, que GetLastError() renvoie-t-il? –

+0

Il renvoie une valeur négative de la poubelle:/ – Idov

Répondre

0

Votre erreur est résultat de l'appel MAKE_HRESULT macro comme

MAKE_HRESULT(ERROR_SEVERITY_ERROR, FACILITY_NULL, ERROR_INVALID_DATA) 

donc votre code d'erreur est pas trash. Documentaion ne dit pas quel type de données pourrait être invalide dans ce contexte. Je vais essayer de voir ce qui cause exactement le problème.

EDIT:

Ce code fonctionne pour moi

#include <windows.h> 
#include <stdio.h> 
#include <dbghelp.h> 
#include <WinError.h> 

bool EnablePrivilege(LPCTSTR lpszPrivilegeName, BOOL bEnable) 
{ 
    HANDLE hToken; 
    TOKEN_PRIVILEGES tp; 
    LUID luid; 
    bool ret; 

    if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY | TOKEN_READ, &hToken)) 
    return FALSE; 

    if (!LookupPrivilegeValue(NULL, lpszPrivilegeName, &luid)) 
    return FALSE; 

    tp.PrivilegeCount   = 1; 
    tp.Privileges[0].Luid  = luid; 
    tp.Privileges[0].Attributes = bEnable ? SE_PRIVILEGE_ENABLED : 0; 

    ret = AdjustTokenPrivileges(hToken, FALSE, &tp, sizeof(tp), NULL, NULL); 
    CloseHandle(hToken); 

    return ret; 
} 

void main() 
{ 
    EnablePrivilege(SE_DEBUG_NAME, TRUE); 

    STARTUPINFOA startInfo; 
    PROCESS_INFORMATION processInfo; 
    ZeroMemory(&startInfo, sizeof(startInfo)); 
    startInfo.cb = sizeof(startInfo); 
    ZeroMemory(&processInfo, sizeof(processInfo)); 
    DWORD creationFlags = DEBUG_PROCESS | DEBUG_ONLY_THIS_PROCESS | PROCESS_VM_READ | PROCESS_QUERY_INFORMATION; 
    const char* comLine = "C:\\Windows\\Notepad.exe"; 

//  Start the child process. 
    if(CreateProcessA(NULL, // No module name (use command line) 
     (LPSTR)comLine,// argv[1],  // Command line 
    NULL,   // Process handle not inheritable 
    NULL,   // Thread handle not inheritable 
    FALSE,   // Set handle inheritance to FALSE 
    creationFlags,    // No creation flags 
    NULL,   // Use parent's environment block 
    NULL,   // Use parent's starting directory 
    &startInfo,   // Pointer to STARTUPINFO structure 
    &processInfo)   // Pointer to PROCESS_INFORMATION structure 
    == false) 
    { 
    printf("FAIL!"); 
return; 
    } 

    SetLastError(0); 
    bool ok = SymInitialize(processInfo.hProcess, NULL, true); 
    HRESULT err = HRESULT_FROM_WIN32(GetLastError()); 
} 

Je ne sais pas pourquoi il ne fonctionne pas pour vous - je suis sous Windows XP, il est peut-être une différence. Pour moi, SymInitialize renvoie true et GetLastError renvoie 0x800700cb, ce qui signifie qu'il n'a pas trouvé de variable evirnment pointant vers le répertoire avec les fichiers de symboles.

Cela peut être une question stupide, mais peut-être vous manque certaines bibliothèques de débogage dans votre système? Avez-vous essayé d'installer par exemple. Debugging Tools for Windows? Je recommanderais 'Télécharger les outils de débogage de l'option Windows SDK' - lisez la description. Je suppose que tous les IDE de programmation l'installent pour vous ou l'installent avant de déboguer quoi que ce soit, mais il est toujours préférable de vérifier.

+0

ok, s'il vous plaît faire. merci :) – Idov

+0

Non, j'ai tout. Peu importe, je vais juste charger les symboles manuellement :([sanglotant]. – Idov

0

Lorsque le processus est créé avec l'indicateur DEBUG_PROCESS, aucun symbole n'est chargé. Il est nécessaire d'attendre LOAD_DLL_DEBUG_EVENT, puis d'appeler SymInitialize().

Questions connexes