Using Win32 functions in Visual FoxPro Image Gallery
Code examples:
Custom GDI+ class
How to change display settings: screen resolution, screen refresh rate
GDI+: Implementing image scrolling with inertia
Obtaining names and positions for shortcuts located on the Windows Desktop
Vertical Label control
Displaying dimmed window behind VFP top-level form
Enumerating devices installed on the local machine
How to generate GUID values
Printing Image File, programmatically set print page orientation to landscape
Disconnecting USB Mass Storage Device programmatically
Storing content of the Clipboard to a bitmap file
Monitoring clipboard content changes (VFP9)
Subclassing CommandButton control to create BackColor property
Adding a background image to VFP report (VFP9, ReportListener)
Browsing Windows Known Folders (Special Folders)
Splash Screen for the VFP application
How to load a user profile
How to put a vertical text scrolling on the form (a movie cast)
Compressing and decompressing files with Windows API Runtime Library routines
How to control Adobe Reader 9.0 (SDI mode) from VFP application
Generating random cryptographic keys
GDI+ fun: roach-infested desktop
How to put a horizontal text scrolling on the form (a news line, marquee)
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: 72  
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-2018 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.39), the Open Source standard SQL database, AceHTML Freeware Version 4, freeware HTML Editor of choice.   Hosted by Korax Online Inc.
Last Topics Visited (54.92.197.82)
23 sec.Login
44 sec.Example: 'How to display picture stored in enhanced-format metafile (*.emf)'
1.08 min.Function: 'NetScheduleJobEnum'
Function group: 'Network Management'
1.4 min.Function: 'ShellExecute'
Function group: 'Shell Functions'
1.73 min.Function: 'CreateStreamOnHGlobal'
Function group: 'COM'
2.05 min.Function: 'GetMenuContextHelpId'
Function group: 'Shell Functions'
2.38 min.
Function group: 'Menu'
2.73 min.
Function group: 'HTTP Functions (WinHTTP)'
3.43 min.Updates
3.8 min.Function: 'waveOutUnprepareHeader'
Function group: 'Windows Multimedia'
Google
Advertise here!