WINMAIN.CPP

//----------------------------------------------------------------------------- 
// Microsoft OLE DB TABLECOPY Sample
// Copyright (C) 1996 By Microsoft Corporation.
//
// @doc
//
// @module WINMAIN.CPP
//
//-----------------------------------------------------------------------------


///////////////////////////////////////////////////////////////////////
// Includes
//
///////////////////////////////////////////////////////////////////////
#include "winmain.h"
#include "wizard.h"
#include "common.h"
#include "tablecopy.h"
#include "table.h"
#include "spy.h"


//////////////////////////////////////////////////////////////////
// int WINAPI WinMain
//
// Main application entry point
//////////////////////////////////////////////////////////////////
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
CHAR* pszCmdLine,
INT nCmdShow)
{
//Register IMallocSpy
CMallocSpy* pCMallocSpy = NULL;
MallocSpyRegister(&pCMallocSpy);

CWizard* pCWizard = new CWizard(NULL, hInstance);
HRESULT hr;

XTESTC(hr = CoInitialize(NULL));

//Main Execution
pCWizard->Run();

CLEANUP:
delete pCWizard;

MallocSpyUnRegister(pCMallocSpy);
CoUninitialize();

//Dump all the leaks
MallocSpyDump(pCMallocSpy);
delete pCMallocSpy;

return (hr==S_OK);
}


//////////////////////////////////////////////////////////////////
// void SyncSibling
//
//////////////////////////////////////////////////////////////////
void SyncSibling(HWND hToWnd, HWND hFromWnd)
{
ASSERT(hToWnd && hFromWnd);

//Make both windows synched,
//Get the current selection from the Source
LONG lSel = SendMessage(hFromWnd, LB_GETCURSEL, (WPARAM)0, (LPARAM)0L);

//Tell the Target to select the same selection
if(lSel != LB_ERR)
SendMessage(hToWnd, LB_SETCURSEL, (WPARAM)lSel, (LPARAM)0L);
}


//////////////////////////////////////////////////////////////////
// void InternalAssert
//
//////////////////////////////////////////////////////////////////
void InternalAssert(
char*pszExp,// The expression causing assert
char*pszFile,// The file name
UINTiLine// Line number of assert
)
{
CHARszMsg[MAX_QUERY_LEN];
_snprintf(szMsg, MAX_NAME_LEN, "Assertion Error!\n File '%s', Line '%lu'\n"
"Expression '%s'\n\n"
"Do you wish to Continue? (Press 'OK' to ignore the assertion."
" Press 'Cancel to debug.)",pszFile, iLine, pszExp);

//Popup a MessageBox
LONG dwSelection = MessageBoxA(NULL, szMsg,"Assertion Error",
MB_APPLMODAL | MB_ICONSTOP | MB_OKCANCEL | MB_DEFBUTTON1 );

switch (dwSelection)
{
case IDOK:
break;
case IDCANCEL:
DebugBreak();
break;
default:
ASSERT(!L"Unhandled Choice");
}
}


//////////////////////////////////////////////////////////////////
// void InternalTrace
//
//////////////////////////////////////////////////////////////////
void InternalTrace(WCHAR*pwszFmt, ...)
{
va_listmarker;
WCHARwszBuffer[MAX_NAME_LEN];
CHARszBuffer[MAX_NAME_LEN];

// Use format and arguements as input
//This version will not overwrite the stack, since it only copies
//upto the max size of the array
va_start(marker, pwszFmt);
_vsnwprintf(wszBuffer, MAX_NAME_LEN, pwszFmt, marker);
va_end(marker);

//Make sure there is a NULL Terminator, vsnwprintf will not copy
//the terminator if length==MAX_NAME_LEN
wszBuffer[MAX_NAME_LEN-1] = EOL;

//Convert to MBCS
ConvertToMBCS(wszBuffer, szBuffer, MAX_NAME_LEN);

//Output to the DebugWindow
OutputDebugString(szBuffer);
}


//////////////////////////////////////////////////////////////////
// void InternalTrace
//
//////////////////////////////////////////////////////////////////
void InternalTrace(CHAR*pszFmt, ...)
{
va_listmarker;
CHARszBuffer[MAX_NAME_LEN];

// Use format and arguements as input
//This version will not overwrite the stack, since it only copies
//upto the max size of the array
va_start(marker, pszFmt);
_vsnprintf(szBuffer, MAX_NAME_LEN, pszFmt, marker);
va_end(marker);

//Make sure there is a NULL Terminator, vsnwprintf will not copy
//the terminator if length==MAX_NAME_LEN
szBuffer[MAX_NAME_LEN-1] = '\0';

OutputDebugStringA(szBuffer);
}


//////////////////////////////////////////////////////////////////
// void Busy
//
//////////////////////////////////////////////////////////////////
void Busy(void)
{
static HCURSORhCursor;

ShowCursor(FALSE);

if(hCursor == NULL)
hCursor = SetCursor(LoadCursor(NULL, IDC_WAIT));
else
{
SetCursor(hCursor);
hCursor = NULL;
}

ShowCursor(TRUE);
}


//////////////////////////////////////////////////////////////////
// void OutOfMemory
//
//////////////////////////////////////////////////////////////////
void OutOfMemory(HWND hWnd)
{
//Unicode version is supported on Win95/WinNT
MessageBoxW(hWnd, L"Out of memory", L"Error", MB_OK);
}


