From: <mie...@us...> - 2006-12-23 07:11:24
|
Revision: 92 http://svn.sourceforge.net/oorexx/?rev=92&view=rev Author: miesfeld Date: 2006-12-22 23:11:23 -0800 (Fri, 22 Dec 2006) Log Message: ----------- The third (part 1) of 3 commits to make the file naming format in the ooRexxScript extension consistent with the file naming format in the ooRexx kernel Note: because of the case insensitivity on Windows, you will need to take an extra step to bring your working copy up to date with this commit. This is only for a working copy on a Windows box, it does not apply if the working copy is on a non-Windows box. To update your working copy with this commit: cd interpreter\extensions\platform\windows\oorexxscript svn update classfactory.cpp svn update Alternately, you can update for all 4 parts of this commit by: cd interpreter\extensions\platform\windows\oorexxscript svn update * svn update Added Paths: ----------- interpreter/trunk/extensions/platform/windows/oorexxscript/Classfactory.cpp Removed Paths: ------------- interpreter/trunk/extensions/platform/windows/oorexxscript/classfactory.cpp Copied: interpreter/trunk/extensions/platform/windows/oorexxscript/Classfactory.cpp (from rev 91, interpreter/trunk/extensions/platform/windows/oorexxscript/classfactory.cpp) =================================================================== --- interpreter/trunk/extensions/platform/windows/oorexxscript/Classfactory.cpp (rev 0) +++ interpreter/trunk/extensions/platform/windows/oorexxscript/Classfactory.cpp 2006-12-23 07:11:23 UTC (rev 92) @@ -0,0 +1,613 @@ +/*----------------------------------------------------------------------------*/ +/* */ +/* Copyright (c) 1995, 2004 IBM Corporation. All rights reserved. */ +/* Copyright (c) 2005-2006 Rexx Language Association. All rights reserved. */ +/* */ +/* This program and the accompanying materials are made available under */ +/* the terms of the Common Public License v1.0 which accompanies this */ +/* distribution. A copy is also available at the following address: */ +/* http://www.ibm.com/developerworks/oss/CPLv1.0.htm */ +/* */ +/* Redistribution and use in source and binary forms, with or */ +/* without modification, are permitted provided that the following */ +/* conditions are met: */ +/* */ +/* Redistributions of source code must retain the above copyright */ +/* notice, this list of conditions and the following disclaimer. */ +/* Redistributions in binary form must reproduce the above copyright */ +/* notice, this list of conditions and the following disclaimer in */ +/* the documentation and/or other materials provided with the distribution. */ +/* */ +/* Neither the name of Rexx Language Association nor the names */ +/* of its contributors may be used to endorse or promote products */ +/* derived from this software without specific prior written permission. */ +/* */ +/* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS */ +/* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT */ +/* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS */ +/* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT */ +/* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, */ +/* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED */ +/* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, */ +/* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY */ +/* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING */ +/* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS */ +/* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +/* */ +/*----------------------------------------------------------------------------*/ +#include "ClassFactory.hpp" + + +/* constants used for class registration */ +#define szSERVERKEY "InprocServer32" +#define szTHREADMODEL "Both" + +#ifdef _NO_SCRIPT_GUIDS + #error "no, this is not allowed" +#endif + +/*---------------------------------------------------------------------------*/ + +/* Helper function to create a component category and associated description */ +HRESULT CreateComponentCategory(CATID catid, OLECHAR* catDescription) +{ + + ICatRegister *pcr = NULL; + HRESULT hr = S_OK; + CATEGORYINFO catinfo; + int iLen; + + hr = CoCreateInstance(CLSID_StdComponentCategoriesMgr, + NULL, CLSCTX_INPROC_SERVER, IID_ICatRegister, + (void**)&pcr); + if (FAILED(hr)) + return hr; + + /* Make sure the HKCR\Component Categories\{..catid...} key is registered */ + catinfo.catid = catid; + catinfo.lcid = 0x0409 ; // english + + /* Make sure the provided description is not too long (max 127 chars) */ + iLen = wcslen(catDescription); + if (iLen>127) + iLen = 127; + + wcsncpy(catinfo.szDescription, catDescription, iLen); + + /* Make sure the description is null terminated */ + catinfo.szDescription[iLen] = '\0'; + + hr = pcr->RegisterCategories(1, &catinfo); + pcr->Release(); + + return hr; +} /* end of function CreateComponentCategory */ + + +/* Helper function to register a CLSID as belonging to a component category */ +HRESULT RegisterCLSIDInCategory(REFCLSID clsid, CATID catid) +{ + /* Register your component categories information. */ + ICatRegister *pcr = NULL ; + HRESULT hr = S_OK ; + + hr = CoCreateInstance(CLSID_StdComponentCategoriesMgr, + NULL, CLSCTX_INPROC_SERVER, IID_ICatRegister, + (void**)&pcr); + if (SUCCEEDED(hr)) + { + /* Register this category as being "implemented" by the class. */ + CATID rgcatid[1]; + + rgcatid[0] = catid; + hr = pcr->RegisterClassImplCategories(clsid, 1, rgcatid); + } /* endif */ + + if (pcr != NULL) + pcr->Release(); + + return hr; +} /* end of function RegisterCLSIDInCategory */ + + +/* Helper function to unregister a CLSID as belonging to a component category */ +HRESULT UnRegisterCLSIDInCategory(REFCLSID clsid, CATID catid) +{ + ICatRegister *pcr = NULL ; + HRESULT hr = S_OK ; + + hr = CoCreateInstance(CLSID_StdComponentCategoriesMgr, + NULL, CLSCTX_INPROC_SERVER, IID_ICatRegister, + (void**)&pcr); + if (SUCCEEDED(hr)) + { + /* Unregister this category as being "implemented" by the class. */ + CATID rgcatid[1]; + + rgcatid[0] = catid; + hr = pcr->UnRegisterClassImplCategories(clsid, 1, rgcatid); + } /* endif */ + + if (pcr != NULL) + pcr->Release(); + + return hr; +} /* end of function UnRegisterCLSIDInCategory */ + + +/* StringFromGuidA returns an ANSI string from a CLSID or GUID */ +int StringFromGuidA(REFIID riid, LPSTR pszBuf) +{ + return sprintf((char *)pszBuf, + "{%08lX-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}", + riid.Data1, riid.Data2, riid.Data3, riid.Data4[0], + riid.Data4[1], riid.Data4[2], riid.Data4[3], riid.Data4[4], + riid.Data4[5], riid.Data4[6], riid.Data4[7]); +} + +/*---------------------------------------------------------------------------*/ + +STDMETHODIMP ooRexxClassFactory::QueryInterface(REFIID riid, void** ppvObj) +{ + + char cIID[100],*IIDName,TrulyUnknown[]="??????"; + + StringFromGUID2(riid,(unsigned short *)cIID,sizeof(cIID)/2); + if (ppvObj == 0) + { + return E_POINTER; + } + *ppvObj = NULL; + + if (riid == IID_IUnknown) + { + *ppvObj = (IUnknown *) this; + } + else if (riid == IID_IClassFactory) + { + *ppvObj = (IClassFactory *) this; + } + + if (*ppvObj == NULL) + { + return ResultFromScode(E_NOINTERFACE); + } + else + { + ((IUnknown*) *ppvObj)->AddRef(); + return NOERROR; + } +} + + +STDMETHODIMP_(ULONG) ooRexxClassFactory::AddRef(void) +{ + return ++ulRefCounter; +} + + +STDMETHODIMP_(ULONG) ooRexxClassFactory::Release(void) +{ + ULONG ulTempRefCounter; + + ulTempRefCounter = --ulRefCounter; + + if (ulRefCounter == 0) + delete this; + + return ulTempRefCounter; +} + + +STDMETHODIMP ooRexxClassFactory::LockServer(bool fLock) +{ + if (fLock) + InterlockedIncrement((long *)&ulDllLocks); //++ulDllLocks; + else + InterlockedDecrement((long *)&ulDllLocks); //--ulDllLocks; + + return NOERROR; +} + + +STDMETHODIMP ooRexxClassFactory::RegisterServer() +{ + HKEY hk; + HKEY hkSub; + HKEY hkSub2; + HKEY hkthread; + HRESULT hr = NOERROR; + const int REGPATHMAX = 512; + char szDllPath[REGPATHMAX]; + ULONG ulDllPathLen; + + /* Make a clean start */ + UnregisterServer(); + + /* Create scripting categories if they do not exist yet */ + CreateComponentCategory(CATID_ActiveScript, + OLESTR("Active Scripting Engine")); + CreateComponentCategory(CATID_ActiveScriptParse, + OLESTR("Active Scripting Engine with Parsing")); + + /* Register ourselves in the scripting categories */ + RegisterCLSIDInCategory(guidCLASSID, CATID_ActiveScript); + RegisterCLSIDInCategory(guidCLASSID, CATID_ActiveScriptParse); + + /* Register server info */ + if (RegCreateKey(HKEY_CLASSES_ROOT, szLANGNAME, &hk) == ERROR_SUCCESS) + { + if (RegCreateKey(hk, "OLEScript", &hkSub) != ERROR_SUCCESS) + { + RegCloseKey(hk); + return ResultFromScode(E_FAIL); + } + + RegCloseKey(hkSub); + + RegSetValue(HKEY_CLASSES_ROOT, szLANGNAME, REG_SZ, szDESCRIPTION, + strlen(szDESCRIPTION)); + RegSetValue(hk, "CLSID", REG_SZ, szCLASSID, strlen(szCLASSID)); + RegCloseKey(hk); + } + else + return ResultFromScode(E_FAIL); + + /* Register "alternate name" info */ + if (szALTERNATELANGNAME) + { + if (RegCreateKey(HKEY_CLASSES_ROOT, szALTERNATELANGNAME, &hk) == ERROR_SUCCESS) + { + if (RegCreateKey(hk, "OLEScript", &hkSub) != ERROR_SUCCESS) + { + RegCloseKey(hk); + return ResultFromScode(E_FAIL); + } + + RegCloseKey(hkSub); + RegSetValue(HKEY_CLASSES_ROOT, szALTERNATELANGNAME, REG_SZ, + szDESCRIPTION, strlen(szDESCRIPTION)); + RegSetValue (hk, "CLSID", REG_SZ, szCLASSID, strlen(szCLASSID)); + RegCloseKey (hk); + } + else + return ResultFromScode(E_FAIL); + } + + /* Register createable class */ + if (RegCreateKey(HKEY_CLASSES_ROOT, "CLSID", &hk) == ERROR_SUCCESS) + { + if (RegCreateKey(hk, szCLASSID, &hkSub) != ERROR_SUCCESS) + { + RegCloseKey(hk); + return ResultFromScode (E_FAIL); + } + + if (RegCreateKey(hkSub, "OLEScript", &hkSub2) != ERROR_SUCCESS) + { + RegCloseKey(hk); + RegCloseKey(hkSub); + return ResultFromScode(E_FAIL); + } + + ulDllPathLen = (ULONG) GetModuleFileName(GetModuleHandle(szDLLNAME), + szDllPath, REGPATHMAX); + + if (!ulDllPathLen) + { + RegCloseKey(hkSub2); + RegCloseKey(hkSub); + RegCloseKey(hk); + return ResultFromScode(E_FAIL); + } + + RegSetValue(hk, szCLASSID, REG_SZ, szDESCRIPTION, strlen(szDESCRIPTION)); + RegSetValue(hkSub, "ProgID", REG_SZ, szLANGNAME, strlen(szLANGNAME)); + RegSetValue(hkSub, szSERVERKEY, REG_SZ, szDllPath, ulDllPathLen); + + /* To set the threading model reg entry use the RegOpenKeyEx & SetValueEx */ + if (RegOpenKeyEx(hkSub, szSERVERKEY, 0, KEY_SET_VALUE, &hkthread) == ERROR_SUCCESS) + { + RegSetValueEx(hkthread, "ThreadingModel", 0, REG_SZ, + (BYTE *) szTHREADMODEL, strlen(szTHREADMODEL)); + RegCloseKey(hkthread); + } + else + { + RegCloseKey(hkthread); + RegCloseKey(hkSub2); + RegCloseKey(hkSub); + RegCloseKey (hk); + return ResultFromScode(E_FAIL); + } + + RegCloseKey(hkSub2); + RegCloseKey(hkSub); + RegCloseKey(hk); + } + else + return ResultFromScode(E_FAIL); + + /* Register the extension */ + if (szEXTENSION) + { + if (RegSetValue(HKEY_CLASSES_ROOT, szEXTENSION, + REG_SZ, szLANGFILE, strlen(szLANGFILE)) != ERROR_SUCCESS) + return ResultFromScode(E_FAIL); + } + + /* Register the language file */ + if (szLANGFILE) + { + if (RegCreateKey(HKEY_CLASSES_ROOT, szLANGFILE, &hk) == ERROR_SUCCESS) + { + if (RegCreateKey(hk, "ScriptEngine", &hkSub) != ERROR_SUCCESS) + { + RegCloseKey(hk); + return ResultFromScode(E_FAIL); + } + + RegCloseKey(hkSub); + + if (RegCreateKey(hk, "DefaultIcon", &hkSub) != ERROR_SUCCESS) + { + RegCloseKey(hk); + return ResultFromScode(E_FAIL); + } + + RegSetValueEx(hkSub, NULL, 0, REG_EXPAND_SZ, (const unsigned char *) szDEFAULTICON, strlen(szDEFAULTICON)); + RegCloseKey(hkSub); + + RegSetValue(HKEY_CLASSES_ROOT, szLANGFILE, REG_SZ, szFILEDESCRIPTION, + strlen(szFILEDESCRIPTION)); + RegSetValue(hk, "ScriptEngine", REG_SZ, szLANGNAME, strlen(szLANGNAME)); + createSubKeys(hk); + RegCloseKey(hk); + } + else + return ResultFromScode(E_FAIL); + } + + return S_OK; +} + + +STDMETHODIMP ooRexxClassFactory::UnregisterServer() +{ + HKEY hk; + HKEY hkSub; + HKEY hkSub2; + HRESULT hr = S_OK; + + /* UnRegister ourselves from the category */ + UnRegisterCLSIDInCategory(guidCLASSID, CATID_ActiveScript); + UnRegisterCLSIDInCategory(guidCLASSID, CATID_ActiveScriptParse); + + /* delete server info */ + if (RegOpenKey(HKEY_CLASSES_ROOT, szLANGNAME, &hk) != ERROR_SUCCESS) + { + hr = ResultFromScode(E_FAIL); + } + else + { + if (RegDeleteKey(hk, "CLSID") != ERROR_SUCCESS) + { + hr = ResultFromScode(E_FAIL); + } /* endif */ + + if (RegDeleteKey(hk, "OLEScript") != ERROR_SUCCESS) + { + hr = ResultFromScode (E_FAIL); + } /* endif */ + + RegCloseKey(hk); + + if (RegDeleteKey(HKEY_CLASSES_ROOT, szLANGNAME) != ERROR_SUCCESS) + { + hr = ResultFromScode(E_FAIL); + } /* endif */ + } + + /* delete alternate name entry */ + if (szALTERNATELANGNAME) + { + if (RegOpenKey(HKEY_CLASSES_ROOT, szALTERNATELANGNAME, &hk) != ERROR_SUCCESS) + { + hr = ResultFromScode(E_FAIL); + } + else + { + if (RegDeleteKey(hk, "CLSID") != ERROR_SUCCESS) + { + hr = ResultFromScode(E_FAIL); + } /* endif */ + + if (RegDeleteKey(hk, "OLEScript") != ERROR_SUCCESS) + { + hr = ResultFromScode(E_FAIL); + } /* endif */ + + RegCloseKey(hk); + + if (RegDeleteKey(HKEY_CLASSES_ROOT, szALTERNATELANGNAME) != ERROR_SUCCESS) + { + hr = ResultFromScode(E_FAIL); + } /* endif */ + } + } + + /* delete entries for CLSID */ + if (RegCreateKey(HKEY_CLASSES_ROOT, "CLSID", &hk) != ERROR_SUCCESS) + { + hr = ResultFromScode(E_FAIL); + } + else + { + if (RegOpenKey(hk, szCLASSID, &hkSub) != ERROR_SUCCESS) + { + hr = ResultFromScode(E_FAIL); + } + else + { + if (RegDeleteKey(hkSub, "ProgID") != ERROR_SUCCESS) + { + hr = ResultFromScode(E_FAIL); + } /* endif */ + + if (RegDeleteKey(hkSub, "OLEScript") != ERROR_SUCCESS) + { + hr = ResultFromScode(E_FAIL); + } /* endif */ + + if (RegDeleteKey(hkSub, "Implemented Categories") != ERROR_SUCCESS) + { + hr = ResultFromScode(E_FAIL); + } /* endif */ + + if (RegDeleteKey(hkSub, szSERVERKEY) != ERROR_SUCCESS) + { + hr = ResultFromScode(E_FAIL); + } /* endif */ + + RegCloseKey(hkSub); + } + + if (RegDeleteKey(hk, szCLASSID) != ERROR_SUCCESS) + { + hr = ResultFromScode(E_FAIL); + } /* endif */ + + RegCloseKey(hk); + } + + if (szEXTENSION) + { + /* delete extension */ + if (RegDeleteKey(HKEY_CLASSES_ROOT, szEXTENSION) != ERROR_SUCCESS) + { + hr = ResultFromScode(E_FAIL); + } /* endif */ + } + + if (szLANGFILE) + { + if (RegOpenKey(HKEY_CLASSES_ROOT, szLANGFILE, &hk) != ERROR_SUCCESS) + { + hr = ResultFromScode(E_FAIL); + } + else + { + deleteSubKeys(hk); + if (RegDeleteKey(hk, "ScriptEngine") != ERROR_SUCCESS) + { + hr = ResultFromScode(E_FAIL); + } + if (RegDeleteKey(hk, "DefaultIcon") != ERROR_SUCCESS) + { + hr = ResultFromScode(E_FAIL); + } + RegCloseKey(hk); + } + if (RegDeleteKey(HKEY_CLASSES_ROOT, szLANGFILE) != ERROR_SUCCESS) + { + hr = ResultFromScode(E_FAIL); + } + } + + return hr; +} + +HRESULT ooRexxClassFactory::deleteSubKeys(HKEY parent) +{ + HKEY hk1, hk2; + HRESULT hResult = E_FAIL; + + if (RegOpenKey(parent, "Shell", &hk1) == ERROR_SUCCESS) + { + // delete EDIT entry + if (RegOpenKey(hk1, "Edit", &hk2) == ERROR_SUCCESS) + { + RegDeleteKey(hk2, "Command"); + RegCloseKey(hk2); + RegDeleteKey(hk1, "Edit"); + } + // delete OPEN entry + if (RegOpenKey(hk1, "Open", &hk2) == ERROR_SUCCESS) + { + RegDeleteKey(hk2, "Command"); + RegCloseKey(hk2); + RegDeleteKey(hk1, "Open"); + } + // delete OPEN2 entry + if (RegOpenKey(hk1, "Open2", &hk2) == ERROR_SUCCESS) + { + RegDeleteKey(hk2, "Command"); + RegCloseKey(hk2); + RegDeleteKey(hk1, "Open2"); + } + // delete PRINT entry + if (RegOpenKey(hk1, "Print", &hk2) == ERROR_SUCCESS) + { + RegDeleteKey(hk2, "Command"); + RegCloseKey(hk2); + RegDeleteKey(hk1, "Print"); + } + // delete SHELL entry + RegCloseKey(hk1); + if (RegDeleteKey(parent, "Shell") == ERROR_SUCCESS) + { + hResult = S_OK; + } + } + return hResult; +} + +HRESULT ooRexxClassFactory::createSubKeys(HKEY parent) +{ + HKEY hk1, hk2, hk3; + HRESULT hResult = E_FAIL; + + if (RegCreateKey(parent, "Shell", &hk1) == ERROR_SUCCESS) + { + hResult = S_OK; + // create EDIT entry + if (RegCreateKey(hk1, "Edit", &hk2) == ERROR_SUCCESS) + { + if (RegCreateKey(hk2, "Command", &hk3) == ERROR_SUCCESS) + { + RegSetValueEx(hk3, NULL, 0, REG_EXPAND_SZ, (const unsigned char *) szSHELLEDIT, strlen(szSHELLEDIT)); + RegCloseKey(hk3); + } + RegCloseKey(hk2); + } + // create OPEN entry + if (RegCreateKey(hk1, "Open", &hk2) == ERROR_SUCCESS) + { + if (RegCreateKey(hk2, "Command", &hk3) == ERROR_SUCCESS) + { + RegSetValueEx(hk3, NULL, 0, REG_EXPAND_SZ, (const unsigned char *) szSHELLOPEN, strlen(szSHELLOPEN)); + RegCloseKey(hk3); + } + } + // create OPEN2 entry + if (RegCreateKey(hk1, "Open2", &hk2) == ERROR_SUCCESS) + { + if (RegCreateKey(hk2, "Command", &hk3) == ERROR_SUCCESS) + { + RegSetValueEx(hk3, NULL, 0, REG_EXPAND_SZ, (const unsigned char *) szSHELLOPEN2, strlen(szSHELLOPEN2)); + RegCloseKey(hk3); + } + } + // create PRINT entry + if (RegCreateKey(hk1, "Print", &hk2) == ERROR_SUCCESS) + { + if (RegCreateKey(hk2, "Command", &hk3) == ERROR_SUCCESS) + { + RegSetValueEx(hk3, NULL, 0, REG_EXPAND_SZ, (const unsigned char *) szSHELLPRINT, strlen(szSHELLPRINT)); + RegCloseKey(hk3); + } + } + RegCloseKey(hk1); + } + return hResult; +} Deleted: interpreter/trunk/extensions/platform/windows/oorexxscript/classfactory.cpp =================================================================== --- interpreter/trunk/extensions/platform/windows/oorexxscript/classfactory.cpp 2006-12-23 06:46:25 UTC (rev 91) +++ interpreter/trunk/extensions/platform/windows/oorexxscript/classfactory.cpp 2006-12-23 07:11:23 UTC (rev 92) @@ -1,613 +0,0 @@ -/*----------------------------------------------------------------------------*/ -/* */ -/* Copyright (c) 1995, 2004 IBM Corporation. All rights reserved. */ -/* Copyright (c) 2005-2006 Rexx Language Association. All rights reserved. */ -/* */ -/* This program and the accompanying materials are made available under */ -/* the terms of the Common Public License v1.0 which accompanies this */ -/* distribution. A copy is also available at the following address: */ -/* http://www.ibm.com/developerworks/oss/CPLv1.0.htm */ -/* */ -/* Redistribution and use in source and binary forms, with or */ -/* without modification, are permitted provided that the following */ -/* conditions are met: */ -/* */ -/* Redistributions of source code must retain the above copyright */ -/* notice, this list of conditions and the following disclaimer. */ -/* Redistributions in binary form must reproduce the above copyright */ -/* notice, this list of conditions and the following disclaimer in */ -/* the documentation and/or other materials provided with the distribution. */ -/* */ -/* Neither the name of Rexx Language Association nor the names */ -/* of its contributors may be used to endorse or promote products */ -/* derived from this software without specific prior written permission. */ -/* */ -/* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS */ -/* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT */ -/* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS */ -/* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT */ -/* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, */ -/* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED */ -/* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, */ -/* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY */ -/* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING */ -/* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS */ -/* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -/* */ -/*----------------------------------------------------------------------------*/ -#include "ClassFactory.hpp" - - -/* constants used for class registration */ -#define szSERVERKEY "InprocServer32" -#define szTHREADMODEL "Both" - -#ifdef _NO_SCRIPT_GUIDS - #error "no, this is not allowed" -#endif - -/*---------------------------------------------------------------------------*/ - -/* Helper function to create a component category and associated description */ -HRESULT CreateComponentCategory(CATID catid, OLECHAR* catDescription) -{ - - ICatRegister *pcr = NULL; - HRESULT hr = S_OK; - CATEGORYINFO catinfo; - int iLen; - - hr = CoCreateInstance(CLSID_StdComponentCategoriesMgr, - NULL, CLSCTX_INPROC_SERVER, IID_ICatRegister, - (void**)&pcr); - if (FAILED(hr)) - return hr; - - /* Make sure the HKCR\Component Categories\{..catid...} key is registered */ - catinfo.catid = catid; - catinfo.lcid = 0x0409 ; // english - - /* Make sure the provided description is not too long (max 127 chars) */ - iLen = wcslen(catDescription); - if (iLen>127) - iLen = 127; - - wcsncpy(catinfo.szDescription, catDescription, iLen); - - /* Make sure the description is null terminated */ - catinfo.szDescription[iLen] = '\0'; - - hr = pcr->RegisterCategories(1, &catinfo); - pcr->Release(); - - return hr; -} /* end of function CreateComponentCategory */ - - -/* Helper function to register a CLSID as belonging to a component category */ -HRESULT RegisterCLSIDInCategory(REFCLSID clsid, CATID catid) -{ - /* Register your component categories information. */ - ICatRegister *pcr = NULL ; - HRESULT hr = S_OK ; - - hr = CoCreateInstance(CLSID_StdComponentCategoriesMgr, - NULL, CLSCTX_INPROC_SERVER, IID_ICatRegister, - (void**)&pcr); - if (SUCCEEDED(hr)) - { - /* Register this category as being "implemented" by the class. */ - CATID rgcatid[1]; - - rgcatid[0] = catid; - hr = pcr->RegisterClassImplCategories(clsid, 1, rgcatid); - } /* endif */ - - if (pcr != NULL) - pcr->Release(); - - return hr; -} /* end of function RegisterCLSIDInCategory */ - - -/* Helper function to unregister a CLSID as belonging to a component category */ -HRESULT UnRegisterCLSIDInCategory(REFCLSID clsid, CATID catid) -{ - ICatRegister *pcr = NULL ; - HRESULT hr = S_OK ; - - hr = CoCreateInstance(CLSID_StdComponentCategoriesMgr, - NULL, CLSCTX_INPROC_SERVER, IID_ICatRegister, - (void**)&pcr); - if (SUCCEEDED(hr)) - { - /* Unregister this category as being "implemented" by the class. */ - CATID rgcatid[1]; - - rgcatid[0] = catid; - hr = pcr->UnRegisterClassImplCategories(clsid, 1, rgcatid); - } /* endif */ - - if (pcr != NULL) - pcr->Release(); - - return hr; -} /* end of function UnRegisterCLSIDInCategory */ - - -/* StringFromGuidA returns an ANSI string from a CLSID or GUID */ -int StringFromGuidA(REFIID riid, LPSTR pszBuf) -{ - return sprintf((char *)pszBuf, - "{%08lX-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}", - riid.Data1, riid.Data2, riid.Data3, riid.Data4[0], - riid.Data4[1], riid.Data4[2], riid.Data4[3], riid.Data4[4], - riid.Data4[5], riid.Data4[6], riid.Data4[7]); -} - -/*---------------------------------------------------------------------------*/ - -STDMETHODIMP ooRexxClassFactory::QueryInterface(REFIID riid, void** ppvObj) -{ - - char cIID[100],*IIDName,TrulyUnknown[]="??????"; - - StringFromGUID2(riid,(unsigned short *)cIID,sizeof(cIID)/2); - if (ppvObj == 0) - { - return E_POINTER; - } - *ppvObj = NULL; - - if (riid == IID_IUnknown) - { - *ppvObj = (IUnknown *) this; - } - else if (riid == IID_IClassFactory) - { - *ppvObj = (IClassFactory *) this; - } - - if (*ppvObj == NULL) - { - return ResultFromScode(E_NOINTERFACE); - } - else - { - ((IUnknown*) *ppvObj)->AddRef(); - return NOERROR; - } -} - - -STDMETHODIMP_(ULONG) ooRexxClassFactory::AddRef(void) -{ - return ++ulRefCounter; -} - - -STDMETHODIMP_(ULONG) ooRexxClassFactory::Release(void) -{ - ULONG ulTempRefCounter; - - ulTempRefCounter = --ulRefCounter; - - if (ulRefCounter == 0) - delete this; - - return ulTempRefCounter; -} - - -STDMETHODIMP ooRexxClassFactory::LockServer(bool fLock) -{ - if (fLock) - InterlockedIncrement((long *)&ulDllLocks); //++ulDllLocks; - else - InterlockedDecrement((long *)&ulDllLocks); //--ulDllLocks; - - return NOERROR; -} - - -STDMETHODIMP ooRexxClassFactory::RegisterServer() -{ - HKEY hk; - HKEY hkSub; - HKEY hkSub2; - HKEY hkthread; - HRESULT hr = NOERROR; - const int REGPATHMAX = 512; - char szDllPath[REGPATHMAX]; - ULONG ulDllPathLen; - - /* Make a clean start */ - UnregisterServer(); - - /* Create scripting categories if they do not exist yet */ - CreateComponentCategory(CATID_ActiveScript, - OLESTR("Active Scripting Engine")); - CreateComponentCategory(CATID_ActiveScriptParse, - OLESTR("Active Scripting Engine with Parsing")); - - /* Register ourselves in the scripting categories */ - RegisterCLSIDInCategory(guidCLASSID, CATID_ActiveScript); - RegisterCLSIDInCategory(guidCLASSID, CATID_ActiveScriptParse); - - /* Register server info */ - if (RegCreateKey(HKEY_CLASSES_ROOT, szLANGNAME, &hk) == ERROR_SUCCESS) - { - if (RegCreateKey(hk, "OLEScript", &hkSub) != ERROR_SUCCESS) - { - RegCloseKey(hk); - return ResultFromScode(E_FAIL); - } - - RegCloseKey(hkSub); - - RegSetValue(HKEY_CLASSES_ROOT, szLANGNAME, REG_SZ, szDESCRIPTION, - strlen(szDESCRIPTION)); - RegSetValue(hk, "CLSID", REG_SZ, szCLASSID, strlen(szCLASSID)); - RegCloseKey(hk); - } - else - return ResultFromScode(E_FAIL); - - /* Register "alternate name" info */ - if (szALTERNATELANGNAME) - { - if (RegCreateKey(HKEY_CLASSES_ROOT, szALTERNATELANGNAME, &hk) == ERROR_SUCCESS) - { - if (RegCreateKey(hk, "OLEScript", &hkSub) != ERROR_SUCCESS) - { - RegCloseKey(hk); - return ResultFromScode(E_FAIL); - } - - RegCloseKey(hkSub); - RegSetValue(HKEY_CLASSES_ROOT, szALTERNATELANGNAME, REG_SZ, - szDESCRIPTION, strlen(szDESCRIPTION)); - RegSetValue (hk, "CLSID", REG_SZ, szCLASSID, strlen(szCLASSID)); - RegCloseKey (hk); - } - else - return ResultFromScode(E_FAIL); - } - - /* Register createable class */ - if (RegCreateKey(HKEY_CLASSES_ROOT, "CLSID", &hk) == ERROR_SUCCESS) - { - if (RegCreateKey(hk, szCLASSID, &hkSub) != ERROR_SUCCESS) - { - RegCloseKey(hk); - return ResultFromScode (E_FAIL); - } - - if (RegCreateKey(hkSub, "OLEScript", &hkSub2) != ERROR_SUCCESS) - { - RegCloseKey(hk); - RegCloseKey(hkSub); - return ResultFromScode(E_FAIL); - } - - ulDllPathLen = (ULONG) GetModuleFileName(GetModuleHandle(szDLLNAME), - szDllPath, REGPATHMAX); - - if (!ulDllPathLen) - { - RegCloseKey(hkSub2); - RegCloseKey(hkSub); - RegCloseKey(hk); - return ResultFromScode(E_FAIL); - } - - RegSetValue(hk, szCLASSID, REG_SZ, szDESCRIPTION, strlen(szDESCRIPTION)); - RegSetValue(hkSub, "ProgID", REG_SZ, szLANGNAME, strlen(szLANGNAME)); - RegSetValue(hkSub, szSERVERKEY, REG_SZ, szDllPath, ulDllPathLen); - - /* To set the threading model reg entry use the RegOpenKeyEx & SetValueEx */ - if (RegOpenKeyEx(hkSub, szSERVERKEY, 0, KEY_SET_VALUE, &hkthread) == ERROR_SUCCESS) - { - RegSetValueEx(hkthread, "ThreadingModel", 0, REG_SZ, - (BYTE *) szTHREADMODEL, strlen(szTHREADMODEL)); - RegCloseKey(hkthread); - } - else - { - RegCloseKey(hkthread); - RegCloseKey(hkSub2); - RegCloseKey(hkSub); - RegCloseKey (hk); - return ResultFromScode(E_FAIL); - } - - RegCloseKey(hkSub2); - RegCloseKey(hkSub); - RegCloseKey(hk); - } - else - return ResultFromScode(E_FAIL); - - /* Register the extension */ - if (szEXTENSION) - { - if (RegSetValue(HKEY_CLASSES_ROOT, szEXTENSION, - REG_SZ, szLANGFILE, strlen(szLANGFILE)) != ERROR_SUCCESS) - return ResultFromScode(E_FAIL); - } - - /* Register the language file */ - if (szLANGFILE) - { - if (RegCreateKey(HKEY_CLASSES_ROOT, szLANGFILE, &hk) == ERROR_SUCCESS) - { - if (RegCreateKey(hk, "ScriptEngine", &hkSub) != ERROR_SUCCESS) - { - RegCloseKey(hk); - return ResultFromScode(E_FAIL); - } - - RegCloseKey(hkSub); - - if (RegCreateKey(hk, "DefaultIcon", &hkSub) != ERROR_SUCCESS) - { - RegCloseKey(hk); - return ResultFromScode(E_FAIL); - } - - RegSetValueEx(hkSub, NULL, 0, REG_EXPAND_SZ, (const unsigned char *) szDEFAULTICON, strlen(szDEFAULTICON)); - RegCloseKey(hkSub); - - RegSetValue(HKEY_CLASSES_ROOT, szLANGFILE, REG_SZ, szFILEDESCRIPTION, - strlen(szFILEDESCRIPTION)); - RegSetValue(hk, "ScriptEngine", REG_SZ, szLANGNAME, strlen(szLANGNAME)); - createSubKeys(hk); - RegCloseKey(hk); - } - else - return ResultFromScode(E_FAIL); - } - - return S_OK; -} - - -STDMETHODIMP ooRexxClassFactory::UnregisterServer() -{ - HKEY hk; - HKEY hkSub; - HKEY hkSub2; - HRESULT hr = S_OK; - - /* UnRegister ourselves from the category */ - UnRegisterCLSIDInCategory(guidCLASSID, CATID_ActiveScript); - UnRegisterCLSIDInCategory(guidCLASSID, CATID_ActiveScriptParse); - - /* delete server info */ - if (RegOpenKey(HKEY_CLASSES_ROOT, szLANGNAME, &hk) != ERROR_SUCCESS) - { - hr = ResultFromScode(E_FAIL); - } - else - { - if (RegDeleteKey(hk, "CLSID") != ERROR_SUCCESS) - { - hr = ResultFromScode(E_FAIL); - } /* endif */ - - if (RegDeleteKey(hk, "OLEScript") != ERROR_SUCCESS) - { - hr = ResultFromScode (E_FAIL); - } /* endif */ - - RegCloseKey(hk); - - if (RegDeleteKey(HKEY_CLASSES_ROOT, szLANGNAME) != ERROR_SUCCESS) - { - hr = ResultFromScode(E_FAIL); - } /* endif */ - } - - /* delete alternate name entry */ - if (szALTERNATELANGNAME) - { - if (RegOpenKey(HKEY_CLASSES_ROOT, szALTERNATELANGNAME, &hk) != ERROR_SUCCESS) - { - hr = ResultFromScode(E_FAIL); - } - else - { - if (RegDeleteKey(hk, "CLSID") != ERROR_SUCCESS) - { - hr = ResultFromScode(E_FAIL); - } /* endif */ - - if (RegDeleteKey(hk, "OLEScript") != ERROR_SUCCESS) - { - hr = ResultFromScode(E_FAIL); - } /* endif */ - - RegCloseKey(hk); - - if (RegDeleteKey(HKEY_CLASSES_ROOT, szALTERNATELANGNAME) != ERROR_SUCCESS) - { - hr = ResultFromScode(E_FAIL); - } /* endif */ - } - } - - /* delete entries for CLSID */ - if (RegCreateKey(HKEY_CLASSES_ROOT, "CLSID", &hk) != ERROR_SUCCESS) - { - hr = ResultFromScode(E_FAIL); - } - else - { - if (RegOpenKey(hk, szCLASSID, &hkSub) != ERROR_SUCCESS) - { - hr = ResultFromScode(E_FAIL); - } - else - { - if (RegDeleteKey(hkSub, "ProgID") != ERROR_SUCCESS) - { - hr = ResultFromScode(E_FAIL); - } /* endif */ - - if (RegDeleteKey(hkSub, "OLEScript") != ERROR_SUCCESS) - { - hr = ResultFromScode(E_FAIL); - } /* endif */ - - if (RegDeleteKey(hkSub, "Implemented Categories") != ERROR_SUCCESS) - { - hr = ResultFromScode(E_FAIL); - } /* endif */ - - if (RegDeleteKey(hkSub, szSERVERKEY) != ERROR_SUCCESS) - { - hr = ResultFromScode(E_FAIL); - } /* endif */ - - RegCloseKey(hkSub); - } - - if (RegDeleteKey(hk, szCLASSID) != ERROR_SUCCESS) - { - hr = ResultFromScode(E_FAIL); - } /* endif */ - - RegCloseKey(hk); - } - - if (szEXTENSION) - { - /* delete extension */ - if (RegDeleteKey(HKEY_CLASSES_ROOT, szEXTENSION) != ERROR_SUCCESS) - { - hr = ResultFromScode(E_FAIL); - } /* endif */ - } - - if (szLANGFILE) - { - if (RegOpenKey(HKEY_CLASSES_ROOT, szLANGFILE, &hk) != ERROR_SUCCESS) - { - hr = ResultFromScode(E_FAIL); - } - else - { - deleteSubKeys(hk); - if (RegDeleteKey(hk, "ScriptEngine") != ERROR_SUCCESS) - { - hr = ResultFromScode(E_FAIL); - } - if (RegDeleteKey(hk, "DefaultIcon") != ERROR_SUCCESS) - { - hr = ResultFromScode(E_FAIL); - } - RegCloseKey(hk); - } - if (RegDeleteKey(HKEY_CLASSES_ROOT, szLANGFILE) != ERROR_SUCCESS) - { - hr = ResultFromScode(E_FAIL); - } - } - - return hr; -} - -HRESULT ooRexxClassFactory::deleteSubKeys(HKEY parent) -{ - HKEY hk1, hk2; - HRESULT hResult = E_FAIL; - - if (RegOpenKey(parent, "Shell", &hk1) == ERROR_SUCCESS) - { - // delete EDIT entry - if (RegOpenKey(hk1, "Edit", &hk2) == ERROR_SUCCESS) - { - RegDeleteKey(hk2, "Command"); - RegCloseKey(hk2); - RegDeleteKey(hk1, "Edit"); - } - // delete OPEN entry - if (RegOpenKey(hk1, "Open", &hk2) == ERROR_SUCCESS) - { - RegDeleteKey(hk2, "Command"); - RegCloseKey(hk2); - RegDeleteKey(hk1, "Open"); - } - // delete OPEN2 entry - if (RegOpenKey(hk1, "Open2", &hk2) == ERROR_SUCCESS) - { - RegDeleteKey(hk2, "Command"); - RegCloseKey(hk2); - RegDeleteKey(hk1, "Open2"); - } - // delete PRINT entry - if (RegOpenKey(hk1, "Print", &hk2) == ERROR_SUCCESS) - { - RegDeleteKey(hk2, "Command"); - RegCloseKey(hk2); - RegDeleteKey(hk1, "Print"); - } - // delete SHELL entry - RegCloseKey(hk1); - if (RegDeleteKey(parent, "Shell") == ERROR_SUCCESS) - { - hResult = S_OK; - } - } - return hResult; -} - -HRESULT ooRexxClassFactory::createSubKeys(HKEY parent) -{ - HKEY hk1, hk2, hk3; - HRESULT hResult = E_FAIL; - - if (RegCreateKey(parent, "Shell", &hk1) == ERROR_SUCCESS) - { - hResult = S_OK; - // create EDIT entry - if (RegCreateKey(hk1, "Edit", &hk2) == ERROR_SUCCESS) - { - if (RegCreateKey(hk2, "Command", &hk3) == ERROR_SUCCESS) - { - RegSetValueEx(hk3, NULL, 0, REG_EXPAND_SZ, (const unsigned char *) szSHELLEDIT, strlen(szSHELLEDIT)); - RegCloseKey(hk3); - } - RegCloseKey(hk2); - } - // create OPEN entry - if (RegCreateKey(hk1, "Open", &hk2) == ERROR_SUCCESS) - { - if (RegCreateKey(hk2, "Command", &hk3) == ERROR_SUCCESS) - { - RegSetValueEx(hk3, NULL, 0, REG_EXPAND_SZ, (const unsigned char *) szSHELLOPEN, strlen(szSHELLOPEN)); - RegCloseKey(hk3); - } - } - // create OPEN2 entry - if (RegCreateKey(hk1, "Open2", &hk2) == ERROR_SUCCESS) - { - if (RegCreateKey(hk2, "Command", &hk3) == ERROR_SUCCESS) - { - RegSetValueEx(hk3, NULL, 0, REG_EXPAND_SZ, (const unsigned char *) szSHELLOPEN2, strlen(szSHELLOPEN2)); - RegCloseKey(hk3); - } - } - // create PRINT entry - if (RegCreateKey(hk1, "Print", &hk2) == ERROR_SUCCESS) - { - if (RegCreateKey(hk2, "Command", &hk3) == ERROR_SUCCESS) - { - RegSetValueEx(hk3, NULL, 0, REG_EXPAND_SZ, (const unsigned char *) szSHELLPRINT, strlen(szSHELLPRINT)); - RegCloseKey(hk3); - } - } - RegCloseKey(hk1); - } - return hResult; -} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |