How To Use the Win32 Registry from 16-bit Windows-Based App

Last reviewed: January 15, 1997
Article ID: Q137378
The information in this article applies to:
  • Microsoft Win32 Software Development KIT (SDK) versions 3.1x, 3.5, 3.51, 4.0

SUMMARY

By obtaining access to all Win32-based registry APIs, a 16-bit Windows-based application can also gain full access to the Win32 registry. Such access would be based on Generic Thunking to ensure portability between Windows NT and Windows 95.

MORE INFORMATION

A 16-bit Windows-based application must implement a generic thunk function for each Win32 API required to access the Win32 registry. To achieve the greatest flexibility, you need to implement these new functions in a 16-bit Windows-based DLL that can be implicitly linked to a 16-bit Windows-based application. In Windows NT and Windows 95, all Win32 registry APIs are exported from Advapi.dll.

For a 16-bit Windows-based application to access the Win32 registry, it must first define the predefined reserved handle value (HKEY_LOCAL_MACHINE) as defined by the Win32 environment. These predefined reserved handle values are located in the Winreg.h file in the Win32 SDK.

The following demonstrates a sample technique used to perform registry thunking:

// required includes
#include "wownt16.h"     // available from Win32 SDK
#include "shellapi.h"    // 16-bit Windows header

// As defined by the Win32 SDK in Winreg.h
#define HKEY_CURRENT_USER           (( HKEY ) 0x80000001 )

HKEY hOpen; DWORD rc;

if ( ERROR_SUCCESS ==

      (rc=RegOpenKey(HKEY_CURRENT_USER, "Control Panel\\Desktop", &hOpen)))
      {
         DWORD dwType, cbData;
         BYTE   bData[1000];
         cbData = 1000;

         if ( (rc =
         RegQueryValueEx(hOpen, "BorderWidth", NULL,
                              &dwType, bData, &cbData ))
              == ERROR_SUCCESS )
          {
          // used retreived data
          MessageBox(NULL, "Value Retreived","Information",MB_OK);
          }
          else
             MessageBox(NULL, "RegQueryValueEx Failed","Error",MB_OK);
             RegCloseKey(hOpen);
      }

// This code should be placed in a DLL to allow for maximum flexibility.
// When the full conversion to 32-bit is complete, the thunking library
// need not be included.

LONG RegQueryValueEx(
    HKEY  hKey,
    LPSTR  lpszValueName,
    LPDWORD  lpdwReserved,
    LPDWORD  lpdwType,
    LPBYTE  lpbData,
    LPDWORD  lpcbData
   )
{
    DWORD pFn;
    DWORD dwResult = ERROR_ACCESS_DENIED; //random error if fail
    DWORD hAdvApi32;

    hAdvApi32=LoadLibraryEx32W("ADVAPI32.DLL", NULL, 0);

    if ((DWORD)0!=hAdvApi32)
     {
           // call ANSI version
      pFn=GetProcAddress32W(hAdvApi32, "RegQueryValueExA");
      if ((DWORD)0!=pFn)
       {
        dwResult=CallProcEx32W(
               CPEX_DEST_STDCALL | 6, // standard function call with
                                      // six parameters
               0x3e,                  // Identify what parameters
                                      //(addresses) must be translated
               pFn,                   // function pointer
               hKey,                  // open key
               lpszValueName,         // value to query
               lpdwReserved,          // reserved for future use
               lpdwType,              // value type
               lpbData,               // value data
               lpcbData               // value data length
               );
       }
     }
    if (hAdvApi32)
                 FreeLibrary32W(hAdvApi32);

              return(dwResult);
}

To include the generic thunk APIs, the 16-bit app needs to add the following to its module definition (.DEF) file:

IMPORTS

        _CallProcEx32W           = KERNEL.518
        LoadLibraryEx32W         = KERNEL.513
        FreeLibrary32W           = KERNEL.514
        GetProcAddress32W        = KERNEL.515


KBCategory: kbprg kbcode
KBSubcategory: BseRegistry
Additional reference words: 3.50 3.51 4.00


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: January 15, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.