⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rcenuminstance.cpp

📁 MSSQL备份及恢复的VC++源代码。提供给大家学习。
💻 CPP
字号:
//////////////////////////////////////////////////////////////////////////////
//																			//
//			Created by Ozzy Osbourne(maojun) . HangZhou . 20030212			//
//				 warning: need add sqldmoid.h & sqldmo.h					//
//																			//
//////////////////////////////////////////////////////////////////////////////

// CRcEnumInstance is enum all MSSQLServer instances in the LAN 
// 
// Expect bugs.
// 
// Please use and enjoy. Please let me know of any bugs/mods/improvements 
// that you have found/implemented and I will fix/incorporate them into this file.
// Please send report to OzzyJMalmsteen@yahoo.com.cn or Ozzman@163.net

#include "stdafx.h"
#include "RcMSSQL.h"
#include "RcEnumInstance.h"

#include <lmerr.h>

// Stardard headers
#include <windows.h>
#include <tchar.h>
#include <stdio.h>
#include <initguid.h>

// DMO headers
#include "..\base\include\sqldmoid.h"
#include "..\base\include\sqldmo.h"

// Custom headers
#include "RcErrorMessage.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

// Must add the header file "#include <afxtempl.h>".

CArray<CString, CString&> g_InstanceArray;				// save sql server instances in LAN.
CArray<CString, CString&> g_InstalledInstanceArray;		// save installed instance in local

long g_lAmount;											// save instances amount.
long g_lInstalledAmount;								// save installed instance amount.

CRcErrorMessage * g_EnumError;

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CRcEnumInstance::CRcEnumInstance ()
{
	g_InstanceArray.RemoveAll ();
	g_InstalledInstanceArray.RemoveAll ();

	g_lAmount=0;
	g_lInstalledAmount=0;

	g_EnumError = NULL;
	if (g_EnumError == NULL)
	{
		g_EnumError = new CRcErrorMessage;
	}
}

CRcEnumInstance::~CRcEnumInstance ()
{
	if (g_EnumError != NULL)
	{
		delete g_EnumError;
		g_EnumError = NULL;
	}
}

BOOL CRcEnumInstance::SetInstanceList ()
{
	g_InstanceArray.RemoveAll ();
	g_lAmount=0;

	// Assume failure.
	BOOL bExitStatus = FALSE;

	HRESULT hr;

	LPSQLDMOAPPLICATION pDMOApp = NULL;
	LPSQLDMONAMELIST pNameList = NULL;

	try
	{
		// Initialize COM.
		if FAILED(hr = CoInitialize (NULL))
		{
			m_strErrorMessage.Format ("%s", "CoInitialize 失败!");
			return (bExitStatus);
		}

		// Create SQLDMOApplication.
		if FAILED(hr = CoCreateInstance (CLSID_SQLDMOApplication, NULL, CLSCTX_INPROC_SERVER, 
		IID_ISQLDMOApplication, (LPVOID*)&pDMOApp))
		{
			m_strErrorMessage.Format ("%s", "创建 CLSID_SQLDMOApplication 实例时失败!");
			return (bExitStatus);
		}

		// Get instance list.
		if SUCCEEDED(pDMOApp->ListAvailableSQLServers (&pNameList))
		{
			// Retrieve the count.
			long lCount=0; pNameList->GetCount (&lCount);

			BSTR strTemplate = NULL;	// BSTR: A 32-bit character pointer.
			CString strInstanceName;
			for(long i = 0; i < lCount; i++)
			{
				// Get the server name
				pNameList->GetItemByOrd (i, &strTemplate);
				strInstanceName = strTemplate;
				g_InstanceArray.Add (strInstanceName);
				g_lAmount++;
			}
			SysFreeString(strTemplate);

			bExitStatus = TRUE;
		}

		if (pDMOApp)
		{
			pNameList = NULL;
			pDMOApp->Release ();
			pDMOApp = NULL;
		}
		CoUninitialize ();
	}
	catch(...)
	{
		// _tprintf(TEXT("error dmolistservers unhandled exception (%d)\n"), GetLastError() );
		g_EnumError->FormatErrorMessage("IID_ISQLDMOApplication 异常中断", GetLastError ());
		m_strErrorMessage = g_EnumError->GetErrorMessage();
	}

	return bExitStatus;
}

CArray<CString,CString&> * CRcEnumInstance::GetInstanceList ()
{
	return &g_InstanceArray;
}

long CRcEnumInstance::GetInstanceAmount ()
{
	return g_lAmount;
}

BOOL CRcEnumInstance::SetInstalledInstanceList()
{
	g_InstalledInstanceArray.RemoveAll ();
	g_lInstalledAmount=0;

	// Assume failure.
	BOOL bExitStatus = FALSE;

	HRESULT hr;

	LPSQLDMOSERVER2 pDMOServer2 = NULL;
	LPSQLDMONAMELIST pNameList = NULL;

	try
	{
		// Initialize COM.
		if FAILED(hr = CoInitialize (NULL))
		{
			m_strErrorMessage.Format ("%s", "CoInitialize 失败!");
			return (bExitStatus);
		}

		// Create SQLDMOServer2.
		if FAILED(hr = CoCreateInstance (CLSID_SQLDMOServer2, NULL, CLSCTX_INPROC_SERVER, 
		IID_ISQLDMOServer2, (LPVOID*)&pDMOServer2))
		{
			m_strErrorMessage.Format ("%s", "创建 CLSID_SQLDMOServer2 实例时失败!");
			return (bExitStatus);
		}

		// Get the list of installed instance.
		if SUCCEEDED(pDMOServer2->ListInstalledInstances (&pNameList,NULL))
		{
			// Retrieve the count.
			long lCount=0; pNameList->GetCount (&lCount);

			BSTR strTemplate = NULL;	// BSTR: A 32-bit character pointer.
			CString strInstanceName;
			for (long i = 0; i < lCount; i++)
			{
				// Get the server name
				pNameList->GetItemByOrd (i, &strTemplate);
				strInstanceName = strTemplate;
				g_InstalledInstanceArray.Add (strInstanceName);
				g_lInstalledAmount++;
			}
			SysFreeString(strTemplate);

			bExitStatus = TRUE;
		}

		if (pDMOServer2)
		{
			pNameList = NULL;
			pDMOServer2->Release ();
			pDMOServer2 = NULL;
		}
		CoUninitialize ();
	}
	catch(...)
	{
		// _tprintf(TEXT("error dmolistservers unhandled exception (%d)\n"), GetLastError() );
		g_EnumError->FormatErrorMessage("IID_ISQLDMOServer2 异常中断", GetLastError ());
		m_strErrorMessage = g_EnumError->GetErrorMessage();
	}

	return bExitStatus;
}

CArray<CString,CString&> * CRcEnumInstance::GetInstalledInstanceList ()
{
	return &g_InstalledInstanceArray;
}

long CRcEnumInstance::GetInstalledInstanceAmount ()
{
	return g_lInstalledAmount;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -