www.gusucode.com > 一些VC++加密算法+实例源码源码程序 > 一些VC++加密算法+实例源码/优化后的加密注册模块/优化后的加密注册模块/inventory(optimized)/inventory.cpp

    // inventory.cpp : Implementation of WinMain


// Note: Proxy/Stub Information
//      To build a separate proxy/stub DLL, 
//      run nmake -f inventoryps.mk in the project directory.

#include "stdafx.h"
#include "resource.h"
#include <initguid.h>
#include <stdio.h>
#include <winbase.h>
#include "inventory.h"
#include "InventoryObj.h"
#include "am_lv1.h"
#include "ErrorReport.h"
#include "Encry.h"
#include "MapLink.h"
#include "UCICMP.h"
#include "general.h"

#include "inventory_i.c"
#include "EndAuthServer_i.c"


/////////////////////////////////////////////////////////////////////
bool PingFlag;
const DWORD dwTimeOut = 5000;	 // time for EXE to be idle before shutting down
const DWORD dwPause = 1000;		 // time to wait for threads to finish up
MapLink *MAPL;

IDBObject *pIobject_mm=NULL;		//globle pointer of sqlservice		
//HANDLE	handle;		可以解决主副线程时序问题。(CreateEvent,SetEvent,ResetEvent,WaitForSingleObject)

extern unsigned char pKey[8];		//globle public key for encrypt/decrypt 
extern HRESULT SetEndSecurity(	long serviceID);

//define some pointer to point 
//the records queried from db
BSTR  *condevdb;
BSTR  *mapdb;
BSTR  *cpudb;
BSTR  *memdb;
BSTR  *filedb;
BSTR  *osdb;
BSTR  *devicedb;
BSTR  *networkdb;
BSTR  *statdb;
BSTR  *netstatdb;
/////////////////////////////////////////////////////////////////////////

//****************************************************
//*Polling						                     *
//*   Polling all end system to check availability	 *
//*Call format:										 *
//*UINT Polling(LPVOID pParam)						 *
//*	  pParam  : thread ID of _tWinMain				 *
//*input:											 *
//*   thread ID of _tWinMain 	 					 *
//*Procedure called:                                 *
//*   _tWinMain										 * 
//*	:Polling all end system to check availability	 * 
//****************************************************
UINT Polling(LPVOID pParam)
{
	UCICMP *PingClass=new UCICMP;
	int interval,i;
	bool ret;
	long tmpid;
	while(MAPL->number<=0)
		Sleep(60000);
	MAPNode *header=MAPL->header;
	PingClass->mfInit();		//initialize

	while(PingFlag)
	{
		if(header==NULL)
		{
			while(MAPL->number<=0)
				Sleep(60000);

			header=MAPL->header;
		}
		interval=2*60000/MAPL->number;	//polling interval(2 min a circle)


		for(i=0;i<3;i++)	//Ping 3 times
		{
			//ping the endsystem basis on corresponding ipaddress
			ret=PingClass->mfPing((char *)header->body->IPAddress,1000);
			if(ret) break;
		}
		tmpid=header->body->ID;
		header=header->Next;
		if(!ret)
		{
			//failed, send message to major thread to modify the record in map table
			i=::PostThreadMessage(*(unsigned int *)pParam,WM_USER+100,0,tmpid);
			MAPL->deleteNode(tmpid);
		}
	
		Sleep(interval);
	}
	delete MAPL;

	PingClass->mfUninit();
	delete PingClass;
	
	return 0;
}
//////////////////////////////////////////////////////////////////////////

CServiceModule _Module;

BEGIN_OBJECT_MAP(ObjectMap)
OBJECT_ENTRY(CLSID_InventoryObj, CInventoryObj)
END_OBJECT_MAP()


LPCTSTR FindOneOf(LPCTSTR p1, LPCTSTR p2)
{
    while (p1 != NULL && *p1 != NULL)
    {
        LPCTSTR p = p2;
        while (p != NULL && *p != NULL)
        {
            if (*p1 == *p)
                return CharNext(p1);
            p = CharNext(p);
        }
        p1 = CharNext(p1);
    }


    return NULL;
}

// Although some of these functions are big they are declared inline since they are only used once

