//+---------------------------------------------------------------------------
//
//  Copyright (C) Digi International, Inc. 1998.
//
//  File:       D I G I U P G . C P P
//
//  Contents:   Digi DataFire ISDN component upgrade DLL for NT 5.0
//
//				Taken from sample code for OEM network component upgrade DLL
//				Author:     kumarp    16-October-97
//
//  Notes:		The upgrade DLL saves the registry information of an installed
//				Digi DataFire ISDN driver into an AnswerFile.  The information
//				is then restored to the registry as part of the upgrade process.
//				See the Windows NT 5.0 OEM Network Upgrade Support 
//				Specification from Microsoft.
//
//  Author:     Ruairi Meyler    11 Feb 1998
//
//----------------------------------------------------------------------------

#include "wtypes.h"

#include "pch.h"
#pragma hdrstop

#include "windows.h"


#include "dgupgex.h"
#include "dgupgex.cpp"
#include "upgrade.h"



static HMODULE g_hNetupgrdDll;

NetUpgradeAddSectionPrototype       g_pfnNuAddSection;
NetUpgradeAddLineToSectionPrototype g_pfnNuAddLineToSection;

VENDORINFO g_viOem =
{
    TEXT("Digi International Inc."),
    TEXT("(800) 344 4273"),
    TEXT("http://www.dgii.com"),
    TEXT("Please visit our website for further information")
};

EXTERN_C LONG __declspec(dllexport) __stdcall
PreUpgradeInitialize(IN  LPCTSTR         szWorkingDir,
                     IN  NetUpgradeInfo* pNetUpgradeInfo,
                     //OUT LPCTSTR*        szProductId,
                     OUT VENDORINFO*     pviVendorInfo,
                     OUT DWORD*          pdwFlags,
                     OUT LPVOID          pvReserved)
{
    DWORD dwError=ERROR_SUCCESS;

    // get function address of the two exported functions
    // for writing into the answerfile
    //
    g_hNetupgrdDll = GetModuleHandle(TEXT("netupgrd.dll"));
    
    if (g_hNetupgrdDll)
    {
        g_pfnNuAddSection =
            (NetUpgradeAddSectionPrototype)
            GetProcAddress(g_hNetupgrdDll, c_szNetUpgradeAddSection);

        g_pfnNuAddLineToSection =
            (NetUpgradeAddLineToSectionPrototype)
            GetProcAddress(g_hNetupgrdDll, c_szNetUpgradeAddLineToSection);

        if (!g_pfnNuAddSection || !g_pfnNuAddLineToSection)
        {
            // this should never occur
            //
            dwError = ERROR_CALL_NOT_IMPLEMENTED;
        }
    }
    
    return dwError;
}

EXTERN_C LONG __declspec(dllexport) __stdcall
DoPreUpgradeProcessing(IN   HWND    hParentWindow,
                       IN   HKEY    hkeyParams,
                       IN   LPCTSTR szPreNT5InfId,
                       IN   LPCTSTR szPreNT5Instance,
                       IN   LPCTSTR szNT5InfId,
                       IN   LPCTSTR szSectionName,
                       OUT  VENDORINFO* pviVendorInfo,
                       OUT  DWORD*  pdwFlags,
                       IN   LPVOID  pvReserved)
{
    DWORD dwError=ERROR_SUCCESS;
    TCHAR szTempSection[256];
    TCHAR szTempLine[256];

	*pdwFlags = 0L;	//Don't request abort dialog or load in GUI mode.
#ifdef _DEBUG
	OutputDebugString(szPreNT5InfId);OutputDebugString(_T("\n"));
	OutputDebugString(szPreNT5Instance);OutputDebugString(_T("\n"));
#endif
    if (g_pfnNuAddSection && g_pfnNuAddLineToSection)
    {
        // add the top level section [params.xxx.OemSection]
        //
        g_pfnNuAddSection(szSectionName);

        // add the optional key InfToRunAfterInstall
        //
        wsprintf(szTempLine, TEXT("%s=\"\",%s.SectionToRun"),
                  c_szInfToRunAfterInstall, szSectionName);
        g_pfnNuAddLineToSection(szSectionName, szTempLine);

        // now add the section that should be run [Digixxx.SectionToRun]
        //
        wsprintf(szTempSection, TEXT("%s.SectionToRun"), szSectionName);
        g_pfnNuAddSection(szTempSection);

        // add the AddReg key to [Digixxx.SectionToRun]
        // AddReg = Digixxx.SectionToRun.AddReg
        wsprintf(szTempLine, TEXT("AddReg=%s.AddReg"), szTempSection);
        g_pfnNuAddLineToSection(szTempSection, szTempLine);

        // now add the AddReg section [Digixxx.SectionToRun.AddReg]
        //
        wsprintf(szTempSection, TEXT("%s.SectionToRun.AddReg"),
                  szSectionName);
        g_pfnNuAddSection(szTempSection);

        // finally add registry operations to this section
        //
      
		// Read the existing registry values and write them 
		// to the answer file section [Digixxx.SectionToRun.AddReg]	

		NetWriteDigiIsdnRegistry( hkeyParams, szPreNT5InfId, 
									szNT5InfId, szTempSection );
#if 0		
		//ISDN Example
		wsprintf(szTempLine, TEXT("HKR,0\\0,IsdnPhoneNumber,0,\"%s\""),
                  TEXT("111-2222")); 
        g_pfnNuAddLineToSection(szTempSection, szTempLine);

        wsprintf(szTempLine, TEXT("HKR,0\\0,IsdnPhoneNumber,0,\"%s\""),
                  TEXT("333-4444")); 
        g_pfnNuAddLineToSection(szTempSection, szTempLine);
#endif
    }
    
    return dwError;
}

EXTERN_C LONG __declspec(dllexport) __stdcall
TestUpgradeProcessing( IN   HKEY    hkeyParams,
                       IN   LPCTSTR szPreNT5InfId,
                       IN   LPCTSTR szNT5InfId,
                       IN   LPCTSTR szSectionName)
{

		NetWriteDigiIsdnRegistry( hkeyParams, szPreNT5InfId, 
									szNT5InfId, szSectionName );
		return ERROR_SUCCESS;
}