Using Win32 functions in Visual FoxPro Image Gallery
Memory Management
..msdn
CopyMemory
FillMemory
GetProcessHeap
GetProcessHeaps
GlobalAlloc
GlobalFree
GlobalLock
GlobalMemoryStatus
GlobalReAlloc
GlobalSize
GlobalUnlock
HeapAlloc
HeapCompact
HeapFree
HeapLock
HeapReAlloc
HeapSize
HeapUnlock
HeapValidate
HeapWalk
LocalAlloc
LocalFree
LocalSize
VirtualAllocEx
VirtualFreeEx
ZeroMemory
Code examples:
Adding and deleting Scheduled Tasks using NetScheduleJob API functions
Adding and deleting User Accounts
Adding printer to the list of supported printers for the specified server
Adding user-defined items to the Control Menu of VFP form (requires VFP9)
Attaching menu to a top-level form
Browsing Windows Known Folders (Special Folders)
Changing pitch and speed of a wave file
Class for sound recording
Compressing and decompressing files with Windows API Runtime Library routines
Copying strings through the global memory block
Creating the Open dialog box to specify the drive, directory, and name of a file to open
Creating the Save dialog box to specify the drive, directory, and name of a file to save
Custom HttpRequest class (WinHTTP)
Deleting files into the Recycle Bin
Displaying dimmed window behind VFP top-level form
Displaying standard progress dialog box when copying files
Displaying system dialog that selects a folder
Dynamic strings implemented through VFP Custom class
Enhanced GetFont dialog
Enumerating forms supported by a specified printer
Enumerating network resources
Enumerating ports that are available for printing on a specified server
Enumerating print jobs and retrieving information for default printer (JOB_INFO_1 structures)
Enumerating printer drivers installed
Extensible Storage Engine class library
FindText -- the hopeless and useless Common Dialog
GDI+: reading and writing metadata in JPEG and TIFF files
How to assemble an array of strings and pass it to external function
How to browse and connect to printers on a network (WinNT)
How to convert a bitmap file to monochrome format (1 bpp)
How to delete IE cookies, clear IE history and delete files in Temporary Internet Files directory
How to display a user-defined icon in the MessageBox dialog
How to display advanced Task Dialog (Vista)
How to display the Print property sheet
How to display the Properties dialog box for a file (ShellExecuteEx)
How to enumerate cookies and URL History entries in the cache of the local computer
How to enumerate, add and delete shares on the local computer (WinNT/XP)
How to prevent users from accessing the Windows Desktop and from switching to other applications
How to print a bitmap file
How to print FoxPro form
How to remove a directory that is not empty
How to write and read Window Properties for the specified window
Loading a string resource from an executable file
MapiSendMail class for Visual FoxPro application
Mapping and disconnecting network drives
Obtaining addresses for the adapters on the local computer (Win XP/2003/Vista)
Obtaining list of tables stored in an ODBC Data Source
Passing data records between VFP applications via the Clipboard
Playing WAV sounds simultaneously
Printing Image File, programmatically set print page orientation to landscape
Quering Audio Mixer Device
Reading entries from Event logs
Reading the structure of VFP main menu
Sending email messages with Simple MAPI
Shortcut Menu Class
Simple printer queue monitor: deletes, pauses, resumes print jobs for local printer
Starting a dialog box for connecting to network resources and passing input parameters
Storing content of the Clipboard to a bitmap file
Storing screen shot of a form to bitmap file
Subclassing CommandButton control to create BackColor property
URL: splitting into its component parts
Using Change Notification Objects to monitor changes to the printer or print server
Using EnumPrinters function to enumerate locally installed printers
Using FillMemory
Using the ChooseColor function
Using WM_COPYDATA for interprocess communication (VFP9)
Verifying a file using the Authenticode policy provider
Vertical Label control
WAV file player
Windows Shell Icons displayed and exported to ICO files (Vista)
Winsock: connecting to a news server (NNTP, port 119)
Writing entries to custom Event Log
Copying strings through the global memory block

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
 