inline HRESULT CServiceModule::RegisterServer(BOOL bRegTypeLib, BOOL bService)
{
    HRESULT hr = CoInitialize(NULL);
    if (FAILED(hr))
        return hr;

    // Remove any previous service since it may point to
    // the incorrect file
    Uninstall();

    // Add service entries
    UpdateRegistryFromResource(IDR_Inventory, TRUE);

    // Adjust the AppID for Local Server or Service
    CRegKey keyAppID;
    LONG lRes = keyAppID.Open(HKEY_CLASSES_ROOT, _T("AppID"), KEY_WRITE);
    if (lRes != ERROR_SUCCESS)
        return lRes;

    CRegKey key;
    lRes = key.Open(keyAppID, _T("{BE43B812-198F-11D4-ADAC-00104B134DB9}"), KEY_WRITE);
    if (lRes != ERROR_SUCCESS)
        return lRes;
    key.DeleteValue(_T("LocalService"));
    
    if (bService)
    {
        key.SetValue(_T("inventory"), _T("LocalService"));
        key.SetValue(_T("-Service"), _T("ServiceParameters"));
        // Create service
        Install();
    }

    // Add object entries
    hr = CComModule::RegisterServer(bRegTypeLib);

    CoUninitialize();
    return hr;
}

inline HRESULT CServiceModule::UnregisterServer()
{
    HRESULT hr = CoInitialize(NULL);
    if (FAILED(hr))
        return hr;

    // Remove service entries
    UpdateRegistryFromResource(IDR_Inventory, FALSE);
    // Remove service
    Uninstall();
    // Remove object entries
    CComModule::UnregisterServer(TRUE);
    CoUninitialize();
    return S_OK;
}

inline void CServiceModule::Init(_ATL_OBJMAP_ENTRY* p, HINSTANCE h, UINT nServiceNameID, const GUID* plibid)
{
    CComModule::Init(p, h, plibid);

    m_bService = TRUE;

    LoadString(h, nServiceNameID, m_szServiceName, sizeof(m_szServiceName) / sizeof(TCHAR));

    // set up the initial service status 
    m_hServiceStatus = NULL;
    m_status.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
    m_status.dwCurrentState = SERVICE_STOPPED;
    m_status.dwControlsAccepted = SERVICE_ACCEPT_STOP|SERVICE_ACCEPT_SHUTDOWN;
    m_status.dwWin32ExitCode = 0;
    m_status.dwServiceSpecificExitCode = 0;
    m_status.dwCheckPoint = 0;
    m_status.dwWaitHint = 0;
}

LONG CServiceModule::Unlock()
{
    LONG l = CComModule::Unlock();
    if (l == 0 && !m_bService)
        PostThreadMessage(dwThreadID, WM_QUIT, 0, 0);
    return l;
}

BOOL CServiceModule::IsInstalled()
{
    BOOL bResult = FALSE;

    SC_HANDLE hSCM = ::OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);

    if (hSCM != NULL)
    {
        SC_HANDLE hService = ::OpenService(hSCM, m_szServiceName, SERVICE_QUERY_CONFIG);
        if (hService != NULL)
        {
            bResult = TRUE;
            ::CloseServiceHandle(hService);
        }
        ::CloseServiceHandle(hSCM);
    }
    return bResult;
}

inline BOOL CServiceModule::Install()
{
    if (IsInstalled())
        return TRUE;

    SC_HANDLE hSCM = ::OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
    if (hSCM == NULL)
    {
        MessageBox(NULL, _T("Couldn't open service manager"), m_szServiceName, MB_OK);
        return FALSE;
    }

    // Get the executable file path
    TCHAR szFilePath[_MAX_PATH];
    ::GetModuleFileName(NULL, szFilePath, _MAX_PATH);

    SC_HANDLE hService = ::CreateService(
        hSCM, m_szServiceName, m_szServiceName,
        SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS,
        SERVICE_DEMAND_START, SERVICE_ERROR_NORMAL,
        szFilePath, NULL, NULL, _T("RPCSS\0"), NULL, NULL);

    if (hService == NULL)
    {
        ::CloseServiceHandle(hSCM);
        MessageBox(NULL, _T("Couldn't create service"), m_szServiceName, MB_OK);
        return FALSE;
    }

    ::CloseServiceHandle(hService);
    ::CloseServiceHandle(hSCM);
    return TRUE;
}

