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

📄 component c.cpp

📁 经验交流,从网上下载的好东西望大家分享
💻 CPP
字号:
// local.cpp
#define _WIN32_DCOM
#include <windows.h>
#include <stdio.h>
#include <conio.h>
#include <iaccess.h>   // IAccessControl
#include <iostream.h>  // For cout
#include "registry.h"  // For registry functions
#include "Component B\component.h" // Generated by MIDL

const IID IID_IAccessControl = {0xEEDD23E0,0x8410,0x11CE,{0xA1,0xC3,0x08,0x00,0x2B,0x2B,0x8D,0x8F}};

long g_cComponents = 0;
long g_cServerLocks = 0;
HANDLE g_hEvent;

#define MB_SERVICE_NOTIFICATION          0x00200000L

class CInsideCOM : public ISum
{
public:
	// IUnknown
	ULONG __stdcall AddRef();
	ULONG __stdcall Release();
	HRESULT __stdcall QueryInterface(REFIID riid, void** ppv);

	// ISum
	HRESULT __stdcall Sum(int x, int y, int* retval);

	CInsideCOM() : m_cRef(1) { g_cComponents++; }
	~CInsideCOM() { cout << "Component: CInsideCOM::~CInsideCOM()" << endl, g_cComponents--; }

private:
	ULONG m_cRef;
};

ULONG CInsideCOM::AddRef()
{
	cout << "Component: CInsideCOM::AddRef() m_cRef = " << m_cRef + 1 << endl;
	return ++m_cRef;
}

ULONG CInsideCOM::Release()
{
	cout << "Component: CInsideCOM::Release() m_cRef = " << m_cRef - 1 << endl;
	if(--m_cRef != 0)
		return m_cRef;
	SetEvent(g_hEvent);	// ADD THIS!!!
	delete this;
	return 0;
}

HRESULT CInsideCOM::QueryInterface(REFIID riid, void** ppv)
{
	if(riid == IID_IUnknown)
	{
		cout << "Component: CInsideCOM::QueryInterface() for IUnknown returning " << this << endl;
		*ppv = reinterpret_cast<IUnknown*>(this);
	}
	else if(riid == IID_ISum)
	{
		cout << "Component: CInsideCOM::QueryInterface() for ISum returning " << this << endl;
		*ppv = (ISum*)this;
	}
	else 
	{
		*ppv = NULL;
		return E_NOINTERFACE;
	}
	AddRef();
	return S_OK;
}

