Using Win32 functions in Visual FoxPro Image Gallery
Code examples:
How to hot-track menu item selection in top-level form (requires VFP9)
How to obtain the number of rows affected by remote UPDATE, INSERT or DELETE statement
Obtaining a handle to the desktop associated with the calling thread
Obtaining addresses for the adapters on the local computer (Win XP/2003/Vista)
Opening access to the Microsoft Internet functions for the application
Reading and setting Environment variables
Reading and setting the priority class values for the current process and thread
Deleting a file stored on the FTP server
GDI+: printing image file
How to print FoxPro form -- II
Number of clipboard formats available
Retrieving list of Global Atom names
Retrieving System Error message strings
Winsock: how to retrieve the protocol information corresponding to a protocol name
FindText -- the hopeless and useless Common Dialog
Saving HKEY_LOCAL_MACHINE\\Software\\ODBC Registry Entries to an XML file
Uploading file to the FTP server using InternetWriteFile
Using Beep and Sleep functions to make the old tin buzz sing (WinNT only?)
Converting long file names to the short format and vice versa
How to generate GUID values
Retrieving information about the specified icon
Retrieving long values associated with the class of the VFP window
Retrieving Printer Device Context using PrintDlg function
Creating a directory on the FTP
Winsock: retrieving information from a host database for a given host name

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:
An example of return for this sample code:
Host name: microsoft.com
IP 1: 207.46.249.190
IP 2: 207.46.249.222
IP 3: 207.46.249.27
IP 4: 207.46.134.155
IP 5: 207.46.134.190
IP 6: 207.46.134.222
 
*| struct HOSTENT {
*|   char FAR *       h_name;       0:4
*|   char FAR * FAR * h_aliases;    4:4
*|   short            h_addrtype;   8:2
*|   short            h_length;    10:2
*|   char FAR * FAR * h_addr_list; 12:4
*| }; total = 16 bytes
#DEFINE HOSTENT_SIZE  16
DO decl
 
IF Not InitWinsock()
    ? "WSAStartup failed"
    RETURN
ENDIF
 
LOCAL nHost, lcHOSTENT, lnNamePtr, lnAddrlistPtr
nHost = gethostbyname("microsoft.com")
 
IF nHost <> 0
    * copying data from the HOSTENT structure to a VFP string
    lcHOSTENT = GetMemBuf(nHost, HOSTENT_SIZE)
 
    * a pointer to the host name
    lnNamePtr = buf2dword(SUBSTR(lcHOSTENT, 1,4))
    ? "Host name:", GetMemStr(lnNamePtr)
 
    * a pointer to a null-terminated list of IP addresses
    lnAddrlistPtr = buf2dword(SUBSTR(lcHOSTENT, 13,4))
 
    * displaying IP addresses for this host
    = DisplayIPs(lnAddrlistPtr)
ELSE
* 10093 = WSANOTINITIALISED
* Either the application has not called WSAStartup, or WSAStartup failed
    ? "Error:", GetLastError()
ENDIF
= WSACleanup()
* End of Main
 
PROCEDURE  DisplayIPs (lnAddrlistPtr)
* retrieving IP addresses from the list
 
    LOCAL lnElementPtr, lnIPcount, lcDataAddress, lnDataAddress,;
        lcIPAddrBuf
 
    lnIPcount = 0  && list elements retrieved
    lnElementPtr = lnAddrlistPtr  && first member on the list
 
    * scanning the list one member by another
    * until a null member found
    DO WHILE .T.
        * the list member contains a DWORD memory address
        lcDataAddress = GetMemBuf(lnElementPtr, 4)
        lnDataAddress = buf2dword(lcDataAddress)
 
        IF lnDataAddress = 0
        * the last member on this list is null
            EXIT
        ENDIF
 
        * retrieving a DWORD with an IP address
        lcIPAddrBuf = GetMemBuf (lnDataAddress, 4)
 
        lnIPcount = lnIPcount + 1
        ? "IP " + LTRIM(STR(lnIPcount)) + ":",;
            inet_ntoa(buf2dword(lcIPAddrBuf))
 
        * shifting to the next element on the list
        lnElementPtr = lnElementPtr + 4   && DWORD
    ENDDO
RETURN
 
FUNCTION GetMemStr (lnAddr)
* returning data from a memory block as a VFP string
#DEFINE MEMSTR_BUFSIZE   128
    LOCAL lcBuffer
    lcBuffer = Repli(Chr(0), MEMSTR_BUFSIZE)
    = CopyMemory (@lcBuffer, lnAddr, MEMSTR_BUFSIZE)
RETURN SUBSTR(lcBuffer, 1, AT(Chr(0),lcBuffer)-1)
 
FUNCTION GetMemBuf (lnAddr, lnBufsize)
    LOCAL lcBuffer
    lcBuffer = Repli(Chr(0), lnBufsize)
    = CopyMemory (@lcBuffer, lnAddr, lnBufsize)
RETURN lcBuffer
 
FUNCTION InitWinsock()
#DEFINE WSADATA_SIZE 398
#DEFINE WS_VERSION 0x0202
    LOCAL lcWSADATA, lnInitResult
    lcWSADATA = Repli(Chr(0), WSADATA_SIZE)
    lnInitResult = WSAStartup (WS_VERSION, @lcWSADATA)
RETURN (lnInitResult = 0)
 
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)
 
PROCEDURE decl
    DECLARE INTEGER GetLastError IN kernel32
    DECLARE INTEGER WSAStartup IN ws2_32 INTEGER wVerRq, STRING @lpWSAData
    DECLARE INTEGER WSACleanup IN ws2_32
    DECLARE INTEGER gethostbyname IN ws2_32 STRING hostname
    DECLARE STRING inet_ntoa IN ws2_32 INTEGER in_addr
 
    DECLARE RtlMoveMemory IN kernel32 As CopyMemory;
        STRING @Dest, INTEGER Src, INTEGER nLength
 
 
 

User rating: 0/10 (0 votes)
Rate this code sample:
  • ~
3215 bytes  
Created: 2001-12-15 09:33:39  
Modified: 2008-12-03 15:26:21  
Visits in 7 days: 64  
Listed functions:
CopyMemory
gethostbyname
GetLastError
inet_ntoa
WSACleanup
WSAStartup
Printer friendly API declarations
My comment:


#kwd: sln_winsock.
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-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 (50.16.17.90)
3 sec.Example: 'Placing an arbitrary rectangular area of main VFP window on the Clipboard'
6 sec.Example: 'How to make a VFP form fading out when released (GDI version)'
6.17 hrs.Example: 'WAV file player'
 Example: 'Placing an arbitrary rectangular area of main VFP window on the Clipboard'
7.22 hrs.Function: 'GlobalGetAtomName'
 Function: 'ChangeDisplaySettings'
Function group: 'Device Context'
 Function: 'HeapAlloc'
Function group: 'Memory Management'
8.15 hrs.Example: 'Joining local computer to a domain (XP/2000)'
 Function: 'GdipGetPageUnit'
8.93 hrs.Function: 'SetLayeredWindowAttributes'
Google
Advertise here!