Using Win32 functions in Visual FoxPro Image Gallery
Code examples:
Custom GDI+ class
Storing content of the Clipboard to a bitmap file
Storing screen shot of a form to bitmap file
How to print FoxPro form
Displaying icons in the system tray (VFP9)
Using NetWkstaTransportEnum to obtain MAC Address of remote server
Capturing keyboard activity of another application with the Raw Input API (VFP9)
How to print a bitmap file
GDI+: Implementing image scrolling with inertia
Using the LoadImage function to have a bitmap file loaded and displayed on VFP main window
Browsing Windows Known Folders (Special Folders)
How to ping a remote site using IP Helper API calls
Obtaining names and positions for shortcuts located on the Windows Desktop
Using InternetSetFilePointer when resuming interrupted download from the Internet
Monitoring clipboard content changes (VFP9)
Enumerating Processes -- WinNT
How to view icons stored in executable files (Icon Viewer) - II
GDI+: reading and writing metadata in JPEG and TIFF files
HOWTO: Use the Win32 API to Access File Dates and Times
System Image List Viewer
Attaching menu to a top-level form
Loading a string resource from an executable file
Vertical Label control
Detecting changes in connections to removable drives (VFP9)
How to make application automatically close all documents it opened

User rating: 0/10 (0 votes)
Rate this code sample:
  • ~
More code examples    Listed functions    Add comment     W32 Constants      Translate this page Printer friendly version of this code sample
Versions:
click to open
Before you begin:
Imagine FoxPro application that has opened several windows like Word, Excel, PDF, browser, picture viewer, DOS windows etc. Closing all those documents automatically on exit from the application, sometimes this can be a good idea.

Windows XP/2K introduces a Job Object, a tool that can manage a group of processes as a single process. In other words, operations performed on the job object affect all processes associated with it.

The processes are associated with a job object through their handles. FoxPro RUN command does not return a handle of a started process. For that reason, the CreateProcess API function is used instead of RUN.


Click on Open Document button and open several files like texts, spreadsheets, pictures. Then click on Close All button to close all windows that have been opened.

LOCAL oForm
oForm = CREATEOBJECT("TForm")
oForm.Show(1)
* end of main

DEFINE CLASS TForm As Form
#DEFINE MAX_PATH 260
    Width=300
    Height=200
    BorderStyle=2
    MinButton=.F.
    MaxButton=.F.
    Autocenter=.T.
    Caption="Managing group of processes"
    hJob=0

    ADD OBJECT cmdOpenApp As CommandButton WITH;
    Left=90, Top=36, Width=120, Height=27, Caption="Open Document"

    ADD OBJECT cmdOpenDos As CommandButton WITH;
    Left=90, Top=70, Width=120, Height=27, Caption="Open DOS Window"

    ADD OBJECT cmdClose As CommandButton WITH;
    Left=90, Top=130, Width=120, Height=27, Caption="Close All"

PROCEDURE Init
    THIS.declare

PROCEDURE Destroy
    THIS.CloseJob

PROCEDURE cmdOpenApp.Click
    ThisForm.AddToJob

PROCEDURE cmdOpenDos.Click
    ThisForm.OpenDosWindow

PROCEDURE AddToJob
    LOCAL cPath, cDocName, hProcess, nResult

    cPath = SYS(5) + SYS(2003)
    cDocName = GETFILE("*;TXT;DOC;XLS;MDB;PDF;JPG;BMP;GIF")
    SET DEFAULT TO (m.cPath)

    IF NOT EMPTY(cDocName)
        hProcess = THIS.OpenDoc(cDocName)

        IF hProcess <> 0
            IF THIS.hJob = 0
                THIS.OpenJob
            ENDIF

            nResult = AssignProcessToJobObject(THIS.hJob, hProcess)

            IF nResult = 0
                ? "AssignProcessToJobObject faied:", GetLastError()
            ENDIF

        ENDIF
    ENDIF

PROCEDURE cmdClose.Click
    ThisForm.CloseJob

PROCEDURE OpenJob
    THIS.hJob = CreateJobObject(0, SUBSTR(SYS(2015), 3))

PROCEDURE CloseJob
    IF THIS.hJob <> 0

        LOCAL nResult
        nResult = TerminateJobObject(THIS.hJob, 0)

        IF nResult = 0
            ACTIVATE SCREEN
            ? "TerminateJobObject failed:", GetLastError()
        ENDIF

        = CloseHandle(THIS.hJob)
        THIS.hJob=0
    ENDIF

PROCEDURE OpenDosWindow
    LOCAL cSystem32, cAppName, hProcess

    cSystem32 = THIS.GetSystem32Directory()
    cAppName = cSystem32 + "\cmd.exe"

    IF NOT FILE(cAppName)
        cAppName = cSystem32 + "\command.com"
    ENDIF

    IF FILE(cAppName)
        hProcess = THIS.StartProcess(cAppName, "")
        IF hProcess <> 0
            IF THIS.hJob = 0
                THIS.OpenJob
            ENDIF
            = AssignProcessToJobObject(THIS.hJob, hProcess)
        ENDIF
    ENDIF

PROCEDURE GetSystem32Directory
    LOCAL cBuffer, nResult
    cBuffer = REPLICATE(CHR(0), MAX_PATH)
    nResult = GetSystemDirectory(@cBuffer, Len(cBuffer))
RETURN SUBSTR(cBuffer, 1, nResult)

