Modula-2 home

  Home  
  Tutorial  
  Win32 API  
  Reference  
  Projects  
  Source code  
  Links  
Shows printer-friendly 
version in new window  

 

How to use the Wininet library to retrieve information from the Internet

For Stony Brook Modula-2

By Frank Schoonjans (frank.schoonjans@ugent.be)

 

We have (partially) converted Microsoft's Wininet headers to a Modula-2 definition file. In order to successfully retrieve files using this library, the following steps are required:

  1. Initialize the WinInet API by calling the InternetOpen function
  2. Connect to a site and retrieve a handle to the open connection by using the InternetConnect function
  3. Use InternetReadFile to read from the site
  4. Close the connection

The download wininet.zip contains a complete Stony Brook project including wininet.lib, wininet.def and a test program file inettest.mod.

This is the inettest.mod test program that illustrates the use of the library. The program retrieves the latest version number of our MedCalc program. This information can be used to inform the user of a possible availability of a software update.

MODULE inettest;

(* Test Wininet library - Frank Schoonjans - http://www.modula2.org *)

FROM SYSTEM    IMPORT FUNC,ADDRESS;
FROM WINX      IMPORT NIL_STR;
FROM ExStorage IMPORT ReALLOCATE;
FROM Terminal  IMPORT WriteString,WriteLn,ReadChar;
FROM MemUtils  IMPORT MoveMem;
FROM Wininet   IMPORT HINTERNET,InternetOpen,InternetOpenUrl,
                      InternetReadFile,InternetCloseHandle,
                      INTERNET_OPEN_TYPE_PRECONFIG,INTERNET_FLAG_RELOAD;

TYPE StringPointer = POINTER TO ARRAY OF CHAR;

PROCEDURE ReadFile(url : ARRAY OF CHAR;
                   VAR buf : StringPointer; VAR size : CARDINAL) : BOOLEAN;

VAR hNet,hUrlFile : HINTERNET;
    inbuf   : ARRAY [0..255] OF CHAR;
    count   : CARDINAL;
    ok      : BOOLEAN;
    retries : CARDINAL;
    osz     : CARDINAL;
BEGIN
    hNet:=InternetOpen("itest",INTERNET_OPEN_TYPE_PRECONFIG,
                       NIL_STR,NIL_STR,0);
    IF hNet=NIL THEN
      RETURN FALSE
    END;
    hUrlFile:=InternetOpenUrl(hNet,url,NIL_STR,0,INTERNET_FLAG_RELOAD,0);
    IF hUrlFile=NIL THEN
      FUNC InternetCloseHandle(hNet);
      RETURN FALSE
    END;
    buf:=NIL; size:=0; ok:=TRUE;
    LOOP
      retries:=0;
      REPEAT
        INC(retries);
        IF retries>50 THEN ok:=FALSE; EXIT END;
      UNTIL InternetReadFile(hUrlFile,inbuf,SIZE(inbuf)-1,count);
      IF count=0 THEN EXIT END;
      osz:=size;
      INC(size,count);
      ReALLOCATE(buf:ADDRESS,size);
      MoveMem(buf^[osz],inbuf,count);
    END;
    FUNC InternetCloseHandle(hUrlFile);
    FUNC InternetCloseHandle(hNet);
    RETURN ok;
END ReadFile;

VAR strptr : StringPointer;
    size   : CARDINAL;
    url    : ARRAY [0..80] OF CHAR;
BEGIN
    WriteString("INETTEST"); WriteLn; WriteLn;
    url:="http://www.medcalc.be/update/currentversion.txt";
    IF ReadFile(url,strptr,size) THEN
      ReALLOCATE(strptr:ADDRESS,size+1);
      strptr^[size]:=00C;
      WriteString("Current version = "); WriteString(strptr^); WriteLn;
    END;
    FUNC ReadChar();
END inettest.