#DEFINE GMEM_FIXED     0
#DEFINE GMEM_MOVEABLE  2
DO decl
 
    LOCAL lcTarget, lcSource, lnAllocSize, hMem, lnAddr
 
    lcSource = "Copying a string through an allocated memory object"
    lnAllocSize = Len(lcSource)
    lcTarget = SPACE(lnAllocSize) && the destination string is empty
 
    ? "*** Before"
    ? "Source string:     ", lcSource
    ? "Destination string:", lcTarget
    ?
 
    * allocating an amount of memory capable of storing
    * the source string (not Unicode)
    hMem = GlobalAlloc (GMEM_MOVEABLE, lnAllocSize)
 
    IF hMem <> 0
 
        * lock the memory block to get its address
        * WinNT: both can be identical
        lnAddr = GlobalLock (hMem)
        ? "Memory object handle:", hMem
        ? "Memory block address:", lnAddr
        ?
 
        DECLARE RtlMoveMemory IN kernel32 As String2Heap;
            INTEGER Destination, STRING @ Source,;
            INTEGER nLength
 
        * copying source string -> allocated memory object
        = String2Heap (lnAddr, @lcSource, lnAllocSize)
 
        * every time you must re-declare this function
        * because this time you call with different parameter types
        DECLARE RtlMoveMemory IN kernel32 As Heap2String;
            STRING @ Destination, INTEGER Source,;
            INTEGER nLength
 
        * copying allocated memory object -> destination string
        = Heap2String (@lcTarget, lnAddr, lnAllocSize)
 
        * testing the results
        ? "*** After"
        ? "Source string:     ", lcSource
        ? "Destination string:", lcTarget
 
        * releasing the medium
        = GlobalUnlock (hMem)
        = GlobalFree (hMem)
    ENDIF
 
PROCEDURE  decl
    DECLARE INTEGER GlobalFree IN kernel32 INTEGER hMem
 
    DECLARE INTEGER GlobalAlloc IN kernel32;
        INTEGER wFlags, INTEGER dwBytes
 
    DECLARE INTEGER GlobalLock IN kernel32 INTEGER hMem
    DECLARE INTEGER GlobalUnlock IN kernel32 INTEGER hMem
 
 

User rating: 0/10 (0 votes)
Rate this code sample:
  • ~
1760 bytes  
Created: 2001-09-19 12:00:00  
Modified: 2001-09-28 16:45:46  
Visits in 7 days: 73  
Listed functions:
GlobalAlloc
GlobalFree
GlobalLock
GlobalUnlock
Printer friendly API declarations
My comment:
This code demonstrates how to move binary data to and from global memory blocks. The source VFP string is copied into an allocated global memory block. Then content of this block is copied into the destination string. There is no direct usefulness in this code -- just a demonstration.

In some situations you need to pass to a structure (not to a function, that is easy) the 4-byte pointer to a string -- LPCSTR, or LPCTSTR.

As an example, to run some printer functions you must supply the DOCINFO structure:
typedef struct { 
int cbSize;
LPCTSTR lpszDocName;
LPCTSTR lpszOutput;
LPCTSTR lpszDatatype;
DWORD fwType;
} DOCINFO, *LPDOCINFO;

Within this structure lpszDocName is not a string, but a pointer to a string with a defined document name -- e.g. "My Printer Job".

That means, you must allocate this string in memory and pass the pointer to the structure. Using global memory functions makes this task possible. At that time -- to my knowledge -- there is no regular function in VFP to return a pointer to a string.

If GMEM_FIXED being used instead of GMEM_MOVEABLE you can avoid using the GlobalLock and GlobalUnlock couple. With GMEM_FIXED you receive a pointer comparing to a handle to the memory object for the GMEM_MOVEABLE.
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:
Burkhard | 2010-02-10 17:31:15
Hi! I'm using VFP 9 and played a little bit with the SYS(2600...) function. Pls have a look at the following code fragment. Maybe I'm wrong, but I think SYS(2600) is not exactly behaving like described in the docs!

DECLARE RtlMoveMemory IN kernel32 INTEGER, STRING @, INTEGER
DECLARE INTEGER GlobalAlloc IN kernel32;
  INTEGER wFlags,;
  INTEGER dwBytes