PROCEDURE OpenDoc(cDocName)
    LOCAL cAppName
    cAppName = THIS.GetAppName(cDocName)

    IF LEN(cAppName)=0
        WAIT WINDOW NOWAIT "Default application not found for " +;
            CHR(13) + cDocName
        RETURN 0
    ENDIF

    WAIT WINDOW NOWAIT cAppName
RETURN THIS.StartProcess(m.cAppName, m.cDocName)

PROCEDURE StartProcess(cAppName, cCmdLine)
#DEFINE START_INFO_SIZE  68

#DEFINE CREATE_BREAKAWAY_FROM_JOB 0x01000000
#DEFINE NORMAL_PRIORITY_CLASS 0x00000020

    LOCAL cProcInfo, cStartInfo, nResult, hProcess, nFlags

    nFlags = BITOR(CREATE_BREAKAWAY_FROM_JOB, NORMAL_PRIORITY_CLASS)

    hProcess = 0
    cProcInfo = Repli(Chr(0), 16)
    cStartInfo = PADR(Chr(START_INFO_SIZE), START_INFO_SIZE, Chr(0))

    IF NOT EMPTY(m.cCmdLine)
        cCmdLine = [ "] + ALLTRIM(cCmdLine) + ["]
    ENDIF

    nResult = CreateProcess(cAppName, cCmdLine, 0,0,0,;
        nFlags,0, SYS(5)+SYS(2003),;
        @cStartInfo, @cProcInfo)

    IF nResult <> 0
    * retrieve process handle from PROCINFO structure
        hProcess = buf2dword(SUBSTR(cProcInfo, 1,4))
    ENDIF
RETURN m.hProcess

FUNCTION GetAppName(cFileName)
    LOCAL cResult
    cResult = REPLICATE(CHR(0), MAX_PATH)
    = FindExecutable(cFileName, "", @cResult)
RETURN ALLTRIM(STRTRAN(cResult, CHR(0), ""))

PROTECTED PROCEDURE declare
    DECLARE INTEGER GetLastError IN kernel32

    DECLARE INTEGER CloseHandle IN kernel32 INTEGER hObject

    DECLARE INTEGER TerminateJobObject IN kernel32;
        INTEGER hJob, LONG uExitCode

    DECLARE INTEGER CreateJobObject IN kernel32;
        INTEGER lpJobAttributes, STRING lpName

    DECLARE INTEGER AssignProcessToJobObject IN kernel32;
        INTEGER hJob, INTEGER hProcess

    DECLARE INTEGER FindExecutable IN shell32;
        STRING lpFile, STRING lpDir, STRING @lpResult

    DECLARE INTEGER GetSystemDirectory IN kernel32;
        STRING @ lpBuffer, INTEGER nSize

    DECLARE INTEGER CreateProcess IN kernel32;
        STRING lpApplicationName, STRING lpCommandLine,;
        INTEGER lpProcessAttr, INTEGER lpThreadAttr,;
        INTEGER bInheritHandles, INTEGER dwCreationFlags,;
        INTEGER lpEnvironment, STRING lpCurrentDirectory,;
        STRING @lpStartupInfo, STRING @lpProcessInformation

ENDDEFINE

FUNCTION buf2dword(lcBuffer)
RETURN Asc(SUBSTR(lcBuffer, 1,1)) + ;
    BitLShift(Asc(SUBSTR(lcBuffer, 2,1)),  8) +;
    BitLShift(Asc(SUBSTR(lcBuffer, 3,1)), 16) +;
    BitLShift(Asc(SUBSTR(lcBuffer, 4,1)), 24)



User rating: 0/10 (0 votes)
Rate this code sample:
  • ~
4633 bytes  
Created: 2005-07-28 18:32:16  
Modified: 2012-05-02 13:36:34  
Visits in 7 days: 124  
Listed functions:
AssignProcessToJobObject
CloseHandle
CreateJobObject
CreateProcess
FindExecutable
GetLastError
GetSystemDirectory
TerminateJobObject
Printer friendly API declarations
Word Index links for this example:
Translate this page:
  Spanish    Portuguese    German    French    Italian  
FreeTranslation.com offers instant, free translations of text or web pages.
User Contributed Notes:
There are no notes on this subject.


Copyright 2001-2017 News2News, Inc. Before reproducing or distributing any data from this site please ask for an approval from its owner. Unless otherwise specified, this page is for your personal and non-commercial use. The information on this page is presented AS IS, meaning that you may use it at your own risk. Microsoft Visual FoxPro and Windows are trade marks of Microsoft Corp. All other trademarks are the property of their respective owners. 

Privacy policy
Credits: PHP (4.4.9), an HTML-embedded scripting language, MySQL (5.6.38), the Open Source standard SQL database, AceHTML Freeware Version 4, freeware HTML Editor of choice.   Hosted by Korax Online Inc.
Last Topics Visited (54.158.214.111)
3 sec.Example: 'Using WM_COPYDATA for interprocess communication (VFP9)'
6 sec.Function: 'CreateHardLink'
Function group: 'File Management'
9 sec.Function: 'accept'
Function group: 'Windows Sockets 2 (Winsock)'
19 sec.Function: 'GdipTranslateWorldTransform'
Function group: 'GDI+ Graphics'
24 sec.Function: 'FtpGetCurrentDirectory'
31 sec.Function: 'MAPISendDocuments'
3 day(s)Example: 'Using File Mapping for enumerating files opened by Visual FoxPro'
 Function: 'CryptStringToBinary'
 Function: 'JetCreateDatabase'
Function group: 'Extensible Storage Engine (ESE, Jet Blue)'
 Function: 'midiOutGetDevCaps'
Function group: 'Windows Multimedia'
Google
Advertise here!