DBINIT.CPP

//-------------------------------------------------------------------- 
// Microsoft OLE DB Sample Provider
// (C) Copyright 1994 - 1996 Microsoft Corporation. All Rights Reserved.
//
// @doc
//
// @module DBINIT.CPP | IDBInitialize interface implementation
//
//

// Includes ------------------------------------------------------------------
#include "headers.h"

// IDBInitialize specific interface methods


// CImpIDBInitialize::Initialize ---------------------------------------------
//
// @mfunc Initializes the DataSource object.. For this provider it requires
// that a valid path is given to where the file will be located.
//
// @rdesc HRESULT
// @flag S_OK | Path exists
// @flag E_FAIL | Invalid path
// @flag E_INVALIDARG | Invalid Parameters passed in
//
STDMETHODIMP CImpIDBInitialize::Initialize
(
)
{
HRESULThr;
DBPROPIDSETrgPropertyIDSets[1];
ULONGcPropertySets;
DBPROPSET*prgPropertySets;
DBPROPIDrgPropId[1];

char szNewVal[MAX_PATH ];

assert( m_pObj );

if (m_pObj->m_fDSOInitialized)
return ResultFromScode( DB_E_ALREADYINITIALIZED );

rgPropId[0]= DBPROP_INIT_DATASOURCE;

rgPropertyIDSets[0].guidPropertySet= DBPROPSET_DBINIT;
rgPropertyIDSets[0].rgPropertyIDs= rgPropId;
rgPropertyIDSets[0].cPropertyIDs= 1;

// Get the value of the DBPROP_INIT_DATASOURCE property
hr = m_pObj->m_pUtilProp->GetProperties(
1,
rgPropertyIDSets,
&cPropertySets,
&prgPropertySets );

// On failure treat it as if we were opening with prompt..
if( SUCCEEDED(hr) )
lstrcpy(szNewVal, (char *)V_BSTR(&prgPropertySets[0].rgProperties->vValue));
else
*szNewVal = '\0';

// if caller didn't supply a directory path, ask the user
if (*szNewVal == '\0')
{
if (!BrowseDirs( GetDesktopWindow(), g_hInstance, NULL, szNewVal ))
{
return ResultFromScode( E_FAIL );
}
else
{
lstrcpyn( m_pObj->m_szPath, szNewVal, MAX_PATH );
m_pObj->m_fDSOInitialized = TRUE;
return ResultFromScode( S_OK );
}
}
else // caller did supply a directory path
{
// Check if the directory is a valid directory
if (SetCurrentDirectory( szNewVal ))
{
lstrcpyn( m_pObj->m_szPath, szNewVal, MAX_PATH );
m_pObj->m_fDSOInitialized = TRUE;
return ResultFromScode( S_OK );
}
else
{
return ResultFromScode( E_FAIL );
}
}
}



// CImpIDBInitialize::Uninitialize ---------------------------------------------
//
// @mfunc Returns the Data Source Object to an uninitialized state
//
// @rdesc HRESULT
// @flag S_OK | The method succeeded
// @flag DB_E_OBJECTOPEN | A DBSession object was already created
STDMETHODIMP CImpIDBInitialize::Uninitialize
(
void
)
{
assert( m_pObj );

if (!m_pObj->m_fDSOInitialized)
{
// data source object is not initialized; do nothing
return ResultFromScode( S_OK );
}
else
{
if (!m_pObj->m_fDBSessionCreated)
{
// DSO initialized, but no DBSession has been created.
// So, reset DSO to uninitialized state
m_pObj->m_fDSOInitialized = FALSE;
return ResultFromScode( S_OK );
}
else
{
// DBSession has already been created; trying to uninit
// the DSO now is an error
return ResultFromScode( DB_E_OBJECTOPEN );
}
}
}



//////////////////////////////////////////////////////////////////////////////
// Helper Functions Helper Functions Helper Functions
//////////////////////////////////////////////////////////////////////////////


