x86dos - SQL Server - DB-Library for Microsoft SQL Server

Advanced DB-Library for DOS with DPMI16 - breaking DOS 640K memory barrier

Comments

Abstract:

Though Microsoft DB-Library for DOS was designed to be run in real mode, it is possible to use it in DPMI applications if a valid protected-mode INT 62H service is provided. In this article, we present a protected-mode INT 62H implementation that translates INT 62H calls back to valid real mode ones by using DPMI services. Also examples are provided for Borland C++ 4.52/DPMI16 and Visual C++ 1.52/PharLap 286.

Technical points:

Requirements:

1. DOS (pure DOS, DosBox with SQL Server support or 32 bit Windows DOS VM etc.)

Here you can find a copy of my build of DosBox (with SQL Server support):
DOS/DosBox advanced tools

2. DB-Library SDK from Microsoft SQL Server 6.X.
You can get Microsoft SQL Server 6.X on Vetusware and extract DB-Library SDK out of it. Some minor changes to .h files and workarounds for link errors are required for it to be usable in Borland C++ 4.52; so I suggest you download the fixed DB-Library SDK from the following local link: DB-Library SDK, then unpack the downloaded zip file to a folder (let it be '\dev\sdks\sqlsdk') and set the include and lib environment variables as follows:
    set path=%path%;\tools\sqltools;\dev\sdks\sqlsdk
    set include=%include%;\DEV\SDKS\sqlsdk
    set lib=%lib%;\dev\sdks\sqlsdk
3. DB-Library TSRs from Microsoft SQL Server 6.X.
You can find Microsoft SQL Server 6.X on Vetusware and extract DB-Library TSRs out of it. Or you can download DB-Library TSRs from the local link:DB-Library TSRs.
Then unpack the downloaded zip to a folder (let it be '\tools\sqltools') and set the path environment variable as follows:
    set path=%path%;\tools\sqltools;\dev\sdks\sqlsdk
    set include=%include%;\DEV\SDKS\sqlsdk
    set lib=%lib%;\dev\sdks\sqlsdk
4. A Microsoft SQL Server instance (Anything from SQL 4.2 up to SQL 2016).
5. Classic Pubs Sample database.
Local download link:Pubs Sample database.
After the Pubs database downloaded, unpack it to a folder, then attach it to the above SQL Server instance and set up permissions correctly. Also make sure that Named-pipe protocol is enabled on SQL Server.
6. A C/C++ Compiler.
If you are planning to use Microsoft Visual C++:
a). Microsoft Visual C++ 1.52 which can be found on Vetusware..
b). PharLap 286 Lite for Microsoft Visual C++ 1.52.
You can find it on Vetusware. Or you can download it from the the local link:PharLap 286 LIte, then unpack it to a folder such as pharlap under Visual C++ installation as shown in photo below:

And then set PATH/INCLUDE/LIB as follows:
        set path=%path%;\msvc\pharlap\bin
        set include=%include%;\msvc\pharlap\include
        set lib=%lib%;\msvc\pharlap\msvc\lib
c). MASM 6.X.
Again you can find it on Vetusware, or download a copy from local link: MASM 6.X, then unpack it to a folder (let it be \DEV\MASM611) and set many environment variables as follows:
    SET PATH=\DEV\MASM611\BIN;\DEV\MASM611\BINR;%PATH%
    SET LIB=\DEV\MASM611\LIB;\DEV\SDKS\LIB\UCRSTD36\LIB
    SET INCLUDE=\DEV\MASM611\INCLUDE;\DEV\SDKS\LIB\UCRSTD36\INCLUDE
    SET INIT=\DEV\MASM611\INIT
    SET HELPFILES=\DEV\MASM611\HELP\*.HLP
    SET ASMEX=\DEV\MASM611\SAMPLES
    SET TMP=\TEMP
If you are going to use Borland C++:
a). Borland C++ 4.52 which you can get from Vetusware too.
b). Borland Power Pack for DOS.
Again you can find it on Vetusware; or download a copy from local link: Borland Power Pack for DOS, then install it to the above Borland C++ 4.52 installation.

While the above requirements seem complex, if you have ever worked with BC++/DPMI or Microsoft C++/PharLap before, those are too obvious to explain.

The problem

For a database application DOS's 640K memory is really not so much, naturally we want to move it to protected mode by recompiling with DPMI support-- only if DB-Library is usable in protected mode. The good news is that DB-Library actually cares or knows nothing about real or protected mode, it just invokes Int 62H service provided by TSRs like DBNMPIPE.EXE etc. to access SQL Server database. As long as a valid int 62H service (in real or protected mode) exists, DB-Library will work as it should.

The problem is that Microsoft's TSR provides only real mode Int 62H service and leaves Int 62H being uninitialized in in protected mode, which means DB-Library linked into a DPMI application gets no int 62H service; hence, no SQL Server access for the DPMI application.

The solution

To overcome this empty Int 62H obstacle, what we are going to do here is to provide our own int 62H handler for DB-Library in protected mode as shown in the following drawings:

As you can see from the above photo, our Int 62H will translate protected mode calls/results from/back DB-Library to/from real mode ones. Since there is no public document on Int 62H, we have to reverse-engineer the calling protocol Int 62H uses. This can be done by extracting *.obj files for individual routines from a DB-Library .lib file and decompiling these *.obj files to *.asm ones. Then examine the assembly code to find the patterns utill the calling protocol gets worked out. But I will not walk you through the process of the reverse-engineering here and you can just use my findings. I did the DB-Library analyzing and created the protected-mode Int 62H handler about 10 year ago; the disassembler used was IDA Pro that had done a remarkable job in identifying C library routines and structures being specific to a certain compiler.

The code

I put the new Int 62H handler in 3 files: dbdpmihd.asm and dbdpmi.c/dbdpmi.h. To use the protected-mode Int 64H handler in a DPMI application, add these 3 files to its project and call DBInitDPMI()/DBUninitDPMI() at the beginning/ending of main(); that is the main() of a DPIM DB-Library application looks similar to this:


    int	main ()
    {
        //....
        //Initialize our INT 62H
        if(!DBInitDPMI())
        {
            printf("Failed to initialize Int 62H with DPMI\n");
            exit(1);
        }
        //....
    
        //Close the connection and	exit
        dbexit();
        //Uninitialize our INT 62H after dbexit()
        DBUninitDPMI();
    }

As you can see, adding a few lines to your DPMI application makes it ready to grab data from SQL Server databases.

The sample to query Microsoft SQL Server database in DPMI mode

The sample file is ptsq.c. To make it simple, I have used Trusted Connection in the source code, so no need to input password and user id for SQL Server. Before we can test it, make sure database Pubs works.

To check if Pubs is accessible, run the follow commands at Prompt:


        dbnmpipe.exe
        isql -E
        select @@version;
        go
        use pubs;
        go
        select * from authors where au_fname like 'A%';
        go

Now we can move forward to building the sample.

The complete source code

Download link for the Protected mode Int 62H handler and samples:The complete source code

My notes

I created the sample a decade ago (when I could hardly write in English), so even for me it took some time to get familiar with the code. If there is anything unclear in either the code or this article, leave a comment below and I will update it as I can.
(2016.1.25)