inline BOOL CServiceModule::Uninstall()
{
    if (!IsInstalled())
        return TRUE;

    SC_HANDLE hSCM = ::OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);

    if (hSCM == NULL)
    {
        MessageBox(NULL, _T("Couldn't open service manager"), m_szServiceName, MB_OK);
        return FALSE;
    }

    SC_HANDLE hService = ::OpenService(hSCM, m_szServiceName, SERVICE_STOP | DELETE);

    if (hService == NULL)
    {
        ::CloseServiceHandle(hSCM);
        MessageBox(NULL, _T("Couldn't open service"), m_szServiceName, MB_OK);
        return FALSE;
    }
    SERVICE_STATUS status;
    ::ControlService(hService, SERVICE_CONTROL_STOP, &status);

    BOOL bDelete = ::DeleteService(hService);
    ::CloseServiceHandle(hService);
    ::CloseServiceHandle(hSCM);

    if (bDelete)
        return TRUE;

    MessageBox(NULL, _T("Service could not be deleted"), m_szServiceName, MB_OK);
    return FALSE;
}

///////////////////////////////////////////////////////////////////////////////////////
// Logging functions
void CServiceModule::LogEvent(LPCTSTR pFormat, ...)
{
    TCHAR    chMsg[256];
    HANDLE  hEventSource;
    LPTSTR  lpszStrings[1];
    va_list pArg;

    va_start(pArg, pFormat);
    _vstprintf(chMsg, pFormat, pArg);
    va_end(pArg);

    lpszStrings[0] = chMsg;

    if (m_bService)
    {
        /* Get a handle to use with ReportEvent(). */
        hEventSource = RegisterEventSource(NULL, m_szServiceName);
        if (hEventSource != NULL)
        {
            /* Write to event log. */
            ReportEvent(hEventSource, EVENTLOG_INFORMATION_TYPE, 0, 0, NULL, 1, 0, (LPCTSTR*) &lpszStrings[0], NULL);
            DeregisterEventSource(hEventSource);
        }
    }
    else
    {
        // As we are not running as a service, just write the error to the console.
        _putts(chMsg);
    }
}

//////////////////////////////////////////////////////////////////////////////////////////////
// Service startup and registration
inline void CServiceModule::Start()
{
    SERVICE_TABLE_ENTRY st[] =
    {
        { m_szServiceName, _ServiceMain },
        { NULL, NULL }
    };
    if (m_bService && !::StartServiceCtrlDispatcher(st))
    {
        m_bService = FALSE;
    }
    if (m_bService == FALSE)
        Run();
}

inline void CServiceModule::ServiceMain(DWORD /* dwArgc */, LPTSTR* /* lpszArgv */)
{
    // Register the control request handler
    m_status.dwCurrentState = SERVICE_START_PENDING;
    m_hServiceStatus = RegisterServiceCtrlHandler(m_szServiceName, _Handler);
    if (m_hServiceStatus == NULL)
    {
        LogEvent(_T("Handler not installed"));
        return;
    }
    SetServiceStatus(SERVICE_START_PENDING);

    m_status.dwWin32ExitCode = S_OK;
    m_status.dwCheckPoint = 0;
    m_status.dwWaitHint = 0;

    // When the Run function returns, the service has stopped.
    Run();

    SetServiceStatus(SERVICE_STOPPED);
    LogEvent(_T("Service stopped"));
}

inline void CServiceModule::Handler(DWORD dwOpcode)
{
	HRESULT hRes=CoInitialize(NULL);
	_ASSERTE(SUCCEEDED(hRes));

    switch (dwOpcode)
    {
    case SERVICE_CONTROL_STOP:
        SetServiceStatus(SERVICE_STOP_PENDING);
        PostThreadMessage(dwThreadID, WM_QUIT, 0, 0);
        break;
    case SERVICE_CONTROL_PAUSE:
        break;
    case SERVICE_CONTROL_CONTINUE:
        break;
    case SERVICE_CONTROL_INTERROGATE:
        break;
    case SERVICE_CONTROL_SHUTDOWN:
		ServiceShutDown () ;
	    break;
    default:
        LogEvent(_T("Bad service request"));
    }
    CoUninitialize();
}

void WINAPI CServiceModule::_ServiceMain(DWORD dwArgc, LPTSTR* lpszArgv)
{
    _Module.ServiceMain(dwArgc, lpszArgv);
}
void WINAPI CServiceModule::_Handler(DWORD dwOpcode)
{
    _Module.Handler(dwOpcode); 
}