// BrowseDirs ----------------------------------------------------------------
//
// @func Common dialog look-alike which allows you to select a direcotry.
//
// @rdesc BOOLEAN value indicates success or failure
// @flag TRUE | Directory was obtained
// @flag FALSE | No Directory was obtained
//
BOOL WINAPI BrowseDirs
(
HWND hDlg, //@parm IN | Window Handle
HINSTANCE hInst, //@parm IN | Instance of this DLL
LPCSTR lpszPrompt, //@parm IN | Prompt text for dialog, NULL for default
LPSTR lpszDrive //@parm OUT | Drive Name
)
{
OPENFILENAME ofn;
char szFile[_MAX_PATH ];
BROWSEINFO bi;

lstrcpy( szFile, "DUMMY.TXT" );

memset( &bi, 0, sizeof( BROWSEINFO ));
bi.szPrompt = lpszPrompt;

memset( &ofn, 0, sizeof( ofn ));
ofn.lStructSize = sizeof( OPENFILENAME );
ofn.hwndOwner = hDlg;
ofn.hInstance = hInst;
ofn.lpstrFile = (LPSTR) szFile;
ofn.nMaxFile = sizeof( szFile );
ofn.lpstrInitialDir = lpszDrive;
ofn.Flags = OFN_ENABLEHOOK |
OFN_ENABLETEMPLATE |
OFN_HIDEREADONLY |
OFN_NOTESTFILECREATE |
OFN_PATHMUSTEXIST |
OFN_SHAREAWARE;
ofn.lpfnHook = CommDlgHook;
ofn.lpTemplateName = MAKEINTRESOURCE( IDD_BROWSE );
ofn.lCustData = (LPARAM) (lpBROWSEINFO) & bi;
if (GetOpenFileName( &ofn ))
{
lstrcpyn( lpszDrive, szFile, ofn.nFileOffset );
*(lpszDrive + ofn.nFileOffset - 1) = '\0';
return TRUE;
}

return FALSE;
}


// CenterDialog --------------------------------------------------------------
//
// @func Center the dialog over the parent window.
//
// @rdesc NONE
//
void WINAPI CenterDialog
(
HWND hDlg //@parm IN | Window to Center
)
{
RECT rcParent; // Parent window client rect
RECT rcDlg; // Dialog window rect
int nLeft, nTop; // Top-left coordinates
int cWidth, cHeight; // Width and height
HWND hwnd;

// Get frame window client rect in screen coordinates
if ((hwnd = GetParent( hDlg )) == NULL)
{
rcParent.top = rcParent.left = 0;
rcParent.right = GetSystemMetrics( SM_CXFULLSCREEN );
rcParent.bottom = GetSystemMetrics( SM_CYFULLSCREEN );
}
else
GetWindowRect( hwnd, &rcParent );

// Determine the top-left point for the dialog to be centered
GetWindowRect( hDlg, &rcDlg );
cWidth = rcDlg.right - rcDlg.left;
cHeight = rcDlg.bottom - rcDlg.top;
nLeft = rcParent.left +
(((rcParent.right - rcParent.left) - cWidth) / 2);
nTop = rcParent.top +
(((rcParent.bottom - rcParent.top) - cHeight) / 2);
if (nLeft < 0)
nLeft = 0;
if (nTop < 0)
nTop = 0;

// Place the dialog
MoveWindow( hDlg, nLeft, nTop, cWidth, cHeight, TRUE );
return;
}


// CommDlgHook ---------------------------------------------------------------
//
// @func Hook function for browsing a directory. Need for custom info.
//
// @rdesc UINT value indicating hook procs status
//
UINT CALLBACK CommDlgHook
(
HWND hDlg, //@parm IN | Window Handle
UINT msg, //@parm IN | Window Message
WPARAM wParam, //@parm IN | Standard Value
LPARAM lParam //@parm IN | Standard Value
)
{
lpBROWSEINFO lpbi;

lpbi = (lpBROWSEINFO) GetWindowLong( hDlg, GWL_USERDATA );

switch (msg)
{
case WM_INITDIALOG:
{
OPENFILENAME FAR * lpofn;

lpofn = (OPENFILENAME FAR *) lParam;
lpbi = (lpBROWSEINFO) lpofn->lCustData;
SetWindowLong( hDlg, GWL_USERDATA, (LONG) lpbi );

CenterDialog( hDlg );

lpbi->uFileOkString = RegisterWindowMessage( FILEOKSTRING );
lpbi->lpofn = (LPOPENFILENAME) lParam;
if (lpbi->szPrompt)
SetWindowText( GetDlgItem( hDlg, IDT_PROMPT ), (LPSTR) lpbi->szPrompt );
}
return TRUE;

case WM_CLOSE:
if (lpbi)
SetWindowLong( hDlg, GWL_USERDATA, (LONG) 0L );
break;

default:
break;
}

return FALSE;
}