(*)
 [------------------------------------------------------------------------------
 [  DirectSound 8.0 Delphi Adaptation by Ivo Steinmann
 [  DirectSound 7.0 Delphi Adaptation by Erik Unger
 [------------------------------------------------------------------------------
 [  Files    : dsetup.h
 [  Modified : 11-Sep-2002
 [  E-Mail   : isteinmann@bluewin.ch
 [  Download : http://www.crazyentertainment.net
 [------------------------------------------------------------------------------
(*)

(*)
 [------------------------------------------------------------------------------
 [ History :
 [----------
 [ 11-Sep-2002 (Tim Baumgarten) : Removed silly Find-DSetup-Dll-Code, if you
 [                                  want to use DSetup you must include
 [                                  DSetup.DLL yourself. Set DSetupDLLPath !
 [                                Cosmetic changes
 [------------------------------------------------------------------------------
(*)

unit DirectSetup;

{$IFDEF VER150}
  {$WARN UNSAFE_CODE OFF}
  {$WARN UNSAFE_TYPE OFF}
  {$WARN UNSAFE_CAST OFF}
{$ENDIF}

{$MINENUMSIZE 4}
{$ALIGN ON}

interface

uses
  Windows,
  DXCommon;

var
  DSetupDLL : HModule = 0;

const
  DSetupDLLPath = 'DSetup.DLL';

type
  PDLSVersion = ^TDLSVersion;
  TDLSVersion = packed record
    dwVersionMS : LongWord;
    dwVersionLS : LongWord;
  end;


const
  FOURCC_VERS : array[0..3] of Char = ('v','e','r','s');

// DSETUP Error Codes, must remain compatible with previous setup.
  DSETUPERR_SUCCESS_RESTART      = HResult(+1);
  DSETUPERR_SUCCESS              = HResult(+0);
  DSETUPERR_BADWINDOWSVERSION    = HResult(-1);
  DSETUPERR_SOURCEFILENOTFOUND   = HResult(-2);
  DSETUPERR_BADSOURCESIZE        = HResult(-3);
  DSETUPERR_BADSOURCETIME        = HResult(-4);
  DSETUPERR_NOCOPY               = HResult(-5);
  DSETUPERR_OUTOFDISKSPACE       = HResult(-6);
  DSETUPERR_CANTFINDINF          = HResult(-7);
  DSETUPERR_CANTFINDDIR          = HResult(-8);
  DSETUPERR_INTERNAL             = HResult(-9);
  DSETUPERR_NTWITHNO3D           = HResult(-10);  // REM: obsolete, you'll never see this
  DSETUPERR_UNKNOWNOS            = HResult(-11);
  DSETUPERR_USERHITCANCEL        = HResult(-12);
  DSETUPERR_NOTPREINSTALLEDONNT  = HResult(-13);
  DSETUPERR_NEWERVERSION         = HResult(-14);
  DSETUPERR_NOTADMIN             = HResult(-15);
  DSETUPERR_UNSUPPORTEDPROCESSOR = HResult(-16);


// DSETUP flags. DirectX 5.0 apps should use these flags only.
  DSETUP_DDRAWDRV      = {viewgit}{/viewgit}000008;    // install DirectDraw Drivers
  DSETUP_DSOUNDDRV     = {viewgit}{/viewgit}000010;    // install DirectSound Drivers
  DSETUP_DXCORE        = {viewgit}{/viewgit}010000;    // install DirectX runtime
  DSETUP_DIRECTX       = DSETUP_DXCORE or DSETUP_DDRAWDRV or DSETUP_DSOUNDDRV;
  DSETUP_TESTINSTALL   = {viewgit}{/viewgit}020000;    // just test install, don't do anything
  DSETUP_USEROLDERFLAG = 000000;    // enable return DSETUPERR_NEWERVERSION
// Bug #22730
  DSETUP_NTINSTALL     = {viewgit}{/viewgit}080000;    // install on Win2K platform

// These OBSOLETE flags are here for compatibility with pre-DX5 apps only.
// They are present to allow DX3 apps to be recompiled with DX5 and still work.
// DO NOT USE THEM for DX5. They will go away in future DX releases.
  DSETUP_DDRAW            = {viewgit}{/viewgit}000001;  // OBSOLETE. install DirectDraw
  DSETUP_DSOUND           = {viewgit}{/viewgit}000002;  // OBSOLETE. install DirectSound
  DSETUP_DPLAY            = {viewgit}{/viewgit}000004;  // OBSOLETE. install DirectPlay
  DSETUP_DPLAYSP          = {viewgit}{/viewgit}000020;  // OBSOLETE. install DirectPlay Providers
  DSETUP_DVIDEO           = {viewgit}{/viewgit}000040;  // OBSOLETE. install DirectVideo
  DSETUP_D3D              = {viewgit}{/viewgit}000200;  // OBSOLETE. install Direct3D
  DSETUP_DINPUT           = {viewgit}{/viewgit}000800;  // OBSOLETE. install DirectInput
  DSETUP_DIRECTXSETUP     = {viewgit}{/viewgit}001000;  // OBSOLETE. install DirectXSetup DLL's
  DSETUP_NOUI             = {viewgit}{/viewgit}002000;  // OBSOLETE. install DirectX with NO UI
  DSETUP_PROMPTFORDRIVERS = 000000;  // OBSOLETE. prompt when replacing display/audio drivers
  DSETUP_RESTOREDRIVERS   = 000000;  // OBSOLETE. restore display/audio drivers

//******************************************************************
// DirectX Setup Callback mechanism
//******************************************************************

// DSETUP Message Info Codes, passed to callback as Reason parameter.
  DSETUP_CB_MSG_NOMESSAGE                 = 0;
  DSETUP_CB_MSG_CANTINSTALL_UNKNOWNOS     = 1;
  DSETUP_CB_MSG_CANTINSTALL_NT            = 2;
  DSETUP_CB_MSG_CANTINSTALL_BETA          = 3;
  DSETUP_CB_MSG_CANTINSTALL_NOTWIN32      = 4;
  DSETUP_CB_MSG_CANTINSTALL_WRONGLANGUAGE = 5;
  DSETUP_CB_MSG_CANTINSTALL_WRONGPLATFORM = 6;
  DSETUP_CB_MSG_PREINSTALL_NT             = 7;
  DSETUP_CB_MSG_NOTPREINSTALLEDONNT       = 8;
  DSETUP_CB_MSG_SETUP_INIT_FAILED         = 9;
  DSETUP_CB_MSG_INTERNAL_ERROR            = 10;
  DSETUP_CB_MSG_CHECK_DRIVER_UPGRADE      = 11;
  DSETUP_CB_MSG_OUTOFDISKSPACE            = 12;
  DSETUP_CB_MSG_BEGIN_INSTALL             = 13;
  DSETUP_CB_MSG_BEGIN_INSTALL_RUNTIME     = 14;
  DSETUP_CB_MSG_BEGIN_INSTALL_DRIVERS     = 15;
  DSETUP_CB_MSG_BEGIN_RESTORE_DRIVERS     = 16;
  DSETUP_CB_MSG_FILECOPYERROR             = 17;


  DSETUP_CB_UPGRADE_TYPE_MASK      = {viewgit}{/viewgit}0F;
  DSETUP_CB_UPGRADE_KEEP           = {viewgit}{/viewgit}01;
  DSETUP_CB_UPGRADE_SAFE           = {viewgit}{/viewgit}02;
  DSETUP_CB_UPGRADE_FORCE          = {viewgit}{/viewgit}04;
  DSETUP_CB_UPGRADE_UNKNOWN        = {viewgit}{/viewgit}08;

  DSETUP_CB_UPGRADE_HASWARNINGS    = 00;
  DSETUP_CB_UPGRADE_CANTBACKUP     = 00;

  DSETUP_CB_UPGRADE_DEVICE_ACTIVE  = 00;

  DSETUP_CB_UPGRADE_DEVICE_DISPLAY = 00;
  DSETUP_CB_UPGRADE_DEVICE_MEDIA   = 00;


type
  PDSetupCBUpgradeInfo = ^TDSetupCBUpgradeInfo;
  TDSetupCBUpgradeInfo = packed record
    UpgradeFlags : LongWord;
  end;

  PDSetup_CB_UpgradeInfo = ^TDSetup_CB_UpgradeInfo;
  TDSetup_CB_UpgradeInfo = TDSetupCBUpgradeInfo;

  PDSetupCBFileCopyError = ^TDSetupCBFileCopyError;
  TDSetupCBFileCopyError = packed record
    dwError : LongWord;
  end;

  PDSetup_CB_FileCopyError = ^TDSetup_CB_FileCopyError;
  TDSetup_CB_FileCopyError = TDSetupCBFileCopyError;

//
// Data Structures
//
  PDirectXRegisterAppA = ^TDirectXRegisterAppA;
  TDirectXRegisterAppA = packed record
    dwSize               : LongWord;
    dwFlags              : LongWord;
    lpszApplicationName  : PAnsiChar;
    lpGUID               : PGUID;
    lpszFilename         : PAnsiChar;
    lpszCommandLine      : PAnsiChar;
    lpszPath             : PAnsiChar;
    lpszCurrentDirectory : PAnsiChar;
  end;

  PDirectXRegisterApp2A = ^TDirectXRegisterApp2A;
  TDirectXRegisterApp2A = packed record
    dwSize               : LongWord;
    dwFlags              : LongWord;
    lpszApplicationName  : PAnsiChar;
    lpGUID               : PGUID;
    lpszFilename         : PAnsiChar;
    lpszCommandLine      : PAnsiChar;
    lpszPath             : PAnsiChar;
    lpszCurrentDirectory : PAnsiChar;
    lpszLauncherName     : PAnsiChar;
  end;

  PDirectXRegisterAppW = ^TDirectXRegisterAppW;
  TDirectXRegisterAppW = packed record
    dwSize               : LongWord;
    dwFlags              : LongWord;
    lpszApplicationName  : PWideChar;
    lpGUID               : PGUID;
    lpszFilename         : PWideChar;
    lpszCommandLine      : PWideChar;
    lpszPath             : PWideChar;
    lpszCurrentDirectory : PWideChar;
  end;

  PDirectXRegisterApp2W = ^TDirectXRegisterApp2W;
  TDirectXRegisterApp2W = packed record
    dwSize               : LongWord;
    dwFlags              : LongWord;
    lpszApplicationName  : PWideChar;
    lpGUID               : PGUID;
    lpszFilename         : PWideChar;
    lpszCommandLine      : PWideChar;
    lpszPath             : PWideChar;
    lpszCurrentDirectory : PWideChar;
    lpszLauncherName     : PWideChar;
  end;

  PDirectXRegisterApp = ^TDirectXRegisterApp;
  PDirectXRegisterApp2 = ^TDirectXRegisterApp2;
{$IFDEF UNICODE}
  TDirectXRegisterApp = TDirectXRegisterAppW;
  TDirectXRegisterApp2 = TDirectXRegisterAppW2;
{$ELSE}
  TDirectXRegisterApp = TDirectXRegisterAppA;
  TDirectXRegisterApp2 = TDirectXRegisterApp2A;
{$ENDIF}

//
// API
//
var
  DirectXSetupW : function(hWnd : HWND; lpszRootPath : PWideChar; dwFlags : LongWord) : Integer; stdcall;
  DirectXSetupA : function(hWnd : HWND; lpszRootPath : PAnsiChar; dwFlags : LongWord) : Integer; stdcall;
  DirectXSetup : function(hWnd : HWND; lpszRootPath : PCharAW; dwFlags : LongWord) : Integer; stdcall;

  DirectXDeviceDriverSetupW : function(hWnd : HWND; lpszDriverClass : PWideChar; lpszDriverPath : PWideChar; dwFlags : LongWord) : Integer; stdcall;
  DirectXDeviceDriverSetupA : function(hWnd : HWND; lpszDriverClass : PAnsiChar; lpszDriverPath : PAnsiChar; dwFlags : LongWord) : Integer; stdcall;
  DirectXDeviceDriverSetup : function(hWnd : HWND; lpszDriverClass : PCharAW; lpszDriverPath : PCharAW; dwFlags : LongWord) : Integer; stdcall;

  DirectXRegisterApplicationW : function(hWnd : HWND; const lpDXRegApp : TDirectXRegisterAppW) : Integer; stdcall;
  DirectXRegisterApplicationA : function(hWnd : HWND; const lpDXRegApp : TDirectXRegisterAppA) : Integer; stdcall;
  DirectXRegisterApplication : function(hWnd : HWND; const lpDXRegApp : TDirectXRegisterApp) : Integer; stdcall;

  DirectXUnRegisterApplication : function(hWnd : HWND; const lpGUID : TGUID) : Integer; stdcall;

type
  TDSetupCallback = function(Reason : LongWord; MsgType: LongWord; // Same as flags to MessageBox
                             szMessage : PChar; szName : PChar; pInfo : Pointer) : LongWord; stdcall;

var
  DirectXSetupSetCallback : function(Callback : TDSetupCallback) : Integer; stdcall;

  DirectXSetupGetVersion : function(out lpdwVersion, lpdwMinorVersion : LongWord) : Integer; stdcall;

implementation

(*==========================================================================
 *
 *  Copyright (C) 1995-1997 Microsoft Corporation.  All Rights Reserved.
 *
 *  File:       dsetup.h
 *  Content:    DirectXSetup, error codes and flags
 ***************************************************************************)

procedure LoadDSetup;
  function ExistFile(const FileName: string): Boolean;
  var hFile: THandle;
  begin
    hFile := CreateFile(PChar(FileName), 0, 0, nil, OPEN_EXISTING, 0, 0);
    Result := hFile <> INVALID_HANDLE_VALUE;
    if hFile = INVALID_HANDLE_VALUE then CloseHandle(hFile);
  end;
begin
  if ExistFile(DSetupDLLPath) then
    DSetupDLL := LoadLibrary(DSetupDLLPath);

  if DSetupDLL <> 0 then
  begin
    DirectXSetupA := GetProcAddress(DSetupDLL, 'DirectXSetupA');
    DirectXSetupW := GetProcAddress(DSetupDLL, 'DirectXSetupW');
  {$IFDEF UNICODE}
    DirectXSetup := DirectXSetupW;
  {$ELSE}
    DirectXSetup := DirectXSetupA;
  {$ENDIF}

    DirectXDeviceDriverSetupA := GetProcAddress(DSetupDLL, 'DirectXDeviceDriverSetupA');
    DirectXDeviceDriverSetupW := GetProcAddress(DSetupDLL, 'DirectXDeviceDriverSetupW');
  {$IFDEF UNICODE}
    DirectXDeviceDriverSetup := DirectXDeviceDriverSetupW;
  {$ELSE}
    DirectXDeviceDriverSetup := DirectXDeviceDriverSetupA;
  {$ENDIF}

    DirectXRegisterApplicationA := GetProcAddress(DSetupDLL,'DirectXRegisterApplicationA');
    DirectXRegisterApplicationW := GetProcAddress(DSetupDLL,'DirectXRegisterApplicationW');
  {$IFDEF UNICODE}
    DirectXRegisterApplication := DirectXRegisterApplicationW;
  {$ELSE}
    DirectXRegisterApplication := DirectXRegisterApplicationA;
  {$ENDIF}

    DirectXUnRegisterApplication := GetProcAddress(DSetupDLL, 'DirectXUnRegisterApplication');

    DirectXSetupSetCallback := GetProcAddress(DSetupDLL, 'DirectXSetupSetCallback');

    DirectXSetupGetVersion := GetProcAddress(DSetupDLL, 'DirectXSetupGetVersion');
  end;

end;

initialization
begin
  if not IsNTandDelphiRunning then
  begin
    LoadDSetup;
  end;
end;

finalization
begin
  FreeLibrary(DSetupDLL);
end;

end.