1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361
|
/*
+----------------------------------------------------------------------+
| PHP Version 4 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2006 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Authors: Wez Furlong <wez@thebrainroom.com> |
+----------------------------------------------------------------------+
*/
/* $Id: classfactory.cpp,v 1.2.4.1.8.1 2006/01/01 13:47:01 sniper Exp $ */
/* IClassFactory Implementation, and DllXXX function implementation */
#define INITGUID
#define DEBUG_CLASS_FACTORY 0
#include <windows.h>
#include <initguid.h>
extern "C" {
HINSTANCE module_handle;
}
#include <comcat.h>
#include "TSRM.h"
#include "php4as_classfactory.h"
#include "php4as_scriptengine.h"
volatile LONG TPHPClassFactory::factory_count = 0;
volatile LONG TPHPClassFactory::object_count = 0;
/* {{{ Class Factory Implementation */
TPHPClassFactory::TPHPClassFactory()
{
m_refcount = 1;
InterlockedIncrement(const_cast<long*> (&factory_count));
}
TPHPClassFactory::~TPHPClassFactory()
{
InterlockedDecrement(const_cast<long*> (&factory_count));
}
void TPHPClassFactory::AddToObjectCount(void)
{
InterlockedIncrement(const_cast<long*> (&object_count));
}
void TPHPClassFactory::RemoveFromObjectCount(void)
{
InterlockedDecrement(const_cast<long*> (&object_count));
}
STDMETHODIMP_(DWORD) TPHPClassFactory::AddRef(void)
{
return InterlockedIncrement(const_cast<long*> (&m_refcount));
}
STDMETHODIMP_(DWORD) TPHPClassFactory::Release(void)
{
DWORD ret = InterlockedDecrement(const_cast<long*> (&m_refcount));
if (ret == 0)
delete this;
return ret;
}
STDMETHODIMP TPHPClassFactory::QueryInterface(REFIID iid, void **ppvObject)
{
*ppvObject = NULL;
if (IsEqualGUID(IID_IClassFactory, iid)) {
*ppvObject = (IClassFactory*)this;
} else if (IsEqualGUID(IID_IUnknown, iid)) {
*ppvObject = this;
}
if (*ppvObject) {
AddRef();
return S_OK;
}
return E_NOINTERFACE;
}
STDMETHODIMP TPHPClassFactory::LockServer(BOOL fLock)
{
return E_NOTIMPL;
}
STDMETHODIMP TPHPClassFactory::CreateInstance(IUnknown *pUnkOuter, REFIID iid, void **ppvObject)
{
TPHPScriptingEngine *engine = new TPHPScriptingEngine;
HRESULT ret = engine->QueryInterface(iid, ppvObject);
engine->Release();
return ret;
}
int TPHPClassFactory::CanUnload(void)
{
return (object_count + factory_count) == 0 ? 1 : 0;
}
/* }}} */
/* {{{ Registration structures */
struct reg_entry {
HKEY root_key;
char *sub_key;
char *value_name;
char *data;
};
struct reg_class {
/* REFIID here causes compilation errors */
const GUID *class_id;
char *class_name;
char *threading;
const struct reg_entry *reg;
const GUID **cats;
};
#define MAX_REG_SUBST 8
struct reg_subst {
int count;
char *names[MAX_REG_SUBST];
char *values[MAX_REG_SUBST];
};
/* }}} */
/* {{{ Registration information */
/* This registers the sapi as a scripting engine that can be instantiated using it's progid */
static const struct reg_entry engine_entries[] = {
{ HKEY_CLASSES_ROOT, "CLSID\\[CLSID]", NULL, "[CLASSNAME]" },
{ HKEY_CLASSES_ROOT, "CLSID\\[CLSID]\\InprocServer32", NULL, "[MODULENAME]" },
{ HKEY_CLASSES_ROOT, "CLSID\\[CLSID]\\InprocServer32", "ThreadingModel", "[THREADING]" },
{ HKEY_CLASSES_ROOT, "CLSID\\[CLSID]\\OLEScript", NULL, NULL },
{ HKEY_CLASSES_ROOT, "CLSID\\[CLSID]\\ProgID", NULL, "ActivePHP" },
{ HKEY_CLASSES_ROOT, "ActivePHP", NULL, "ActivePHP" },
{ HKEY_CLASSES_ROOT, "ActivePHP\\CLSID", NULL, "[CLSID]"},
{ HKEY_CLASSES_ROOT, "ActivePHP\\OLEScript", NULL, NULL},
{ 0, NULL, NULL, NULL }
};
static const GUID *script_engine_categories[] = {
&CATID_ActiveScript,
&CATID_ActiveScriptParse,
NULL
};
static const struct reg_class classes_to_register[] = {
{ &CLSID_PHPActiveScriptEngine, "PHP Active Script Engine", "Both", engine_entries, script_engine_categories },
{ NULL, NULL, NULL, 0, NULL }
};
/* }}} */
/* {{{ Registration code */
static void substitute(struct reg_subst *subst, char *srcstring, char *deststring)
{
int i;
char *srcpos = srcstring;
char *destpos = deststring;
while(1) {
char *repstart = strchr(srcpos, '[');
if (repstart == NULL) {
strcpy(destpos, srcpos);
break;
}
/* copy everything up until that character to the dest */
strncpy(destpos, srcpos, repstart - srcpos);
destpos += repstart - srcpos;
*destpos = 0;
/* search for replacement strings in the table */
for (i = 0; i < subst->count; i++) {
int len = strlen(subst->names[i]);
if (strncmp(subst->names[i], repstart + 1, len) == 0) {
/* append the replacement value to the buffer */
strcpy(destpos, subst->values[i]);
destpos += strlen(subst->values[i]);
srcpos = repstart + len + 2;
break;
}
}
}
#if DEBUG_CLASS_FACTORY
MessageBox(0, deststring, srcstring, MB_ICONHAND);
#endif
}
static int reg_string(BOOL do_reg, struct reg_subst *subst, const struct reg_entry *entry)
{
char subbuf[MAX_PATH], valuebuf[MAX_PATH], databuf[MAX_PATH];
char *sub = NULL, *value = NULL, *data = NULL;
LRESULT res;
HKEY hkey;
DWORD disp;
if (entry->sub_key) {
substitute(subst, entry->sub_key, subbuf);
sub = subbuf;
}
if (entry->value_name) {
substitute(subst, entry->value_name, valuebuf);
value = valuebuf;
}
if (entry->data) {
substitute(subst, entry->data, databuf);
data = databuf;
}
if (do_reg) {
res = RegCreateKeyEx(entry->root_key, sub, 0, NULL, REG_OPTION_NON_VOLATILE,
KEY_WRITE, NULL, &hkey, &disp);
if (res == NOERROR) {
if (data)
res = RegSetValueEx(hkey, value, 0, REG_SZ, (LPBYTE)data, strlen(data) + 1);
RegCloseKey(hkey);
}
} else {
res = RegDeleteKey(entry->root_key, sub);
}
return res == NOERROR ? 1 : 0;
}
static int perform_registration(BOOL do_reg)
{
char module_name[MAX_PATH];
char classid[40];
int ret = 1;
int i, j;
struct reg_subst subst = {0};
LPOLESTR classidw;
ICatRegister *catreg = NULL;
CATID cats[8];
int ncats;
CoInitialize(NULL);
CoCreateInstance(CLSID_StdComponentCategoriesMgr, NULL, CLSCTX_INPROC_SERVER, IID_ICatRegister, (LPVOID*)&catreg);
GetModuleFileName(module_handle, module_name, sizeof(module_name));
subst.names[0] = "CLSID";
subst.values[0] = classid;
subst.names[1] = "MODULENAME";
subst.values[1] = module_name;
subst.names[2] = "CLASSNAME";
subst.names[3] = "THREADING";
subst.count = 4;
for (i = 0; classes_to_register[i].class_id; i++) {
StringFromCLSID(*classes_to_register[i].class_id, &classidw);
WideCharToMultiByte(CP_ACP, 0, classidw, -1, classid, sizeof(classid), NULL, NULL);
classid[39] = 0;
CoTaskMemFree(classidw);
subst.values[2] = classes_to_register[i].class_name;
subst.values[3] = classes_to_register[i].threading;
if (catreg && classes_to_register[i].cats) {
ncats = 0;
while(classes_to_register[i].cats[ncats]) {
CopyMemory(&cats[ncats], classes_to_register[i].cats[ncats], sizeof(CATID));
ncats++;
}
}
if (do_reg) {
for (j = 0; classes_to_register[i].reg[j].root_key; j++) {
if (!reg_string(do_reg, &subst, &classes_to_register[i].reg[j])) {
ret = 0;
break;
}
}
if (catreg && ncats) {
catreg->RegisterClassImplCategories(*classes_to_register[i].class_id, ncats, cats);
}
} else {
if (catreg && ncats) {
catreg->UnRegisterClassImplCategories(*classes_to_register[i].class_id, ncats, cats);
}
/* do it in reverse order, so count the number of entries first */
for (j = 0; classes_to_register[i].reg[j].root_key; j++)
;
while(j-- > 0) {
if (!reg_string(do_reg, &subst, &classes_to_register[i].reg[j])) {
ret = 0;
break;
}
}
}
}
if (catreg)
catreg->Release();
CoUninitialize();
return ret;
}
/* }}} */
/* {{{ Standard COM server DllXXX entry points */
STDAPI DllCanUnloadNow(void)
{
if (TPHPClassFactory::CanUnload())
return S_OK;
return S_FALSE;
}
STDAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID* ppv)
{
HRESULT ret = E_FAIL;
if (IsEqualCLSID(CLSID_PHPActiveScriptEngine, rclsid)) {
TPHPClassFactory *factory = new TPHPClassFactory();
if (factory) {
ret = factory->QueryInterface(riid, ppv);
factory->Release();
}
}
return ret;
}
STDAPI DllRegisterServer(void)
{
return perform_registration(TRUE) ? S_OK : S_FALSE;
}
STDAPI DllUnregisterServer(void)
{
return perform_registration(FALSE) ? S_OK : S_FALSE;
}
/* }}} */
|