Using Win32 functions in Visual FoxPro Image Gallery
Code examples:
Dragging files from Explorer window and dropping them on FoxPro control (requires VFP9)
How to retrieve network parameters for the local computer (including Host name, Domain name, and DNS Server)
Retrieving Printer Device Context using PrintDlg function
Scanning the hierarchy of child windows down from the main VFP window
Adding and deleting Scheduled Tasks using NetScheduleJob API functions
Building a tree of subdirectories for a given path using FindFile functions
How to remove a directory that is not empty
How to save registry key including its subkeys and values to a file
Monitoring changes occurring within a directory
Peer-to-peer LAN messenger built with Mailslot API functions
Simple printer queue monitor: deletes, pauses, resumes print jobs for local printer
Simple Window Viewer
Smart Card Database Query Functions
Using the GetTempFileName
Configuring DEVMODE structure for a printer
Creating a directory on the FTP
Creating a mailslot
Listing child windows for the Windows desktop
Retrieving geometrical parameters of the system desktop window
Using FoxTray ActiveX control: System Tray Icon and menu attached to VFP form
Clipping mouse cursor area
Enumerating network sessions established on a server
GDI+: how to make VFP controls visually shake and shudder
How to delete IE cookies, clear IE history and delete files in Temporary Internet Files directory
PocketPC: custom RAPI class for executing routines on remote Windows CE device

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
Before you begin:
The class calls CeCreateProcess RAPI function to create new process on PocketPC device. The new process executes the specified executable file.


Use the following code to test
TRapiExec class:

LOCAL rapi As TRapiExec
rapi = CREATEOBJECT("TRapiExec")
IF NOT rapi.Connected
        = MESSAGEBOX("Could not detect connection " +;
                "to Windows CE device. ", 48, "Not connected")
        RETURN
ENDIF
IF NOT rapi.RunApp("\Windows\Notes.exe",;
                "\My Documents\Personal\test.txt")
        = MESSAGEBOX("Error code: " + TRANSFORM(rapi.errorcode) +;
                " (" + TRANSFORM(rapi.errorcode, "@0") +;
                ") ", 48, "Error")
ENDIF

***************************************************
* Custom RAPI class for executing routines
* on remote Windows CE device
***************************************************
DEFINE CLASS TRapiExec As Session
#DEFINE WAIT_OBJECT_0  0
#DEFINE RAPIINIT_SIZE  12
#DEFINE PROCINFO_SIZE  16
    errorcode=0
    connected=.F.

PROCEDURE Init
    THIS.declare
    THIS.InitRapi()

PROTECTED PROCEDURE InitRapi
    LOCAL cBuffer, hEvent
    cBuffer = PADR(CHR(RAPIINIT_SIZE), RAPIINIT_SIZE, CHR(0))

    = CeRapiInitEx(@cBuffer)
    hEvent = buf2dword(SUBSTR(cBuffer,5,4))

    IF WaitForSingleObject(hEvent,2000) = WAIT_OBJECT_0
        THIS.connected = .T.
    ELSE
        THIS.UninitRapi
    ENDIF
RETURN THIS.connected

PROTECTED PROCEDURE UninitRapi
    = CeRapiUninit()
    THIS.connected = .F.

PROCEDURE Destroy
    THIS.UninitRapi

FUNCTION RunApp(cApp, cCmdLine) As Boolean
    LOCAL cBuffer, hProcess, hThread
    cBuffer = REPLICATE(CHR(0), PROCINFO_SIZE)
    IF VARTYPE(cCmdLine) <> "C"
        cCmdLine=""
    ENDIF
    IF CeCreateProcess(ToUnicode(m.cApp),;
        ToUnicode(m.cCmdline), 0,0,0,0,0,0,0, @cBuffer) = 0
        THIS.errorcode=CeGetLastError()
        RETURN .F.
    ENDIF
    hProcess = buf2dword(SUBSTR(cBuffer, 1,4))
    hThread = buf2dword(SUBSTR(cBuffer, 5,4))
    = CeCloseHandle(m.hThread)
    = CeCloseHandle(m.hProcess)
RETURN .T.

