How to Use the DECLARE-DLL Command to Call Win32 SDK Functions

This article was previously published under Q136566
This article has been archived. It is offered "as is" and will no longer be updated.
This article shows by example how to use the DECLARE-DLL command in VisualFoxPro to call functions that are part of the WIN32 Software DevelopmentKit (SDK). The example code shows you how to use DECLARE-DLL to set upfunctions that you can use to swap the Foreground window to anotherapplication.

Sample Code

* DECLARE is used to make connections to two SDK functions. It* uses the keyword WIN32API for the LibraryName to tell Visual FoxPro* to search for the 32-bit Windows .dll function in Kernel32.dll,* Gdi32.dll, User32.dll, Mpr.dll, and Advapi32.dll.** FindWindow takes two parameters and returns the HWND of the application* window if found.DECLARE INTEGER FindWindow IN WIN32API AS findw STRING,STRING* SetForegroundWindow takes one parameter and returns true(1) or false(0)* depending upon its ability to switch to the other window.DECLARE INTEGER SetForegroundWindow IN WIN32API INTEGER* GetFocus takes no parameters and returns the HWND* of the window that currently has the focus.DECLARE INTEGER GetFocus IN WIN32APImynull = .NULL.       && You need a NULL when you do not know the                      && the class name of the window.winname = "File Manager"  && This should be the name of another window that                          && is open on the Windows desktop.* First call FindWindow to get the HWNDs for the application you want* to switch to. It is important that Visual FoxPro has the focus when this* is run and that you know the exact name of the window that you want to* switch to. For more info Please see the Note later in this code.foxhand= getfocus()pmhand = findw(mynull,winname)IF pmhand<>0   && If you have an HWND, switch   WAIT WINDOW "Press Any Key to Switch to another window and then back;                after a short interval."   switched = setforegroundwindow(pmhand)   IF switched = 1  && If switched, count for a while and switch back      ii=1      DO WHILE ii<50000         ii=ii+1      ENDDO      switched = setforegroundwindow(foxhand)      WAIT WINDOW "I'm back now"   ELSE      WAIT WINDOW  "Switch failed"   ENDIFELSE   WAIT WINDOW "Cannot find that window"ENDIF				
NOTE: Under Windows NT and Windows 95, if a child window is maximized, itsname is added to the name of the parent window in the Title Bar of theParent Window. For example, when you run Microsoft Word it automaticallyopens an empty document. If this document (call it Document1), is notmaximized, the title of the main Microsoft Word window is "Microsoft Word."If Document1 is maximized, the title is "Microsoft Word - Document1." Inthe example code, if you want to switch to the Microsoft Word session, setthe winname variable to "Microsoft Word - Document1."

Article ID: 136566 - Last Review: 02/24/2014 08:21:38 - Revision: 1.1

  • Microsoft Visual FoxPro 3.0 Standard Edition
  • kbnosurvey kbarchive KB136566