local with custom IAccessControl.cpp
上传用户:bjlvip
上传日期:2010-02-08
资源大小:744k
文件大小:10k
源码类别:
Windows编程
开发平台:
Visual C++
- // local.cpp
- #define _WIN32_WINNT 0x0403
- #define _WIN32_DCOM
- #include <windows.h>
- #include <stdio.h>
- #include <iaccess.h> // IAccessControl
- #include <iostream.h> // For cout
- #include "registry.h" // For registry functions
- #include "Component IAccessControlcomponent.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;
- class CMyAccessControl : public IAccessControl
- {
- public:
- // IUnknown
- ULONG __stdcall AddRef();
- ULONG __stdcall Release();
- HRESULT __stdcall QueryInterface(REFIID riid, void** ppv);
- // IAccessControl
- HRESULT __stdcall GrantAccessRights(PACTRL_ACCESSW pAccessList);
- HRESULT __stdcall SetAccessRights(PACTRL_ACCESSW pAccessList);
- HRESULT __stdcall SetOwner(PTRUSTEEW pOwner, PTRUSTEEW pGroup);
- HRESULT __stdcall RevokeAccessRights(LPWSTR lpProperty, ULONG cTrustees, TRUSTEEW prgTrusteesp[]);
- HRESULT __stdcall GetAllAccessRights(LPWSTR lpProperty, PACTRL_ACCESSW_ALLOCATE_ALL_NODES* ppAccessList, PTRUSTEEW* ppOwner, PTRUSTEEW* ppGroup);
- HRESULT __stdcall IsAccessAllowed(PTRUSTEEW pTrustee, LPWSTR lpProperty, ACCESS_RIGHTS AccessRights, BOOL* pfAccessAllowed);
- private:
- ULONG m_cRef;
- };
- HRESULT CMyAccessControl::GrantAccessRights(PACTRL_ACCESSW pAccessList)
- {
- return E_NOTIMPL;
- }
- HRESULT CMyAccessControl::SetAccessRights(PACTRL_ACCESSW pAccessList)
- {
- return E_NOTIMPL;
- }
- HRESULT CMyAccessControl::SetOwner(PTRUSTEEW pOwner, PTRUSTEEW pGroup)
- {
- return E_NOTIMPL;
- }
- HRESULT CMyAccessControl::RevokeAccessRights(LPWSTR lpProperty, ULONG cTrustees, TRUSTEEW prgTrusteesp[])
- {
- return E_NOTIMPL;
- }
- HRESULT CMyAccessControl::GetAllAccessRights(LPWSTR lpProperty, PACTRL_ACCESSW_ALLOCATE_ALL_NODES* ppAccessList, PTRUSTEEW* ppOwner, PTRUSTEEW* ppGroup)
- {
- return E_NOTIMPL;
- }
- HRESULT CMyAccessControl::IsAccessAllowed(PTRUSTEEW pTrustee, LPWSTR lpProperty, ACCESS_RIGHTS AccessRights, BOOL* pfAccessAllowed)
- {
- if(MessageBoxW(NULL, pTrustee->ptstrName, L"Grant permission?", MB_SERVICE_NOTIFICATION | MB_SETFOREGROUND | MB_YESNO) == IDYES)
- *pfAccessAllowed = TRUE;
- else
- *pfAccessAllowed = FALSE;
- return S_OK;
- }
- ULONG CMyAccessControl::AddRef()
- {
- return ++m_cRef;
- }
- ULONG CMyAccessControl::Release()
- {
- if(--m_cRef != 0)
- return m_cRef;
- delete this;
- return 0;
- }
- HRESULT CMyAccessControl::QueryInterface(REFIID riid, void** ppv)
- {
- if(riid == IID_IUnknown || riid == IID_IAccessControl)
- *ppv = reinterpret_cast<IUnknown*>(this);
- else
- {
- *ppv = NULL;
- return E_NOINTERFACE;
- }
- AddRef();
- return S_OK;
- }
- 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)
- {
- 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, Thing4Administrator, 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_DCE_PRIVATE:
- cout << "RPC_C_AUTHN_DCE_PRIVATE ";
- break;
- case RPC_C_AUTHN_DCE_PUBLIC:
- cout << "RPC_C_AUTHN_DCE_PUBLIC ";
- break;
- case RPC_C_AUTHN_WINNT:
- cout << "RPC_C_AUTHN_WINNT ";
- break;
- case RPC_C_AUTHN_DEFAULT:
- cout << "RPC_C_AUTHN_DEFAULT ";
- break;
- }
- switch(AuthnSvc)
- {
- 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;
- }
- wprintf(L"ServerPrincNam %sn", 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 %sn", Privs);
- if(Capabilities == EOAC_NONE)
- cout << "EOAC_NONE" << endl;
- CoTaskMemFree(ServerPrincNam);
- cout << "Component: CInsideCOM::Sum() " << x << " + " << y << " = " << x + y << endl;
- *retval = x + y;
- 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 IAccessControl.exe", REGKIND_DEFAULT, &pTypeLib);
- pTypeLib->Release();
- RegisterServer("Component IAccessControl.exe", CLSID_InsideCOM, "Inside COM Sample #1", "Component.InsideCOM", "Component.InsideCOM.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, "Component.InsideCOM", "Component.InsideCOM.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;
- CMyAccessControl ac;
- hr = CoInitializeSecurity(&ac, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_CONNECT, RPC_C_IMP_LEVEL_IDENTIFY,
- NULL, EOAC_ACCESS_CONTROL, NULL);
- if(FAILED(hr))
- {
- printf("CoInitializeSecurity Failed = %0xn", hr);
- exit(0);
- }
- IClassFactory *pClassFactory = new CFactory();
- cout << "Component: CoRegisterClassObject()" << endl;
- DWORD dwRegister;
- CoRegisterClassObject(CLSID_InsideCOM, pClassFactory, CLSCTX_LOCAL_SERVER, REGCLS_MULTIPLEUSE, &dwRegister);
- g_hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
- WaitForSingleObject(g_hEvent, INFINITE);
- CoRevokeClassObject(dwRegister);
- pClassFactory->Release();
- CoUninitialize();
- }