PROTECTED PROCEDURE declare
    DECLARE INTEGER CeRapiUninit IN rapi
    DECLARE INTEGER CeRapiGetError IN rapi
    DECLARE INTEGER CeGetLastError IN rapi
    DECLARE INTEGER CeRapiInitEx IN rapi STRING @pRapiInit
    DECLARE INTEGER CeCloseHandle IN rapi INTEGER hObject

    DECLARE INTEGER WaitForSingleObject IN kernel32;
        INTEGER hHandle, INTEGER dwMilliseconds

    DECLARE INTEGER CeCreateProcess IN rapi;
        STRING lpAppName, STRING lpCmdLine, INTEGER lpProcAttr,;
        INTEGER lpThreadAttr, INTEGER bInheritHandles,;
        INTEGER dwCreationFlags, INTEGER lpEnvironment,;
        INTEGER lpCurrDir, INTEGER lpStartupInfo, STRING @lpProcInfo

    DECLARE INTEGER CeRapiInvoke IN rapi;
        STRING pDllPath, STRING pFunctionName, INTEGER cbInput,;
        STRING @pInput, INTEGER @pcbOutput, STRING @ppOutput,;
        INTEGER @ppIRAPIStream, INTEGER dwReserved
ENDDEFINE

********************* static members **************************
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)

FUNCTION ToUnicode(cStr)
RETURN STRCONV(cStr+CHR(0), 5)

FUNCTION FromUnicode(cStr)
RETURN STRTRAN(STRCONV(cStr, 6), CHR(0), "")



User rating: 0/10 (0 votes)
Rate this code sample:
  • ~
2570 bytes  
Created: 2004-08-13 09:26:22  
Modified: 2011-12-10 09:20:22  
Visits in 7 days: 66  
Listed functions:
CeCloseHandle
CeCreateProcess
CeGetLastError
CeRapiGetError
CeRapiInitEx
CeRapiInvoke
CeRapiUninit
WaitForSingleObject
Printer friendly API declarations
My comment:
RunApp method of the class accepts two parameters: AppName and CommandLine.

Try the code calling various executables located inside \Windows directory on your PocketPC: Notes.exe, pxl.exe, sndplay.exe, tasks.exe, iexplore.exe, pword.exe etc.

* * *
At this point I am not aware how to terminate a remote process. There might be a way, I think, by using the CeRapiInvoke.

* * *
CeRapiInvoke function, I am still struggling with, can be used to remotely execute routines in DLLs stored on Window CE device. This function accepts DLL and function names as parameters, as well as buffer with input parameters for the remote call and buffer for output parameters.

So far I was successful only in calling functions, which do not require input parameters, like Random and GetProcessHeap (both located in Coredll.dll) and similar to them. Hopefully I will be able to add this functionality to the class some time later.

* * *
This is how to create new email message on your Pocket PC:
cApp="\Windows\tmail.exe"
cCmd=[-TO "info@yourcompany.com" -SUBJECT "Test message" ] +;
        [-BODY "Wake up!"]
rapi.RunApp(cApp, cCmd)

Note that the message will be stored in Drafts folder, not sent. Use other command line options: -CC, -BCC, -ATTACHMENT.
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:
dallgood@iservtexas.com | 2006-08-11 15:10:16
Would there be a way to have RunApp wait until the launched application was finished and closed. I played with trying to use WaitForSingleObject, but could not make it work. Thanks!

Copyright 2001-2016 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.34), the Open Source standard SQL database, AceHTML Freeware Version 4, freeware HTML Editor of choice.   Hosted by Korax Online Inc.
Last Topics Visited (54.197.142.219)
3 sec.
Function group: 'Process and Thread'
7 sec.Function: 'LsaClose'
10 sec.Function: 'waveInGetErrorText'
Function group: 'Windows Multimedia'
13 sec.Example: 'How to change display settings: screen resolution, screen refresh rate'
16 sec.Example: 'How to obtain Content-Type value for a file type from the System Registry'
22 sec.Example: 'Opening access to the Microsoft Internet functions for the application'
28 sec.Example: 'HOWTO: Use the Win32 API to Access File Dates and Times'
32 sec.Example: 'How to create non-blocking Winsock server'
Language: 'C++'
36 sec.Function: 'getsockopt'
38 sec.Example: 'Exporting DLL icon resources as .ICO files'
Google
Advertise here!