📄 hellocomadmin.cpp
字号:
//This is a very preliminary sample on COM+ Admin Objects.
//This is the first part in a two part series on using COM+ admin objects in VC++
//Depending on the response I would know what to code in part two
//Feel free to use this code as part of your application
//Do let me know if you do :)
/* Ranjeet Chakraborty(ranjeetc@hotmail.com) */
#include "stdafx.h"
#include <stdio.h>
#import "e:\\winnt\\system32\\com\\Comadmin.dll" no_namespace
void vcMsgBox(int x);
void vcMsgBox(char* psz);
void vcMsgBox(_bstr_t pwsz);
bool confirm(char* pszText, char* pszTitle);
void setRoleForCom(ICatalogCollectionPtr, ICatalogObjectPtr, _bstr_t, _bstr_t);
void installCom(ICOMAdminCatalogPtr, _bstr_t, _bstr_t);
void setRolesForApp(ICatalogCollectionPtr, ICatalogObjectPtr, _bstr_t);
void setConsStrForCom(ICatalogCollectionPtr, ICatalogObjectPtr, _bstr_t, _bstr_t);
/*------------CHANGE THE PATH OF THE DLL BEFORE COMPILING----------------*/
#define HELLOWORLD_DLL_PATH L"C:\\Users\\Ranjeet\\Source\\VC++\\HelloCOMAdmin\\HelloWorldVB\\HelloWorld.dll"
#define PROGID L"HelloWorld.Hello"
#define COMPLUSAPPNAME "HelloWorld"
#define ROLE1 L"Administrators"
#define ROLE2 L"Readers"
#define CONSTRUCTOR_STRING L"The HELLOWORLD constructor string"
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
CoInitialize (NULL);
try{
char pszEcho[] = "Here are the default settings:\nApplicationName: HelloWorld\nComponent's path: c:\\Users\\Ranjeet\\Source\\VC++\\HelloComAdmin\\HelloWorldVB\\HelloWorld.dll\nComponentProgID: HelloWorld.Hello\nRole1: Administrators\nRole2: Readers\nConstructorStrings: The HELLOWORLD constructor string\n\nCHANGE THE MACROS IN THE SOURCE CODE TO POINT THE RIGHT PATH !\n\nRUN THE APPLICATION AS AN LOCAL ADMINISTRATOR";
if(!confirm(pszEcho, "HelloComPlusAdmin Objects - Ranjeet Chakraborty"))
return 0;
//HRESULT hr ;
ICOMAdminCatalogPtr spCatalog("COMAdmin.COMAdminCatalog");
ICatalogCollectionPtr spApplications;
spApplications = spCatalog->GetCollection (L"Applications");
spApplications->Populate ();
ICatalogObjectPtr spHelloWorldApplication;
spHelloWorldApplication = spApplications->Add ();
_variant_t vntName(COMPLUSAPPNAME);
VARIANT vntAppKey;
spHelloWorldApplication->get_Key(&vntAppKey);
spHelloWorldApplication->put_Value (L"Name", vntName);
spApplications->SaveChanges ();
installCom(spCatalog,L"HelloWorld", HELLOWORLD_DLL_PATH);
setRolesForApp(spApplications, spHelloWorldApplication, ROLE1);
setRolesForApp(spApplications, spHelloWorldApplication, ROLE2);
setRoleForCom(spApplications, spHelloWorldApplication, PROGID, ROLE1);
setRoleForCom(spApplications, spHelloWorldApplication, PROGID, ROLE2);
setConsStrForCom(spApplications, spHelloWorldApplication, PROGID, CONSTRUCTOR_STRING);
}
catch(_com_error &e){
::MessageBox(0,(char*) e.Description(), "ERROR",0);
}
CoUninitialize ();
return 0;
}
void setRolesForApp(ICatalogCollectionPtr spApplications, ICatalogObjectPtr spApplication, _bstr_t pwszRole){
try{
ICatalogCollectionPtr spRoles;
VARIANT vntAppKey;
spApplication->get_Key (&vntAppKey);
spRoles = spApplications->GetCollection (L"Roles", &vntAppKey);
ICatalogObjectPtr spSomeRole;
spSomeRole = spRoles->Add ();
_variant_t vntRoleName(pwszRole);
spSomeRole ->put_Value (L"Name",vntRoleName);
spRoles->SaveChanges ();
}
catch(_com_error &e){
vcMsgBox(e.Description());
}
}
void installCom(ICOMAdminCatalogPtr spCatalog, _bstr_t pwszAppName, _bstr_t pwszPath){
try{
spCatalog->InstallComponent(pwszAppName, pwszPath, L"", L"");
}
catch(_com_error &e){
vcMsgBox(e.Description());
}
}
void setConsStrForCom(ICatalogCollectionPtr spApplications, ICatalogObjectPtr spApplication, _bstr_t pwzProgID, _bstr_t pwszConsStr){
try{
HRESULT hr;
ICatalogCollectionPtr spComponents;
VARIANT vntAppKey;
spApplication->get_Key (&vntAppKey);
spComponents = spApplications->GetCollection (L"Components", vntAppKey);
spComponents->Populate ();
ICatalogObjectPtr spComponent;
long lngCompCnt;
spComponents->get_Count (&lngCompCnt);
//vcMsgBox(lngCompCnt);
for(int j=0; j < lngCompCnt; j++){
IDispatch* pDispComObject;
hr = spComponents->get_Item (j,&pDispComObject);
spComponent = pDispComObject;
VARIANT vntRetName;
spComponent->get_Name(&vntRetName);
if(vntRetName.vt == VT_BSTR ){
//vcMsgBox(vntRetName.bstrVal) ;
_bstr_t pwszTemp = vntRetName.bstrVal;
if(pwzProgID == pwszTemp)
break;//vcMsgBox("It matches");
}
}
_variant_t vntConStr = pwszConsStr;
_variant_t vntBool = true;
spComponent->put_Value(L"ConstructionEnabled", vntBool);
spComponent->put_Value (L"ConstructorString", vntConStr );
spComponents->SaveChanges ();
}
catch(_com_error &e){
vcMsgBox(e.Description());
}
}
void setRoleForCom(ICatalogCollectionPtr spApplications, ICatalogObjectPtr spApplication, _bstr_t pwzProgID, _bstr_t pwszRole){
try{
HRESULT hr;
ICatalogCollectionPtr spComponents;
VARIANT vntAppKey;
spApplication->get_Key (&vntAppKey);
spComponents = spApplications->GetCollection (L"Components", vntAppKey);
spComponents->Populate ();
ICatalogObjectPtr spComponent;
long lngCompCnt;
spComponents->get_Count (&lngCompCnt);
//vcMsgBox(lngCompCnt);
for(int j=0; j < lngCompCnt; j++){
IDispatch* pDispComObject;
hr = spComponents->get_Item (j,&pDispComObject);
spComponent = pDispComObject;
VARIANT vntRetName;
spComponent->get_Name(&vntRetName);
if(vntRetName.vt == VT_BSTR ){
//vcMsgBox(vntRetName.bstrVal) ;
_bstr_t pwszTemp = vntRetName.bstrVal;
if(pwzProgID == pwszTemp)
break;//vcMsgBox("It matches");
}
}
ICatalogCollectionPtr spRolesForObject;
spRolesForObject = spComponents->GetCollection (L"RolesForComponent", spComponent->GetKey ());
ICatalogObjectPtr spComponentRole;
spComponentRole = spRolesForObject->Add ();
_variant_t vntRoleName = pwszRole;
spComponentRole->put_Value (L"Name", vntRoleName);
spRolesForObject->SaveChanges ();
//ICatalogObjectPtr spHelloWorldApplication;
VARIANT vntBlnAccessCheck, vntAccessLevel;
vntBlnAccessCheck.vt = VT_BOOL;
vntBlnAccessCheck.boolVal = TRUE;
spApplication->put_Value (L"ApplicationAccessChecksEnabled", vntBlnAccessCheck);
vntAccessLevel.vt = VT_INT;
vntAccessLevel.intVal = 1;
spApplication->put_Value (L"AccessLevel", vntAccessLevel);
spApplications->SaveChanges ();
VARIANT vntBlnComponentCheck;
vntBlnComponentCheck.vt = VT_BOOL;
vntBlnComponentCheck.boolVal = TRUE;
//spComponent->put_Value (L"ComponentAccessChecksEnabled", vntBlnComponentCheck);
spComponents->SaveChanges ();
}
catch(_com_error &e){
vcMsgBox(e.Description());
}
}
bool confirm(char* pszText, char* pszTitle){
int retVal = MessageBox(0,pszText, pszTitle, 1) ;
if (retVal == 1)
return true;
else
return false;
}
void vcMsgBox(int x){
char psz[100];
sprintf(psz,"%d",x);
MessageBox(0,psz, "Value :", 0);
}
void vcMsgBox(char* psz){
MessageBox(0,psz, "Value :", 0);
}
void vcMsgBox(_bstr_t pwsz){
MessageBoxW(0,pwsz, L"Value :", 0);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -