Win32 API samples

This collection of code snippets used to be hosted on www.global-shared.com.
It is an old collection: it all began in 1996 or 1997, when Progress 8 was new and many of us were still using Windows 3.1 !!
Unlike wine or paintings, program code doesn't get better when it ages. You can find fragments that can be improved because Microsoft continuously expands their API, or are outdated because Progress has added features to the ABL so we don't need to use the WIN32 API anymore.


Mailslots

Introduction

There are several techniques for Inter Process Communication (IPC) like DDE, pipes, atoms, sockets and mailslots. Each technique has its own characteristics; this topic concentrates on Mailslots.
Mailslots offer an easy way for a process to broadcast a message to several other processes at once. A Mailslot is a pseudo-file created by one particular process, known as the Mailslot server. The Maislot server can read messages from the mailslot.
Other processes, known as Mailslot clients, can write messages to mailslots owned by Mailslot servers. A Mailslot client is not allowed to read from another process' mailslot.


Ping

by Marian EDU

Marian made a function to check if the host is alive or not. He wrote:

"if you want to get funky you can use gethostbyname to resolve the address, and use more options on IcmpSendEcho. but I don't see no utility in this. you probably want to check if a specific service is running on that machine. so it's more easy to use sockets and attempt to connect to the specific service."

/******************************************************************************
    Program:        ping.p
    
    Written By:     Marian EDU
    Written On:     September 2002

The IP address of your local computer

by Bill Prew

The following procedure returns the ip adress and the host name for the local computer.

&SCOPED-DEFINE WSADESCRIPTION_LEN       256
&SCOPED-DEFINE WSASYS_STATUS_LEN        128
 
&SCOPED-DEFINE WSADATA_VERSION_LOW        1    /* WORD(2)  */
&SCOPED-DEFINE WSADATA_VERSION_HIGH       3    /* WORD(2)  */
&SCOPED-DEFINE WSADATA_DESCRIPTION        5    /* CHAR(WSADESCRIPTION_LEN + 1) */ 
&SCOPED-DEFINE WSADATA_SYSTEM_STATUS    262    /* CHAR(WSASYS_STATUS_LEN + 1)  */ 
&SCOPED-DEFINE WSADATA_MAX_SOCKETS      391    /* SHORT(4) */ 
&SCOPED-DEFINE WSADATA_MAX_UDP          395    /* SHORT(4) */

Get MAC address

by Maurits van Rijnen

Every network adapter has a unique id: the MAC address. The MAC address may be used to identify a computer, at least for a while until its network adapter is replaced or until someone decides to flash a new MAC address into the adapter. Also, be carefull if the computer has more than one network adapter.
To read the MAC address you should read the ARP cache, but that's very complicated. Let's forget about the ARP cache.
Maurits found a neat "workaround" based on UuidCreate:
UuidCreate creates a new universally unique identifier, which can be used for creating new ActiveX controls and so on. Because it has to be universally unique it is based on a MAC address and probably some datetime-value (and perhaps some other hardware metrics). Maurits recognized how to read the MAC address from the uuid :


File Transfer Protocol (FTP)

by Todd G. Nist

Program source is available for download: winftp.w. Modified 20 July 2000.
This program demonstrates common FTP functionality: connect to an FTP server, list directory contents, change the current directory and of course: get, put and delete files.
See also FileDownload.

The code functions as follows:
* By default, it will connect to ftp.progress.com when you press the Connect FTP button.
* Once on the site, if you double click a directory in the directory selection list, it will open that directory and display the contents.


File download

by Todd G. Nist

This program utilizes the standard DoFileDownload API.
On the occasions when you simply need to download one file, this routine will display the IE file download dialog. The nice thing is that the dialog takes care of all aspects of the interface, including the progressbar, animation and statistics. This code can easily be integrated into any application requiring this type of functionality.
The program has been tested under Windows 2000 Server/Professional.
----
API-procedures used in this example are listed here to be included in the search index:


Networking

.


Watched folder

by Gordon Campbell

I created a test program that illustrates how to 'watch a folder' in MS
Windows.  The code can be downloaded at:
http://www.epro-sys.com/samples/watchfolder.w
The sample watches a directory called c:\watch\in and via an IP
(ProcessFile) moves the contents to c:\watch\out.
The ProcessFile IP can be modified to process a file in the 'in' directory.
You could use this to automate conversion of text files to PDF via
PDFinclude or automate the sending of documents to e-mail addresses .... or
whatever automated process you would consider when dealing with a file.

Format a floppy disk (or hard drive)

by Stuart Morris

This allows you to call a standard system dialog for formatting Floppy disks and Hard drives etc.

/* S.A.Morris - 01/02/2000 */
 
&GLOB SHFD_CAPACITY_DEFAULT 0 /* default drive capacity                 */
&GLOB SHFD_CAPACITY_360     3 /* 360KB, applies to 5.25" drives only    */
&GLOB SHFD_CAPACITY_720     5 /* 720KB, applies to 3.5" drives only     */
&GLOB SHFD_FORMAT_QUICK     0 /* quick format                           */
&GLOB SHFD_FORMAT_FULL      1 /* full format                            */
&GLOB SHFD_FORMAT_SYSONLY   2 /* copies system files only (Win95 Only!) */

