Detect other application running

Detect other application running

Postby Randal » Thu Feb 02, 2006 10:09 pm

All,

How can I detect if another program is running in Windows? I have a few functions that I do not want my users to be able to use if a specific application is also running in Windows. I need this to work for both FW & FWH.

Thanks,
Randal Ferguson
Randal
 
Posts: 260
Joined: Mon Oct 24, 2005 8:04 pm

Postby Antonio Linares » Thu Feb 02, 2006 11:31 pm

Randal,

You may use GetModuleHandle( cModuleName ) --> hInstance
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42080
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Postby Randal » Fri Feb 03, 2006 1:27 pm

Antonio,

<<You may use GetModuleHandle( cModuleName ) --> hInstance>>

I've tried using the name as it appears in the Window title bar and the .exe name and it always returns 0. What should I specify for cModuleName? What are the possible return codes? Is this function documented anywhere?

This external application isn't necessarily started from my fw app.

Thanks,
Randal
Randal
 
Posts: 260
Joined: Mon Oct 24, 2005 8:04 pm

Postby Enrico Maria Giordano » Fri Feb 03, 2006 2:35 pm

Randal Ferguson wrote:Antonio,

<<You may use GetModuleHandle( cModuleName ) --> hInstance>>

I've tried using the name as it appears in the Window title bar and the .exe name and it always returns 0. What should I specify for cModuleName? What are the possible return codes? Is this function documented anywhere?

This external application isn't necessarily started from my fw app.

Thanks,
Randal


It seems that GetModuleHandle() can retrieve only the handle of the module started from the current process. From the MSDN:


Platform SDK: DLLs, Processes, and Threads
GetModuleHandle
The GetModuleHandle function retrieves a module handle for the specified module if the file has been mapped into the address space of the calling process.

To avoid the race conditions described in the Remarks section, use the GetModuleHandleEx function.

HMODULE GetModuleHandle(
LPCTSTR lpModuleName // module name
);
Parameters
lpModuleName
[in] Pointer to a null-terminated string that contains the name of the module (either a .dll or .exe file). If the file name extension is omitted, the default library extension .dll is appended. The file name string can include a trailing point character (.) to indicate that the module name has no extension. The string does not have to specify a path. When specifying a path, be sure to use backslashes (\), not forward slashes (/). The name is compared (case independently) to the names of modules currently mapped into the address space of the calling process.
If this parameter is NULL, GetModuleHandle returns a handle to the file used to create the calling process.

Return Values
If the function succeeds, the return value is a handle to the specified module.

If the function fails, the return value is NULL. To get extended error information, call GetLastError.

Remarks
The returned handle is not global or inheritable. It cannot be duplicated or used by another process.

The GetModuleHandle function returns a handle to a mapped module without incrementing its reference count. Therefore, use care when passing the handle to the FreeLibrary function, because doing so can cause a DLL module to be unmapped prematurely.

This function must also be used carefully in a multithreaded application. There is no guarantee that the module handle remains valid between the time this function returns the handle and the time it is used by another function. For example, a thread might retrieve a module handle by calling GetModuleHandle. Before the thread uses the handle in another function, a second thread could free the module and the system could load another module, giving it the same handle as the module that was recently freed. The first thread would be left with a module handle that refers to a module different than the one intended.

Windows 95/98/Me: GetModuleHandleW is supported by the Microsoft Layer for Unicode. To use this, you must add certain files to your application, as outlined in Microsoft Layer for Unicode on Windows 95/98/Me Systems.

Example Code
For an example, see Using Brushes.

Requirements
Windows NT/2000/XP: Included in Windows NT 3.1 and later.
Windows 95/98/Me: Included in Windows 95 and later.
Header: Declared in Winbase.h; include Windows.h.
Library: Use Kernel32.lib.
Unicode: Implemented as Unicode and ANSI versions on Windows NT/2000/XP. Also supported by Microsoft Layer for Unicode.

See Also
Dynamic-Link Libraries Overview, Dynamic-Link Library Functions, FreeLibrary, GetModuleFileName, GetModuleHandleEx

Platform SDK Release: November 2001 What did you think of this topic?
Let us know. Order a Platform SDK CD Online
(U.S/Canada) (International)



Requirements
Windows NT/2000/XP: Included in Windows NT 3.1 and later.
Windows 95/98/Me: Included in Windows 95 and later.
Header: Declared in Winbase.h; include Windows.h.
Library: Use Kernel32.lib.
Unicode: Implemented as Unicode and ANSI versions on Windows NT/2000/XP. Also supported by Microsoft Layer for Unicode.
See Also
Dynamic-Link Libraries Overview, Dynamic-Link Library Functions, FreeLibrary, GetModuleFileName, GetModuleHandleEx


EMG
User avatar
Enrico Maria Giordano
 
Posts: 8710
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia

Postby James Bott » Fri Feb 03, 2006 4:17 pm

Randal,

Good to hear from you. I was just wondering where you were.

I think Enrico originally posted the code below. Give it a try.

James

Code: Select all  Expand view
#include "Fivewin.ch"


#define GW_HWNDFIRST   0
#define GW_HWNDLAST   1
#define GW_HWNDNEXT   2
#define GW_HWNDPREV   3
#define GW_OWNER   4
#define GW_CHILD   5


FUNCTION MAIN()

    LOCAL hWnd := FINDWND( "Internet Explorer" )

    IF !EMPTY( hWnd )
        ? GETWINDOWTEXT( hWnd )
    ELSE
        ? "Window not found"
    ENDIF

    RETURN NIL


FUNCTION FINDWND( cTitle )

    LOCAL hWnd := GETFOREGROUNDWINDOW()

    WHILE hWnd != 0
        IF UPPER( cTitle ) $ UPPER( GETWINDOWTEXT( hWnd ) )
            RETURN hWnd
        ENDIF

        hWnd = GETWINDOW( hWnd, GW_HWNDNEXT )
    ENDDO

    RETURN NIL


DLL32 FUNCTION GETFOREGROUNDWINDOW() AS LONG;
      PASCAL FROM "GetForegroundWindow" LIB "user32.dll"
User avatar
James Bott
 
Posts: 4840
Joined: Fri Nov 18, 2005 4:52 pm
Location: San Diego, California, USA

Postby Randal » Mon Feb 06, 2006 7:48 pm

James,

Thanks for the sample. I'll give this a try.

Thanks,
Randal Ferguson
Randal
 
Posts: 260
Joined: Mon Oct 24, 2005 8:04 pm


Return to FiveWin for CA-Clipper

Who is online

Users browsing this forum: No registered users and 4 guests