#define STRICT // strict type checking
#define WIN32_LEAN_AND_MEAN // do not include the world
#define INC_OLE2 // include OLE/COM files
#define UNICODE
#define _UNICODE
#include <windows.h>
#include <initguid.h>
#include <tchar.h>
#include "C:/Program Files/Microsoft SQL Server/80/Tools/DevTools/include/sqldmoid.h"
#include "C:/Program Files/Microsoft SQL Server/80/Tools/DevTools/include/sqldmo.h"
#include <stdlib.h>
#include <stdio.h>
#include <olectl.h>
#include "backupdb.h"
// **********************************************************************
// function declarations
// **********************************************************************
int DisplayError();
STDMETHODIMP CMyBackupSink::QueryInterface(REFIID riid, LPVOID FAR* ppvObj)
{
if (riid == IID_IUnknown || riid == IID_ISQLDMOBackupSink)
{
AddRef();
*ppvObj = this;
return NOERROR;
}
return E_NOINTERFACE;
}
// Backup Sink Methords.
//
STDMETHODIMP CMyBackupSink::Complete(THIS_ SQLDMO_LPCSTR Message)
{
// Backup object calls us when backup is completed.
//
printf("Backup Completed/n");
return NOERROR;
}
STDMETHODIMP CMyBackupSink:
ercentComplete(THIS_ SQLDMO_LPCSTR Message, long Percent)
{
// Backup object calls us with new percent complete.
//
printf("%ld%s Completed/n", Percent,"%");
return NOERROR;
}
STDMETHODIMP CMyBackupSink::NextMedia(THIS_ SQLDMO_LPCSTR Message)
{
// Backup object calls us when it's ready for next volume.
//
printf("Next Volume Message: %s /n", Message);
return NOERROR;
}
// **********************************************************************
// main()
// **********************************************************************
int main(void)
{
HRESULT hr;
if FAILED(hr = CoInitialize (NULL))
{
_tprintf(TEXT("CoInitialize Failed/n"));
return (0);
}
LPSQLDMOSERVER pSQLServer = NULL;
if FAILED(hr = CoCreateInstance(
CLSID_SQLDMOServer,
NULL,
CLSCTX_INPROC_SERVER,
IID_ISQLDMOServer,
(LPVOID*)&pSQLServer))
{
_tprintf(TEXT("CoCreateInstance Failed/n"));
return (0);
}
pSQLServer->SetLoginTimeout(10);
pSQLServer->SetLoginSecure(TRUE);
if FAILED(hr = pSQLServer->Connect(TEXT(""),TEXT("sa"),TEXT("")))
{
return DisplayError();
}
LPSQLDMOBACKUP pSQLBackup = NULL;
if FAILED(hr = CoCreateInstance(
CLSID_SQLDMOBackup,
NULL,
CLSCTX_INPROC_SERVER,
IID_ISQLDMOBackup,
(LPVOID*)&pSQLBackup))
{
_tprintf(TEXT("CoCreateInstance Failed/n"));
return (0);
}
LPCONNECTIONPOINTCONTAINER pMyConnectionPointContainer;
LPCONNECTIONPOINT pMyConnectionPoint;
CMyBackupSink* pMyBackupSink = new CMyBackupSink();
pMyBackupSink->AddRef();
if (!pMyBackupSink)
{
return(0);
}
DWORD dwCookie;
// ask the connectable object (pSQLBackup) about its outgoing interface,
// by using QI on IID_IConnectionPointContainer. If fails, this object
// does not support outgoing interface
//
if FAILED(pSQLBackup->QueryInterface(
IID_IConnectionPointContainer,
(LPVOID FAR*) &pMyConnectionPointContainer))
{
return DisplayError();
}
// find the specific outgoing interface IID_ISQLDMOBackupSink and retrieve
// a pointer to the connectionpoint object. If fails, the object does not
// support this outgoing interface
//
if FAILED(pMyConnectionPointContainer->FindConnectionPoint(
IID_ISQLDMOBackupSink,
(LPCONNECTIONPOINT FAR*)&pMyConnectionPoint))
{
return DisplayError();
}
// establish the connection between the Sink and the ConnectionPoint object.
// Retrieve a key (cookie) value for terminating the connection later. If
// this fails, the Sink and ConnectionPoint object do not support the same interface
//
if (S_OK != (hr = pMyConnectionPoint->Advise((LPUNKNOWN)pMyBackupSink, &dwCookie)))
{
return DisplayError();
}
if FAILED(hr = pSQLBackup->SetFiles(TEXT("c://test.dmp") ))
{
return DisplayError();
}
if FAILED(hr = pSQLBackup->SetDatabase(TEXT("pubs") ))
{
return DisplayError();
}
printf("Backup Start/n");
if FAILED(hr = pSQLBackup->SQLBackup(pSQLServer))
{
return DisplayError();
}
// terminate the connection using the same connection key
//
pMyConnectionPoint->Unadvise(dwCookie);
pMyConnectionPoint->Release ();
pMyConnectionPointContainer->Release ();
pSQLBackup->Release ();
pSQLServer->Release ();
CoUninitialize ();
return (0);
}
// **********************************************************************
// display error information
// **********************************************************************
int DisplayError()
{
LPERRORINFO pErrorInfo = NULL;
BSTR strDescription, strSource;
GetErrorInfo(0,&pErrorInfo);
pErrorInfo->GetDescription (&strDescription);
pErrorInfo->GetSource(&strSource);
_tprintf(TEXT("%s/n"),strDescription);
_tprintf(TEXT("%s/n"),strSource);
pErrorInfo->Release();
SysFreeString(strDescription);
SysFreeString(strSource);
return(0);
}