SHFileOperation

by Todd G. Nist

Program source is available for download: w-SHFileOp.p
By calling SHFileOperation, one can leverage the existing dialogs for moving files and providing user feed back as to the status of the process all with just one call. It is a fairly simple demo, where it will ask for a directory to be copied, accepting wild cards, a destination directory, a delete directory file specification and a title for the dialog box. Then by invoking the call to SHFileOperation the standard windows dialog box showing the folders and the flying documents will be displayed, this is making the assumption that the information being copied is large enough to allow the dialog to be created. Also, if the files already exist, it will bring up the standard dialog asking if you wish to over write, the size of the file and the date of the files in question. Finally, if delete files is chosen, it will remove the files from the "delete file spec" and bring up the same general dialogs.


Select a filename using the SHAutoComplete feature

by Simon Sweetman

This is a demo program that uses SHAutoComplete from shlwapi.dll to enable the Windows filename auto complete (like the Win2K file-open dialog) on an editor field, the same call can be used on a fill-in but it doesn?t work real well with progress. The editor version works fine except for TAB and BACK-TAB which seem to be trapped by SHAutoComplete before they get to progress.

Attachments

autocomp.w.zip


Reading and setting file attributes

Let's start with a couple of definitions:

&GLOBAL-DEFINE FILE_ATTRIBUTE_READONLY 1
&GLOBAL-DEFINE FILE_ATTRIBUTE_HIDDEN 2
&GLOBAL-DEFINE FILE_ATTRIBUTE_SYSTEM 4
&GLOBAL-DEFINE FILE_ATTRIBUTE_DIRECTORY 16
&GLOBAL-DEFINE FILE_ATTRIBUTE_ARCHIVE 32
&GLOBAL-DEFINE FILE_ATTRIBUTE_NORMAL 128
&GLOBAL-DEFINE FILE_ATTRIBUTE_COMPRESSED 2048
 
PROCEDURE SetFileAttributesA EXTERNAL "kernel32" :
   DEFINE INPUT PARAMETER lpFilename AS CHARACTER.
   DEFINE INPUT PARAMETER dwFileAttributes AS LONG.
END.
 
PROCEDURE GetFileAttributesA EXTERNAL "kernel32" :
   DEFINE INPUT PARAMETER lpFilename AS CHARACTER.

GetSpecialFolder

  MESSAGE GetSpecialFolder({&CSIDL_SYSTEM}) VIEW-AS ALERT-BOX.

will return "c:\windows\system" or "c:\winnt\system32" or whatever is valid on your PC.
There are different implementations of this function. This page shows one by Stuart Morris and one by Jan Verley.

Using SHGetSpecialFolderLocation

by Stuart Morris, stuart@IBS-PUBLIC-SERVICES.co.uk

PROCEDURE SHGetPathFromIDListA EXTERNAL "shell32.dll":U :
  DEFINE INPUT  PARAMETER pidl    AS  LONG.
  DEFINE OUTPUT PARAMETER pszPath AS  CHARACTER.
  DEFINE RETURN PARAMETER iResult AS  LONG.
END PROCEDURE.

GetShortPathName

function GetShortPathName retrieves the 8.3 pathname for an existing long pathname.

 
PROCEDURE GetShortPathNameA EXTERNAL "kernel32.dll" :
  DEFINE INPUT  PARAMETER  lpszLongPath  AS CHARACTER.
  DEFINE OUTPUT PARAMETER  lpszShortPath AS CHARACTER.
  DEFINE INPUT  PARAMETER  cchBuffer     AS LONG.
  DEFINE RETURN PARAMETER  ReturnValue   AS LONG.
END PROCEDURE.
 
DEFINE VARIABLE longname AS CHARACTER NO-UNDO.
DEFINE VARIABLE shortname AS CHARACTER NO-UNDO.
DEFINE VARIABLE returnvalue AS INTEGER NO-UNDO.
 
&GLOB shortsize 68
 
longname = "C:\Program Files\VendorName\Some Application\Data\Monthly Revenue.txt".

Select multiple files

by Scott Anderson, Stuart Morris and Jurjen
"SYSTEM-DIALOG GET-FILE" allows to select only one filename. When you want to select multiple filenames you can call API function GetOpenFileNameA, specifying the OFN_ALLOWMULTISELECT flag.
Procedure SelectMultipleFileNames uses GetOpenFileNameA for the purpose of selecting multiple filenames. The first listing shows how to call the procedure, the second listing shows the implementation of the procedure.
The parameters are:
* FilterList
a list of filters separated by 'pipe'-symbols ( "|" ). Each individual filter is a description, followed by a pipe-symbol, followed by a semicolon-separated list of wildcards. The format of the description is not important but by convention it should be a text followed by the list of wildcards between brackets. The first filter is the default.


#
Syndicate content