//////////////////////////////////////////////////////////////////
// BOOL CenterDialog
//
//////////////////////////////////////////////////////////////////
BOOL CenterDialog(HWND hdlg)
{
RECT rcParent; // Parent window client rect
RECT rcDlg; // Dialog window rect
int nLeft, nTop; // Top-left coordinates
int cWidth, cHeight; // Width and height
HWNDhwnd;

// Get frame window client rect in screen coordinates
hwnd = GetParent(hdlg);
if(hwnd == NULL || hwnd == GetDesktopWindow())
{
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
return MoveWindow(hdlg, nLeft, nTop, cWidth, cHeight, TRUE);
}


//////////////////////////////////////////////////////////////////
// ULONG wMessageBox
//
//////////////////////////////////////////////////////////////////
INT wMessageBox(
HWND hwnd,// Parent window for message display
UINT uiStyle,// Style of message box
WCHAR* pwszTitle,// Title for message
WCHAR* pwszFmt,// Format string
...// Substitution parameters
)
{
va_listmarker;
WCHARwszBuffer[MAX_QUERY_LEN];

// Use format and arguements as input
//This version will not overwrite the stack, since it only copies
//upto the max size of the array
va_start(marker, pwszFmt);
_vsnwprintf(wszBuffer, MAX_QUERY_LEN, pwszFmt, marker);
va_end(marker);

//Make sure there is a NULL Terminator, vsnwprintf will not copy
//the terminator if length==MAX_QUERY_LEN
wszBuffer[MAX_QUERY_LEN-1] = EOL;

//Unicode version is supported on both Win95 / WinNT do need to convert
return MessageBoxW(hwnd, wszBuffer, pwszTitle, uiStyle);
}




//////////////////////////////////////////////////////////////////
// void wSetDlgItemText
//
//////////////////////////////////////////////////////////////////
void wSetDlgItemText(HWND hWnd, INT DlgItem, WCHAR* pwszFmt, ...)
{
va_listmarker;
WCHARwszBuffer[MAX_NAME_LEN];
CHARszBuffer[MAX_NAME_LEN];

// Use format and arguements as input
//This version will not overwrite the stack, since it only copies
//upto the max size of the array
va_start(marker, pwszFmt);
_vsnwprintf(wszBuffer, MAX_NAME_LEN, pwszFmt, marker);
va_end(marker);

//Make sure there is a NULL Terminator, vsnwprintf will not copy
//the terminator if length==MAX_NAME_LEN
wszBuffer[MAX_NAME_LEN-1] = EOL;

//convert to MBCS
ConvertToMBCS(wszBuffer, szBuffer, MAX_NAME_LEN);

SetDlgItemTextA(hWnd, DlgItem, szBuffer);
}


//////////////////////////////////////////////////////////////////
// UINT wGetDlgItemText
//
//////////////////////////////////////////////////////////////////
UINT wGetDlgItemText(HWND hWnd, INT DlgItem, WCHAR* pwsz, INT nMaxSize)
{
ASSERT(pwsz);
CHAR szBuffer[MAX_NAME_LEN];

UINT iReturn = GetDlgItemTextA(hWnd, DlgItem, szBuffer, MAX_NAME_LEN);

//convert to WCHAR
ConvertToWCHAR(szBuffer, pwsz, MAX_NAME_LEN);
return iReturn;
}



//////////////////////////////////////////////////////////////////
// LRESULT wSendMessage
//
//////////////////////////////////////////////////////////////////
LRESULT wSendMessage(HWND hWnd, UINT Msg, WPARAM wParam, WCHAR* pwszName)
{
ASSERT(pwszName);
CHAR szBuffer[MAX_NAME_LEN];

//Convert to ANSI before sending, since we don't know if this was a GET/SET message
ConvertToMBCS(pwszName, szBuffer, MAX_NAME_LEN);

//Send the message with an ANSI Buffer
LRESULT lResult = SendMessage(hWnd, Msg, (WPARAM)wParam, (LPARAM)szBuffer);

//Now convert the result into the users WCHAR buffer
ConvertToWCHAR(szBuffer, pwszName, MAX_NAME_LEN);
return lResult;
}


//////////////////////////////////////////////////////////////////
// BOOL GetEditBoxValue
//
//////////////////////////////////////////////////////////////////
BOOL GetEditBoxValue(HWND hEditWnd, ULONG ulMin, ULONG ulMax, ULONG* pulCount)
{
ASSERT(hEditWnd);
ASSERT(pulCount);

ULONGulCount = 0;
WCHARwszBuffer[MAX_NAME_LEN];
WCHAR* pwszEnd = NULL;

//Get the EditText
wSendMessage(hEditWnd, WM_GETTEXT, MAX_NAME_LEN, wszBuffer);

//Convert to ULONG
ulCount = wcstoul(wszBuffer, &pwszEnd, 10);
if(!wszBuffer[0] || ulCount<ulMin || ulCount>ulMax || pwszEnd==NULL || pwszEnd[0]!=EOL)
{
wMessageBox(hEditWnd, MB_ICONEXCLAMATION | MB_OK, wsz_ERROR,
wsz_INVALID_VALUE_, wszBuffer, ulMin, ulMax);
SetFocus(hEditWnd);
return FALSE;
}

*pulCount = ulCount;
return TRUE;
}