HRESULT CInsideCOM::Sum(int x, int y, int* retval)
{
	ISum* pSum = 0;
	IUnknown* pUnknown = 0;
	HRESULT hr;
	
	IServerSecurity* pServerSecurity;
	hr = CoGetCallContext(IID_IServerSecurity, (void**)&pServerSecurity);
	if(FAILED(hr))
		cout << "CoGetCallContext failed" << endl;


	DWORD AuthnSvc;
	DWORD AuthzSvc;
	OLECHAR* ServerPrincNam;
	DWORD AuthnLevel;
	RPC_AUTHZ_HANDLE Privs;
	DWORD Capabilities;

//	AuthzSvc is ignored when using the RPC_C_AUTHN_WINNT authentication service.
//                             RPC_C_AUTHN_WINNT, ignored,    Administrator, RPC_C_AUTHN_LEVEL_PKT, Thing4\Administrator, EOAC_NONE
	hr = pServerSecurity->QueryBlanket(&AuthnSvc, &AuthzSvc, &ServerPrincNam, &AuthnLevel, NULL, &Privs, &Capabilities);
//                                                                       impersonation level is never returned

	if(FAILED(hr))
		cout << "QueryBlanket failed" << endl;

	pServerSecurity->Release();

	switch(AuthnSvc)
	{
	case RPC_C_AUTHN_NONE:
		cout << "RPC_C_AUTHN_NONE ";
		break;
	case RPC_C_AUTHN_GSS_NEGOTIATE  :
		cout << "RPC_C_AUTHN_GSS_NEGOTIATE   ";
		break;
	case RPC_C_AUTHN_GSS_KERBEROS :
		cout << "RPC_C_AUTHN_GSS_KERBEROS  ";
		break;
	case RPC_C_AUTHN_WINNT:
		cout << "RPC_C_AUTHN_WINNT ";
		break;
	case RPC_C_AUTHN_DEFAULT:
		cout << "RPC_C_AUTHN_DEFAULT ";
		break;
	}

	switch(AuthzSvc)
	{
	case RPC_C_AUTHZ_NONE:
		cout << "RPC_C_AUTHZ_NONE ";
		break;
	case RPC_C_AUTHZ_NAME:
		cout << "RPC_C_AUTHZ_NAME ";
		break;
	case RPC_C_AUTHZ_DCE:
		cout << "RPC_C_AUTHZ_DCE ";
		break;
	}

	MessageBoxW(NULL, ServerPrincNam, L"ServerPrincName", MB_SERVICE_NOTIFICATION|MB_SETFOREGROUND|MB_OK);
	wprintf(L"ServerPrincNam %s\n", ServerPrincNam);

	switch(AuthnLevel)
	{
	case RPC_C_AUTHN_LEVEL_NONE:
		cout << "RPC_C_AUTHN_LEVEL_NONE ";
		break;
	case RPC_C_AUTHN_LEVEL_CONNECT:
		cout << "RPC_C_AUTHN_LEVEL_CONNECT ";
		break;
	case RPC_C_AUTHN_LEVEL_CALL:
		cout << "RPC_C_AUTHN_LEVEL_CALL ";
		break;
	case RPC_C_AUTHN_LEVEL_PKT:
		cout << "RPC_C_AUTHN_LEVEL_PKT ";
		break;
	case RPC_C_AUTHN_LEVEL_PKT_INTEGRITY:
		cout << "RPC_C_AUTHN_LEVEL_PKT_INTEGRITY ";
		break;
	case RPC_C_AUTHN_LEVEL_PKT_PRIVACY:
		cout << "RPC_C_AUTHN_LEVEL_PKT_PRIVACY ";
		break;
	}

	wprintf(L"Privs %s\n", Privs);
		MessageBoxW(NULL, (OLECHAR*)Privs, L"Privs", MB_SERVICE_NOTIFICATION|MB_SETFOREGROUND|MB_OK);

	if(Capabilities == EOAC_NONE)
		cout << "EOAC_NONE" << endl;

	CoTaskMemFree(ServerPrincNam);

	CoImpersonateClient();

	HANDLE handle = 0;
	handle = CreateFile("C:\\test.txt", GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
	if(handle != INVALID_HANDLE_VALUE)
	{
		char buffer[25];
		DWORD bytes_read = 0;
		ReadFile(handle, buffer, 25, &bytes_read, NULL);
		CloseHandle(handle);

		buffer[bytes_read] = 0;
		MessageBox(NULL, buffer, "Read from file", MB_SERVICE_NOTIFICATION|MB_SETFOREGROUND|MB_OK);
	}
	else
	{
		DWORD error = GetLastError();
		MessageBoxW(NULL, L"access denied to file", L"Read from file", MB_SERVICE_NOTIFICATION|MB_SETFOREGROUND|MB_OK);
		printf("Last error = %0x\n", error);
		cout << "Last error = " << error << endl;
		_getch();
	}

	CoRevertToSelf();

	cout << "Component: CInsideCOM::Sum() " << x << " + " << y << " = " << x + y << endl;
	*retval = x + y;

//	_getch();
	return S_OK;
}

class CFactory : public IClassFactory
{
public:
	// IUnknown
	ULONG __stdcall AddRef();
	ULONG __stdcall Release();
	HRESULT __stdcall QueryInterface(REFIID riid, void** ppv);

	// IClassFactory
	HRESULT __stdcall CreateInstance(IUnknown *pUnknownOuter, REFIID riid, void** ppv);
	HRESULT __stdcall LockServer(BOOL bLock);

	CFactory() : m_cRef(1) { }
	~CFactory() { }

private:
	ULONG m_cRef;
};

ULONG CFactory::AddRef()
{
	cout << "Component: CFactory::AddRef() m_cRef = " << m_cRef + 1 << endl;
	return ++m_cRef;
}

ULONG CFactory::Release()
{
	cout << "Component: CFactory::Release() m_cRef = " << m_cRef - 1 << endl;
	if(--m_cRef != 0)
		return m_cRef;
	delete this;
	return 0;
}

HRESULT CFactory::QueryInterface(REFIID riid, void** ppv)
{
	if((riid == IID_IUnknown) || (riid == IID_IClassFactory))
	{
		cout << "Component: CFactory::QueryInteface() for IUnknown or IClassFactory " << this << endl;
		*ppv = (IClassFactory*)this;
	}
	else
	{
		*ppv = NULL;
		return E_NOINTERFACE;
	}
	AddRef();
	return S_OK;
}

HRESULT CFactory::CreateInstance(IUnknown *pUnknownOuter, REFIID riid, void** ppv)
{
	if(pUnknownOuter != NULL)
		return CLASS_E_NOAGGREGATION;

	CInsideCOM *pInsideCOM = new CInsideCOM;
	cout << "Component: CFactory::CreateInstance() " << pInsideCOM << endl;

	if(pInsideCOM == NULL)
		return E_OUTOFMEMORY;

	// QueryInterface probably for IID_IUNKNOWN
	HRESULT hr = pInsideCOM->QueryInterface(riid, ppv);
	pInsideCOM->Release();
	return hr;
}

HRESULT CFactory::LockServer(BOOL bLock)
{
	if(bLock)
		g_cServerLocks++;
	else
		g_cServerLocks--;
	return S_OK;
}

void RegisterComponent()
{
	ITypeLib* pTypeLib;
	LoadTypeLibEx(L"component C.exe", REGKIND_DEFAULT, &pTypeLib);
	pTypeLib->Release();
	RegisterServer("component C.exe", CLSID_InsideCOM_C, "Inside COM+", "Component.InsideCOM_C", "Component.InsideCOM_C.1", NULL);
}

void CommandLineParameters(int argc, char** argv)
{
	RegisterComponent();
	if(argc < 2)
	{
		cout << "No parameter, but registered anyway" << endl;
		exit(false);
	}
	char* szToken = strtok(argv[1], "-/"); 
	if(_stricmp(szToken, "RegServer") == 0)
	{
		RegisterComponent();
		cout << "RegServer" << endl;
		exit(true);
	}
	if(_stricmp(szToken, "UnregServer") == 0)
	{
		UnRegisterTypeLib(LIBID_Component, 1, 0, LANG_NEUTRAL, SYS_WIN32);
		UnregisterServer(CLSID_InsideCOM_C, "Component.InsideCOM_C", "Component.InsideCOM_C.1");
		cout << "UnregServer" << endl;
		exit(true);
	}
	if(_stricmp(szToken, "Embedding") != 0)
	{
		cout << "Invalid parameter" << endl;
		exit(false);
	}
}

void main(int argc, char** argv)
{
	HRESULT hr;
	CommandLineParameters(argc, argv);

	cout << "Component: CoInitializeEx()" << endl;
	hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
	if(FAILED(hr))
		cout << "CoInitializeEx Failed" << endl;

	SOLE_AUTHENTICATION_SERVICE service;
	service.dwAuthnSvc = RPC_C_AUTHN_GSS_KERBEROS;
	service.dwAuthzSvc = RPC_C_AUTHZ_NONE;
	service.hr = 0;
	service.pPrincipalName = L"GuysDomain\\Administrator";

	hr = CoInitializeSecurity(0, 1, &service, NULL, RPC_C_AUTHN_LEVEL_CONNECT, RPC_C_IMP_LEVEL_DELEGATE, 
		NULL, EOAC_APPID, NULL);
	if(FAILED(hr))
	{
		printf("CoInitializeSecurity Failed = %0x\n", hr);
		_getch();
	}

	printf("CoInitializeSecurity service.hr = %0x\n", service.hr);

	IClassFactory *pClassFactory = new CFactory();

	cout << "Component: CoRegisterClassObject()" << endl;
	DWORD dwRegister;
	CoRegisterClassObject(CLSID_InsideCOM_C, pClassFactory, CLSCTX_LOCAL_SERVER, REGCLS_MULTIPLEUSE, &dwRegister);

	g_hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
	WaitForSingleObject(g_hEvent, INFINITE);

	CoRevokeClassObject(dwRegister);
	pClassFactory->Release();
	CoUninitialize();
//	_getch();
}

⌨️ 快捷键说明

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