void CServiceModule::SetServiceStatus(DWORD dwState)
{
    m_status.dwCurrentState = dwState;
    ::SetServiceStatus(m_hServiceStatus, &m_status);
}

void CServiceModule::Run()
{
    _Module.dwThreadID = GetCurrentThreadId();

    HRESULT hr = CoInitialize(NULL);
//  If you are running on NT 4.0 or higher you can use the following call
//  instead to make the EXE free threaded.
//  This means that calls come in on a random RPC thread
//  HRESULT hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);

    _ASSERTE(SUCCEEDED(hr));

    // This provides a NULL DACL which will allow access to everyone.
    CSecurityDescriptor sd;
    sd.InitializeFromThreadToken();
    hr = CoInitializeSecurity(NULL, -1, NULL, NULL,
        RPC_C_AUTHN_LEVEL_NONE, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, NULL);		//shield security
//    _ASSERTE(SUCCEEDED(hr));

    hr = _Module.RegisterClassObjects(CLSCTX_LOCAL_SERVER | CLSCTX_REMOTE_SERVER, REGCLS_MULTIPLEUSE);
    _ASSERTE(SUCCEEDED(hr));

    LogEvent(_T("Service started"));
    if (m_bService)
        SetServiceStatus(SERVICE_RUNNING);

//////////////////////////////////////////////////////////////////////////////////////////////////////////
	//initialize these globle pointer
	condevdb=new BSTR;
	mapdb=new BSTR;
	cpudb=new BSTR;
	memdb=new BSTR;
	filedb=new BSTR;
	osdb=new BSTR;
	devicedb=new BSTR;
	networkdb=new BSTR;
	statdb=new BSTR;
	netstatdb=new BSTR;

	//hereinafter, service register to database
	ServiceInit();		//get db ip and initialize event 
	DWORD dw;	
	CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)RegThreadProc, NULL, 0, &dw);	//in this thread: get pKey, register service...
//	SetEndSecurity(ServiceSourceID);

	CWinThread *PWinThread;
	if(bExit) goto mess;	//if disconnect to db, skip follow codes
connect:	HRESULT hRes;
			hRes=ConnectDb(&pIobject_mm);
			if(hRes!=S_OK)
			{
				Sleep(1000);
				goto connect;
			}
	//hereinafter, the first select map table and initialize the MapLink
	MAPL =new MapLink;

	MAPdb			*mapdb;
	int				i;
	int             offset;			offset=0;
	int             start;			start=0;      
	int             rec_number;		rec_number=0;
	db_table        in_table;
	tab_record		records;
	tables_union	*ret_records;

	char			IPMinstr[16], Host[STR32];
	in_table.db_operation = SELECT;
	in_table.encrypt_id = 0;

 	in_table.db_table_id = DB_INV | TABLE_INV_MAP;
	char sql_str[256];
	strcpy((char *)sql_str,"Select * from INV_MAPTAB");

	Sleep(2000);	//ensure get public key
	DesStrEn((unsigned char *)sql_str, (unsigned char *)in_table.sql_statement, (unsigned char *)pKey);
	do
	{
		start+=offset;
		hRes = pIobject_mm->av_dbsql(&in_table,&records,&rec_number,&offset,start);
		if (hRes == DB_SUCCEED)
		{
			ret_records = (tables_union *)records.records_buf;

			if(offset > 0){
				for(i=0;i<offset;i++){
					mapdb=new MAPdb;
					mapdb->ID =(ret_records+i)->table_inv_map.endsystem_id;
					strcpy((char *)IPMinstr,(char *)(ret_records+i)->table_inv_map.map_ip_addr);
					Tranvers_IP(IPMinstr);
					strcpy((char *)mapdb->IPAddress,(char *)IPMinstr);
					DesStrDe((unsigned char *)(ret_records+i)->table_inv_map.map_hostname, (unsigned char *)Host, (unsigned char *)pKey);
					strcpy((char *)mapdb->HostName,(char *)Host);
					mapdb->AvailableTag =(ret_records+i)->table_inv_map.map_available_flag;
					MAPL->AddNode (mapdb);
					delete mapdb;
					}
			}
		}
	}
	while(start+offset<rec_number);

	unsigned int code;
	code = GetCurrentThreadId();
	PingFlag=true;
	//start polling thread
	PWinThread=AfxBeginThread(Polling,&code);

