vdegenne
vdegenne

Reputation: 13288

GetProcessImageFileName and LPTSTR

Someone can say if I'm using it right ?

LPTSTR nameProc = "";
...
GetProcessImageFileName( hProcess, nameProc, 50 );

printf("name process : %s\n", nameProc);

I'm not used with the win32 types, the 2nd argument of the GetProcessImageFileName requires a LPTSTR typedef and the third one takes a DWORD variable. if not the method maybe i'm not printing the value of nameProc the right way ? (it prints an empty string by the way)

thanks in advance.

(please try to avoid leading me to some win32 api documents, i have no intention to learn about it, i just need to trace the usage memory of one process, therefore I won't no longer deal with win types)

EDIT (updated code):

void printMemoryInfo( DWORD processID ) {

    HANDLE hProcess;
    TCHAR nameProc[MAX_PATH];

    printf("\nProcess ID: %u\n", processID);

    hProcess = OpenProcess( PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, processID );

    if (hProcess == NULL) return;

    if (GetProcessImageFileName( hProcess, nameProc, sizeof(nameProc)/sizeof(*nameProc) )==0)
        printf("error\n");

    else printf("%s\n", nameProc);
}

int main (void) {

    DWORD aProcesses[1024], cbNeeded, cProcesses;
    unsigned int i;

    if (!EnumProcesses( aProcesses, sizeof(aProcesses), &cbNeeded )) {

        return 1;
    }

    cProcesses = cbNeeded/sizeof(DWORD);

    for (i=0; i < cProcesses; i++) {

    printMemoryInfo( aProcesses[i] );
    }

    return 0;
}

Upvotes: 0

Views: 9346

Answers (2)

Matteo Italia
Matteo Italia

Reputation: 126867

You may not want to learn about the Win32 types (and IMHO it's quite a bad idea, since programming on Windows often the APIs come useful), but even without understanding what a LPTSTR is the MSDN documentation is quite explicit about the second parameter:

lpImageFileName [out]

        A pointer to a buffer that receives the full path to the executable file.

So it's quite clear that your nameProc isn't OK, since you aren't allocating any memory for it. You'll probably want to write:

TCHAR nameProc[MAX_PATH];
if(GetProcessImageFileName(hProcess, nameProc, sizeof(nameProc)/sizeof(*nameProc))==0)
{
    // some error happened
}

Upvotes: 4

spencercw
spencercw

Reputation: 3358

nameProc needs to be a mutable buffer as it is an output parameter, at the moment it will probably just crash. Change this:

LPTSTR nameProc = "";

to this:

TCHAR nameProc[MAX_PATH];

Also pass MAX_PATH where you are passing 50 at the moment.

Upvotes: 4

Related Questions