Using Win32 functions in Visual FoxPro Image Gallery
Code examples:
Winsock: sending email messages (SMTP, port 25)
Using Video Capture: displaying on FoxPro form frames and previewing video obtained from a digital camera
How to change display settings: screen resolution, screen refresh rate
MSMQ: how to open a queue
Adding and deleting User Accounts
How to copy the image of a form to the Clipboard using Bitmap API functions
How to make a VFP form fading out when released (GDI+ version)
Retrieving the interface–to–IP address mapping table
Custom GDI+ class
How to draw custom Window Caption on FoxPro form
How to display Windows On-Screen Keyboard
Obtaining the bounding rectangle for the specified device context
Printing Image File, programmatically set print page orientation to landscape
Using WM_COPYDATA for interprocess communication (VFP9)
Custom HttpRequest class (WinHTTP)
Subclassing CommandButton control to create BackColor property
The SQLGetProp() creates a bridge between Visual FoxPro and the ODBC API
Winsock: retrieving the standard host name and IP address for the local machine
Bitmap Class for Visual FoxPro application
Displaying the associated icons and descriptions for files and folders
Splash Screen for the VFP application
How to detect if additional monitor is connected and active
Class for sound recording
Creating irregularly shaped FoxPro form using transparency color key
How to assemble an array of strings and pass it to external function

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:
The PathFindOnPath function is anything but special. Whatever it does, can be accomplished by using FILE() and GETENV() FoxPro functions. Though it gives me a chance to demonstrate a technique of assembling an array of strings for being passed to external function.

More comments below the source code.
 
#DEFINE MAX_PATH 260
 
DECLARE INTEGER PathFindOnPath IN shlwapi;
    STRING @pszFile, STRING @ppszOtherDirs
 
LOCAL cFile, oPath1, oPath2, oPath3, cPathArray
 
* The first parameter is not just a file name to search;
* If the search is successful, this parameter is used 
* to return the fully qualified path name.
* 
* For that reason it must be long enough to accomodate
* a path of maximum length
cFile = PADR("wininet.dll", MAX_PATH, CHR(0))
 
* PChar class wraps GlobalAlloc and GlobalFree calls
oPath1 = CREATEOBJECT("PChar", "c:\dir1" + CHR(0))
oPath2 = CREATEOBJECT("PChar", "c:\dir2" + CHR(0))
oPath3 = CREATEOBJECT("PChar", "c:\documents\dir1" + CHR(0))
 
* the null-terminated array contains three memory addresses
* padded with zero bytes
cPathArray = num2dword(oPath1.GetAddr()) +;
    num2dword(oPath2.GetAddr()) +;
    num2dword(oPath3.GetAddr()) +;
    num2dword(0) + CHR(0)
 
IF PathFindOnPath(@cFile, @cPathArray) = 0
    ? "File not found..."
ELSE
    * at this point cFile contains the fully qualified path
    * padded to MAX_PATH length with zero bytes
    cFile = STRTRAN(cFile, CHR(0), "")
    ? cFile
ENDIF
* end of main
 
FUNCTION num2dword(lnValue)
#DEFINE m0  256
#DEFINE m1  65536
#DEFINE m2  16777216
    IF lnValue < 0
        lnValue = 0x100000000 + lnValue
    ENDIF
    LOCAL b0, b1, b2, b3
    b3 = Int(lnValue/m2)
    b2 = Int((lnValue - b3*m2)/m1)
    b1 = Int((lnValue - b3*m2 - b2*m1)/m0)
    b0 = Mod(lnValue, m0)
RETURN Chr(b0)+Chr(b1)+Chr(b2)+Chr(b3)
 
DEFINE CLASS PChar As Custom
PROTECTED hMem
 
PROCEDURE Init(lcString)
    THIS.hMem = 0
    THIS.setValue (lcString)
 
PROCEDURE Destroy
    THIS.ReleaseString
 
FUNCTION GetAddr  && returns a pointer to the string
RETURN THIS.hMem
 
FUNCTION GetValue && returns string value
    LOCAL lnSize, lcBuffer
    lnSize = THIS.getAllocSize()
    lcBuffer = SPACE(lnSize)
 
    IF THIS.hMem <> 0
        DECLARE RtlMoveMemory IN kernel32 As MemToStr;
            STRING @, INTEGER, INTEGER
        = MemToStr(@lcBuffer, THIS.hMem, lnSize)
    ENDIF
RETURN lcBuffer
 
FUNCTION GetAllocSize  && returns allocated memory size (string length)
    DECLARE INTEGER GlobalSize IN kernel32 INTEGER hMem
RETURN Iif(THIS.hMem=0, 0, GlobalSize(THIS.hMem))
 
PROCEDURE SetValue (lcString) && assigns new string value
#DEFINE GMEM_FIXED   0 
    THIS.ReleaseString
 
    DECLARE INTEGER GlobalAlloc IN kernel32 INTEGER, INTEGER
    DECLARE RtlMoveMemory IN kernel32 As StrToMem;
        INTEGER, STRING @, INTEGER
 
    LOCAL lnSize
    lcString = lcString + Chr(0)
    lnSize = Len(lcString)
    THIS.hMem = GlobalAlloc(GMEM_FIXED, lnSize)
    IF THIS.hMem <> 0
        = StrToMem(THIS.hMem, @lcString, lnSize)
    ENDIF
 
PROCEDURE ReleaseString  && releases allocated memory
    IF THIS.hMem <> 0
        DECLARE INTEGER GlobalFree IN kernel32 INTEGER
        = GlobalFree (THIS.hMem)
        THIS.hMem = 0
    ENDIF
ENDDEFINE  && pchar
 
 
 

User rating: 0/10 (0 votes)
Rate this code sample:
  • ~
2847 bytes  
Created: 2005-04-27 19:35:53  
Modified: 2005-04-28 09:32:16  
Visits in 7 days: 17  
Listed functions:
GlobalAlloc
GlobalFree
GlobalSize
PathFindOnPath
Printer friendly API declarations
My comment:
The second parameter of the PathFindOnPath is a memory address of null-terminated array of pointers to strings. Members of such array must be 4-byte numbers, format called UINT or DWORD. Each array member is an address of allocated in memory string that contains a directory name.

In this code sample, I want to make this function search for the file in three directories:
c:\dir1
c:\dir2
c:\documents\dir1



As the first step, I use the GlobalAlloc to allocate each directory name in memory. Three GlobalAlloc calls return three memory addresses.

To become a null-terminated array, these memory addresses must be put one after another to a memory buffer and padded by some null characters. As I said, each address must occupy four bytes.

So far these numbers are in FoxPro numeric format. This is something similar to managed objects in .Net. Technically I can not say how many bytes each numeric value takes and where exactly it resides in memory.

Somehow I need to convert them to 4-byte DWORDs. Three addresses, as three directories are searched, and five zero bytes, produce the total length of 17 bytes. I use num2dword() function to convert each number to 4-character string, which is the binary character representation of the DWORD format. In VFP9 you probably can use BINTOC() for this conversion.

So now I have created the binary character representation for the array -- a string 17 characters long. I could use the GlobalAlloc again to allocate this string in memory and obtain its address.

But more elegant solution is declaring this parameter as STRING @ -- a reference to a string, cPathArray in the code above. That makes the FoxPro to take care about all memory operations: memory allocation, passing the pointer to the PathFindOnPath and releasing the memory in the end.

PChar class hides certain complexity of memory API calls. In particular, it calls the GlobalFree, whenever a PChar instance is destroyed or re-allocated, preventing memory leaks.
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-2015 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.5.43), the Open Source standard SQL database, AceHTML Freeware Version 4, freeware HTML Editor of choice.   Hosted by Korax Online Inc.
Last Topics Visited (54.162.93.137)
1.98 min.Function: 'CryptDestroyKey'
Google
Advertise here!