/////////////////////////////////////////////////////////////////////////////////////////////////////////
mess: 	MSG msg;
   while (GetMessage(&msg, 0, 0, 0))
		{
			//major thread get the message from polling thread, 
			//modify the map table
			if(msg.message==WM_USER+100)
			{
				db_table		in_table;
				in_table.db_table_id = DB_INV | TABLE_INV_MAP;
				in_table.encrypt_id = 0;
				in_table.db_operation = UPDATE;
				sprintf((char *)sql_str,"update INV_MAPTAB set availabletag=0 where endsystemID=%ld",msg.lParam);
				DesStrEn((unsigned char *)sql_str, (unsigned char *)in_table.sql_statement, (unsigned char *)pKey);
				hRes = pIobject_mm->av_dbsql2(&in_table);
			}
			else
				DispatchMessage(&msg);
		}
	
	pIobject_mm->Release();	//release the pointer of sqlservice

	ServiceShutDown () ; //unregister service from database

    _Module.RevokeClassObjects();
    Sleep(dwPause); //wait for any threads to finish

    CoUninitialize();
	
}

extern "C" int WINAPI _tWinMain(HINSTANCE hInstance, 
    HINSTANCE /*hPrevInstance*/, LPTSTR lpCmdLine, int /*nShowCmd*/)
{
    lpCmdLine = GetCommandLine(); //this line necessary for _ATL_MIN_CRT
    _Module.Init(ObjectMap, hInstance, IDS_SERVICENAME, &LIBID_INVENTORYLib);
    _Module.m_bService = TRUE;

    TCHAR szTokens[] = _T("-/");

    LPCTSTR lpszToken = FindOneOf(lpCmdLine, szTokens);
    while (lpszToken != NULL)
    {
        if (lstrcmpi(lpszToken, _T("UnregServer"))==0)
            return _Module.UnregisterServer();

        // Register as Local Server
        if (lstrcmpi(lpszToken, _T("RegServer"))==0)
            return _Module.RegisterServer(TRUE, FALSE);
        
        // Register as Service
        if (lstrcmpi(lpszToken, _T("Service"))==0)
		{
			//if it's running on 98,cannot open service management,then exit
            int SysRet=_Module.RegisterServer(TRUE, TRUE);
			//Change the propriety:operate on the Registry directly.
			CRegKey prkey;
			if(prkey.Create(HKEY_LOCAL_MACHINE,"SYSTEM\\CurrentControlSet\\Services\\inventory")
				!=ERROR_SUCCESS)
			{
				MessageBox(NULL,"Cannot open Registry","Error: Registry",MB_OK);
				return -1;
			}
			
			if(prkey.SetValue(0x2,"Start")!=ERROR_SUCCESS)
			{
				MessageBox(NULL,"Cannot set the Registry's value","Error: Registry",MB_OK);
				return -1;
			}
			if(prkey.SetValue(0x20,"Type")!=ERROR_SUCCESS)
			{
				MessageBox(NULL,"Cannot set the Registry's value","Error: Registry",MB_OK);
				return -1;
			}
			prkey.Close();
			return SysRet;
		}        
        lpszToken = FindOneOf(lpszToken, szTokens);
    }
    // Are we Service or Local Server
    CRegKey keyAppID;
    LONG lRes = keyAppID.Open(HKEY_CLASSES_ROOT, _T("AppID"), KEY_READ);
    if (lRes != ERROR_SUCCESS)
        return lRes;

    CRegKey key;
    lRes = key.Open(keyAppID, _T("{BE43B812-198F-11D4-ADAC-00104B134DB9}"), KEY_READ);
    if (lRes != ERROR_SUCCESS)
        return lRes;

    TCHAR szValue[_MAX_PATH];
    DWORD dwLen = _MAX_PATH;
    lRes = key.QueryValue(szValue, _T("LocalService"), &dwLen);

    _Module.m_bService = FALSE;
    if (lRes == ERROR_SUCCESS)
        _Module.m_bService = TRUE;
    _Module.Start();

    // When we get here, the service has been stopped
    return _Module.m_status.dwWin32ExitCode;
}