How to know the number of the process copy ?

This is a discussion on How to know the number of the process copy ? within the Windows Programming forums, part of the Platform Specific Boards category; Guys, Have a problem.. How can i check how many copies of the application was started ? I've tried in ...

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    9

    How to know the number of the process copy ?

    Guys, Have a problem..
    How can i check how many copies of the application was started ?
    I've tried in such way but it show me only the second copy, and the third copy is shown as the second anyway.. how to check the number of opened app copies and write it in the tooltip in tray ?
    thanks

    here is the code:
    Code:
    LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
    
    int WINAPI
    WinMain (HINSTANCE hThisInstance,
             HINSTANCE hPrevInstance,
             LPSTR lpszArgument,
             int nFunsterStil)
    
    {
        g_hwnd = FindWindow(szClassName,szTitleName);      
        MSG messages;                                  
        WNDCLASSEX wincl;                                
    
        _hInstance = hThisInstance;
    
        wincl.cbSize = sizeof (WNDCLASSEX);           
        wincl.style = CS_DBLCLKS;                      
        wincl.lpfnWndProc = WindowProcedure;            
        wincl.cbClsExtra = 0;                         
        wincl.cbWndExtra = 0;                 
        wincl.hInstance = hThisInstance;         
        wincl.hIcon = LoadIcon(NULL, MAKEINTRESOURCE(IDI_MYICON)); 
        wincl.hIconSm = LoadImage(NULL, MAKEINTRESOURCE(IDI_MYICON), IMAGE_ICON, 16, 16, 0); 
        wincl.hCursor = LoadCursor (NULL, IDC_ARROW);  
        wincl.hbrBackground = (HBRUSH)16; 
        wincl.lpszMenuName = NULL;               
        wincl.lpszClassName = szClassName;           
    
        g_hwnd = CreateWindowEx (
                0,                                           
                szClassName,                                       
                szTitleName,                                  
                WS_DLGFRAME,                                 
                160,           
                120,              
                310,                                          
                250,                                           
                HWND_DESKTOP,                                   
                NULL,                                        
                hThisInstance,                                     
                NULL                                            
                );
    
        while (GetMessage (&messages, NULL, 0, 0))
        {
            TranslateMessage(&messages); 
            DispatchMessage(&messages);  
        }
        return messages.wParam;
    }
    
    LRESULT CALLBACK
    WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
    
        NOTIFYICONDATA nid;
    
        switch (message)                  /* handle the messages */
        {
            case WM_CREATE:
                {
    
                    ZeroMemory(&nid,sizeof(nid));
                    nid.cbSize				=	sizeof(NOTIFYICONDATA);
                    nid.hWnd				=	hwnd;
                    nid.uID					=	0;
                    nid.uFlags				=	NIF_ICON | NIF_MESSAGE | NIF_TIP | NIF_INFO;
                    nid.uCallbackMessage	=	WM_USER;
                    nid.hIcon				=	LoadIcon(_hInstance,MAKEINTRESOURCE(IDI_MYICON));
    
                    if (g_hwnd)
                    {
                        _CopyOfapp++;
                        sprintf(nid.szTip, "%s%d", "app (copy ", _CopyOfapp,);
                    }
                    else
                    {
                        lstrcpy(nid.szTip,"app");
                    }
                    Shell_NotifyIcon(NIM_ADD,&nid);
                }
            break;
            case WM_DESTROY:
                PostQuitMessage (0);       /* send a WM_QUIT to the message queue */
                break;
            default:                      /* for messages that we don't deal with */
                return DefWindowProc (hwnd, message, wParam, lParam);
        }
    
        return 0;
    }

  2. #2
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    You could create a shared data segment and increment the variable for starting/closing the app. You could create a named region of shared memory and do the same as well. A semaphore could also be used for this task.

  3. #3
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    1,691
    Keep in mind that if you use a shared memory region, you could have problems if one of your instances closes before it can decrement the count (like if it crashes).

    If your looking for different instances of the same PE file open, rather than unique program, use PSAPI.
    A class that doesn't overload all operators just isn't finished yet. -- SmugCeePlusPlusWeenie
    A year spent in artificial intelligence is enough to make one believe in God. -- Alan J. Perlis

  4. #4
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    1,691
    On second thought,
    I would use event objects. The first instance will create Event 1. The second instance would detect the presence of Event 1 and create Event 2. The third instance would detect Event 1 and Event 2. And this could go on forever if done right. That would be pretty effecient, and easy to impliment too.
    A class that doesn't overload all operators just isn't finished yet. -- SmugCeePlusPlusWeenie
    A year spent in artificial intelligence is enough to make one believe in God. -- Alan J. Perlis

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Using a semaphore, as valaris suggested, would probably be the most efficient way to do it, AFAIK.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  6. #6
    Registered User
    Join Date
    Jun 2009
    Posts
    9
    can someone show me an example of how to use one of those functions?
    cause the examples in MSDN not so clear..
    I would prefer using a semaphore or event objects..

    thanks

  7. #7
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    1,691
    Here is a crude (sorry, just wrote it) example using semaphores
    Code:
    HANDLE h = OpenSemaphore(SEMAPHORE_ALL_ACCESS, 0, "my_special_app_sem_name");
    if(!h)
       h = CreateSemaphore(0, 1, 100, "my_special_app_sem_name");
    DWORD prev;
    ReleaseSemaphore(h, 1, &prev);
    DWORD dwInstanceCount = prev + 1;
    ReleaseSemaphore(h, dwInstanceCount, 0);
    
    
    cout << "Hello, I am instance #" << dwInstanceCount << " of this application." << endl;
    system("pause");
    
    
    DWORD prev;
    ReleaseSemaphore(h, 1, &prev);
    if(prev > 1)
       ReleaseSemaphore(h, prev - 1, 0);
    CloseHandle(h);
    A class that doesn't overload all operators just isn't finished yet. -- SmugCeePlusPlusWeenie
    A year spent in artificial intelligence is enough to make one believe in God. -- Alan J. Perlis

  8. #8
    Registered User
    Join Date
    Jun 2009
    Posts
    9
    But where do I need to place this code in windowed Winapi program ?
    That what i didn't understood in the MSDN example.. they showing an example with console application.
    And i don't know how to do it in basic win32api window..
    I'll try something, but i don know.

    P.S - also disn't found no one example on the internet on how to use it in windowed app.

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,022
    It should be easy if you understand how a Win32 Window App is made... it isn't rocket science.
    I suggest you make some research on how those programs work. Then you should easily fit it inside.
    Heck, you might even place it at the start of your program to prevent multiple instances from even starting the program.

    Just beware that some users may sneer at limitations such as multiple instances.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Registered User
    Join Date
    Jun 2009
    Posts
    9
    Ok. I fitted it in the prog.
    Just it always show #2 instance... and no increment..
    What can be the prob ?

    Also it showed me that 'prev;' was declared twice, so i deleted the second 'DWORD prev;'

  11. #11
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> What can be the prob ?

    The problem, quite frankly, is that you haven't built up enough trouble-shooting skills to work things out in a clear, logical manner. You can't just slap together random pieces of code, without even understanding what it does, and then expect everything to work out great. There's *no* substitute for hard study and practice. It's as simple as that.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  12. #12
    Registered User
    Join Date
    Jun 2009
    Posts
    9
    Ok. First of all, i know how it all works. I'm a programmer about 3 years. i've built already many programs.
    My problem that Recently, i'm very lazy. So i thought maybe people that know about this topix will show me a simple code of how to do this.

    Ok. I'll try to do it myself.

    Thanks for everyone..

  13. #13
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    I understand. We all get lazy from time to time, but ultimately you do have to stand on your own and be willing to do most of the work. We're just here to give advice, after all.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  14. #14
    Registered User
    Join Date
    Jun 2009
    Posts
    9
    people i'm tired..
    Need help again.
    All works fine for me now, but!
    Example:
    I'm opening the first instance (It shows me instance #1)
    Then opening the second instance (It shows me instance #2)
    Then CLOSING the first instance
    Then opening another new instance (It shows me instance #2 again !!! because I closed the first one so the semaphore decreased by one)
    maybe there is a way to show here instance #1 ? I mean the number of the instance that was closed?
    Because I don't want that there will be two instances with the same number.

    P.S - please don't tell me now that i can't do this with the semaphore function, and i need to use another!

  15. #15
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,812
    What are you trying to do.

    Do you need to number of applications running or do you just need the application running to have a unique id or something?
    Woop?

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 02-08-2009, 09:26 PM
  2. Check number of times a process is running
    By linuxwolf in forum Windows Programming
    Replies: 6
    Last Post: 10-17-2008, 12:08 PM
  3. xor linked list
    By adramalech in forum C Programming
    Replies: 23
    Last Post: 10-14-2008, 11:13 AM
  4. Replies: 11
    Last Post: 10-07-2008, 07:19 PM
  5. copy some bits into a 8 bits binary number
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 05-29-2002, 11:54 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21