Can a PM program tell if there's a previous instance of itself running?

Raja Thiagarajan (sthiagar@bronze.ucs.indiana.edu) writes:

Can a PM program tell if there's a previous instance of itself running? In Win3.x (but apparently NOT Win32), there's a hPrevInst handle; is there an OS/2 2.x equivalent? Basically, I'm thinking in terms of a program that would try to borrow resources from a previous instance if a previous instance is running. (Specifically, if my palette animation program gets started twice, the second instance oughta share palettes with the first instance!)

What you're really asking is two questions:

  • How can I determine if a previous instance of my application is already running?
  • How can I share resources between multiple instances of my application?

    There are some ways to achieve this; You can use semaphores or/and shared memory or the other method described below. Try to open your named semaphore, if it's there, your application is already running, else create a semaphore... To share data, use shared memory. There are some excellent examples in Schildt & Goosey "OS/2 Programming".

    To answer your first question, you need to enumerate all of the main windows present on the desktop, and figure out if any of them are yours. This is achieved using the following code:

    HWND queryAppInstance(PCHAR pchClassWanted)
    {
       HENUM heEnum;
       HWND hwndTop;
       HWND hwndClient;
       CHAR achClass[256];
    
       heEnum=WinBeginEnumWindows(HWND_DESKTOP);
    
       hwndTop=WinGetNextWindow(heEnum);
       while (hwndTop!=NULLHANDLE) {
          hwndClient=WinWindowFromID(hwndTop,FID_CLIENT);
          if (hwndClient!=NULLHANDLE) {
             WinQueryClassName(hwndClient,sizeof(achClass),achClass);
             if (strcmp(achClass,pchClassWanted)==0) {
                WinEndEnumWindows(heEnum);
                return hwndClient;
             } /* endif */
          } /* endif */
    
          hwndTop=WinGetNextWindow(heEnum);
       } /* endwhile */
    
       WinEndEnumWindows(heEnum);
       return NULLHANDLE;
    }
    

    To answer your second question, the only way that I know of to share resources is to place them in a DLL. The procedure to do this is as follows:

    o

    o o

    Then, when your application starts, you simply call WinLoadLibrary (the preferred way) or DosLoadModule to load the DLL. These functions return a handle to the DLL which must then be used in any function which loads resources (e.g. GpiLoadBitmap, WinLoadPointer, etc.).

    Note that this procedure does not require knowing the window handle of any previous instance of your application because OS/2 implements DLLs in a shared fashion (which I suspect is one of the reasons they were created in the first place). All you need to know is the name of the DLL. This technique can also be used to share resources between different applications.

    (Quoted almost directly from EDMI/2 Edition 1)


    [Back: How do I put bitmaps on buttons?]
    [Next: How to avoid crash when using Drag and Drop?]