[C]Why is this code not working ?

Discussion in 'Mixed Languages' started by Pr3acher, Feb 28, 2013.

  1. Pr3acher

    Pr3acher MDL Member

    Aug 24, 2012
    142
    48
    10
    Hi,
    i'm on a project and i almost finished, but i'm stuck with a part of code.

    Actually, my aim is to browse all process till i find the right one and then i open it. If the process is not yet running, then i wait for it to be running, that's why i have a Sleep() to slow the loop down and a limit to the loop (avoid infinite loop).
    That's something like that (C Win32):

    HANDLE snap=NULL;
    PROCESSENTRY32 pe32={0};
    int securedLoop=0;

    Process32First(snap,&pe32);

    while(stricmp(pe32.szExeFile,fileNameProcess)!=0)
    {
    Process32Next(snap,&pe32); // Next process in the snapshot

    securedLoop++;
    if(securedLoop>=700)//Avoid infinite loop
    {
    SendMessage(logEdit,WM_SETTEXT,0,(LPARAM)"...");
    return 1;
    }
    Sleep(1000);
    }

    My program just dont work, it freezes so i need help with it, thanks.
     
  2. Calistoga

    Calistoga MDL Senior Member

    Jul 25, 2009
    420
    198
    10
    #2 Calistoga, Mar 2, 2013
    Last edited by a moderator: Apr 20, 2017
    The documentation, my friend :)

    Process32First()

    See http://msdn.microsoft.com/en-us/library/windows/desktop/ms684834(v=vs.85).aspx

    Example
    Code:
    HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
        
    if (INVALID_HANDLE_VALUE == hSnapshot)
    {
        return 0; /* Failed to create snapshot. */
    }
    
    PROCESSENTRY32

    See http://msdn.microsoft.com/en-us/library/windows/desktop/ms684839(v=vs.85).aspx

    Example
    Code:
    PROCESSENTRY32 pe32 = { 0 };
    {
        pe32.dwSize = sizeof(pe32);
    }
    
    And when you're done, don't forget
     
  3. Pr3acher

    Pr3acher MDL Member

    Aug 24, 2012
    142
    48
    10
    #3 Pr3acher, Mar 2, 2013
    Last edited by a moderator: Apr 20, 2017
    (OP)
    Yes of course, i made a mistake copying my code.... Actually i correctly used the functions i think, but this problem is solved btw. But now i need help for some other problems (2) :D .
    So i send some text to an edit and just after i begin a loop to search for a process, but the text can't be seen in the edit whereas the loop is being executed, besides my program doesn't respond while looping and make my PC freeze and i need to manually restart the PC each time... So i paste my code (no pasting mistake this time ^^ ):

    Code:
    
    SendMessage(logEdit,WM_SETTEXT,0,(LPARAM)logBuff); //Send the text
    
    while(PeekMessage(&uMsg,0,0,0,PM_REMOVE)>0)//Force messages in the queue to be processed
    {
    TranslateMessage(&uMsg);
    DispatchMessage(&uMsg);
    }
    
           // Take a snapshot, look for process ...
    
    do
    {
    if(i>=50)
    {
    snap=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
    Process32First(snap,&pe32);
    i=0;
    }
    
    Process32Next(snap,&pe32);
    i++;
    
    }while(stricmp(fileNameProcess,pe32.szExeFile)!=0);
    
    
    hOpenProcess=OpenProcess(PROCESS_ALL_ACCESS,FALSE,pe32.th32ProcessID);
    
     
  4. Calistoga

    Calistoga MDL Senior Member

    Jul 25, 2009
    420
    198
    10
    Sorry for answering so late. If you haven't already solved it ...

    You're sure it's not the SendMessage() that hangs? That might happen if the window you're sending a message to for some reason doesn't process them. If the program is single threaded, that might be the problem.

    You could try launching a separate thread (CreateThread(...)), or - just to test - replace SendMessage() with PostMessage() (doesn't wait for the target window to process the message).
     
  5. Pr3acher

    Pr3acher MDL Member

    Aug 24, 2012
    142
    48
    10
    Hi,
    well i checked what SendMessage() returned and all was ok (return TRUE). Then i already thought about using PostMessage() instead since i know those messages won't be queueded, but i didn't make it. So I'll try PostMessage() later, and i'll say you if it is fixed. If not, then i'll try to create two separated thread indeed because it currently has only one. Thanks anyway, and no problem for the delay you responded :)