DECLARE INTEGER GlobalFree IN kernel32 INTEGER hMem

LOCAL hMem AS Integer && Memory handle returned by GlobalAlloc(GMEM_MOVEABLE,...)
LOCAL lpVoid AS Long && Memory pointer returned by GlobalAlloc(GMEM_FIXED,...) and others
LOCAL lcString AS String, lcString2 AS String, lnSize As Integer

*\\ Create some VFP strings. We don't have to zero-terminate ;
    them coz we do not process them any further using Win API ;
    calls written in C/C++
lcString = "This is a great VFP test string!"
lcString2= "This is another VFP test string!"
lnSize = LEN(m.lcString) && == LEN(m.lcString2) !!
clear

*\\ allocate fixed memory
#DEFINE GMEM_FIXED 0
lpVoid = GlobalAlloc(GMEM_FIXED, m.lnSize)
? "lpVoid", m.lpVoid && no handle but a pointer!
*//
*\\ move VFP's string into allocated memory block
? "RtlMoveMemory() : ", RtlMoveMemory(m.lpVoid, @m.lcString, m.lnSize)
*//
*\\ now get back our string content using VFP's SYS(2600) function
? SYS(2600, m.lpVoid, m.lnSize)
*//
*\\ free memory: GlobalFree() accepts a pointer in this case
lpVoid = GlobalFree(m.lpVoid)
? "lpVoid after GlobalFree(): ", m.lpVoid
*//
? "-------------------------------------"
*\\ now, let's try something different:
DECLARE INTEGER GlobalLock IN kernel32 INTEGER hMem
DECLARE INTEGER GlobalUnlock IN kernel32 INTEGER hMem
*\\ allocate moveable memory block
#DEFINE GMEM_MOVEABLE 2
hMem = GlobalAlloc(GMEM_MOVEABLE, m.lnSize)
? "hMem  ", m.hMem, "a handle"
*//
*\\ lock memory to get a pointer the starting address
lpVoid = GlobalLock(m.hMem)
? "lpVoid", m.lpVoid, "a pointer"
*//
*\\ again, move the string into allocated memory block now using a VFP Fn()
? SYS(2600, m.lpVoid, m.lnSize, m.lcString2) && write string content to memory block
*\\ Note the outcome of SYS(2600) above then read the online help for it! I thought;
    that SYS(2600) 1st reads (and returns) the addressed memory block then writes ;
    to it (if optional 4th argument is passed in). But that isn't true, obviously!
*//
*\\ get back our string content using VFP's SYS(2600)
? SYS(2600, m.lpVoid, m.lnSize)
*//
*\\ unlock memory before freeing it: GlobalUnlock() returns <0> if successful;
    this isn't really necessary here coz GlobalFree() unlocks the memory block anyway
? "GlobalUnlock()", GlobalUnlock(m.hMem)
*//
*\\ free memory: GlobalFree() accepts a handle in this case ;
    on success GlobalFree() returns <0>, otherwise the handle/pointer value passed in
m.hMem = GlobalFree(m.hMem)
*//
? "hMem after GlobalFree(): ", m.hMem

Copyright © 2001-2013 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.1.55-log), the Open Source standard SQL database, AceHTML Freeware Version 4, freeware HTML Editor of choice.   Hosted by Korax Online Inc.
Last Topics Visited (184.72.184.104)
2 sec.Example: 'Using GetFileSize'
7 sec.Example: 'Retrieving a handle to DLL and address of an exported function in it'
5.14 hrs.Function: 'PrintDlgEx'
5.15 hrs.Example: 'GDI+: copying to the Clipboard (a) image of active FoxPro window/form, (b) image file'
7.59 hrs.Example: 'How to detect if additional monitor is connected and active'
7.82 hrs.Example: 'Enumerating network interfaces on the local computer'
Language: 'C#'
 Function: 'GetDefaultPrinter'
Function group: 'Printing and Print Spooler'
8.86 hrs.Function: 'GetShellWindow'
10.16 hrs.Example: 'Using InternetSetFilePointer when resuming interrupted download from the Internet'
 Example: 'A class that encrypts and decrypts files using Cryptography API Functions'
Google
Advertise here!