///////////////////////////////////////////////////////////////////////////////
// ATLVCL.CPP - Provides the connective tissue between
// the ATL framework and VCL components.
//
// 1.1
//
// Copyright (c) 1997, 2000 Borland International
///////////////////////////////////////////////////////////////////////////////
#include <vcl.h>
#pragma hdrstop
#if !defined(__ATLVCL_H_)
#include <atl\atlvcl.h>
#endif
#include <comconst.hpp>
#include <axctrls.hpp>
#include <tchar.h>
namespace TInternalRegistrationHelpers {
// Contains functions needed internally to this module for the purpose
// of manipulating data into more pleasant forms and/or talking to the
// Registry. These functions are not available externally.
HRESULT CreateRegKey(const TCHAR* Key, const TCHAR* ValueName, const TCHAR* Value)
{
HKEY tempKey;
HRESULT Status = RegCreateKeyEx(HKEY_CLASSES_ROOT, Key, 0, _T(""),
REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, 0, &tempKey, 0);
if SUCCEEDED(Status)
{
Status = RegSetValueEx(tempKey, ValueName, 0, REG_SZ, (BYTE*)Value,
_tcslen(Value) + 1);
RegCloseKey(tempKey);
}
return Status;
}
HRESULT DeleteRegValue(TCHAR* Key, TCHAR* ValueName)
{
HKEY tempKey;
HRESULT Status = RegOpenKey(HKEY_CLASSES_ROOT, Key, &tempKey);
if SUCCEEDED(Status)
{
Status = RegDeleteValue(tempKey, ValueName);
RegCloseKey(tempKey);
}
return Status;
}
/* must be freed by caller. */
TCHAR* CreateKeyOfClass(TCHAR* CLSID)
{
TCHAR* retval = new TCHAR[_tcslen(CLSID)+_tcslen(_T("CLSID\\"))+1]; // length of "CLSID\"+ null terminator
_tcscpy(retval, _T("CLSID\\"));
_tcscat(retval, CLSID);
return retval;
}
/* must be freed by caller */
TCHAR* CreateAppIDOfClass(TCHAR* CLSID)
{
TCHAR* retval = new TCHAR[_tcslen(CLSID)+_tcslen(_T("AppID\\"))+1]; // length of "AppID\" + null terminator
_tcscpy(retval, _T("AppID\\"));
_tcscat(retval, CLSID);
return retval;
}
/* must be freed by caller */
TCHAR* CreateImplementedCategoriesOfClass(TCHAR* CLSID)
{
TCHAR* retval = new TCHAR[_tcslen(_T("CLSID\\"))+_tcslen(CLSID)+_tcslen(_T("\\Implemented Categories\\"))+1];
_tcscpy(retval, _T("CLSID\\"));
_tcscat(retval, CLSID);
_tcscat(retval, _T("\\Implemented Categories\\"));
return retval;
}
/* must be freed by caller */
TCHAR* CreateImplementedMIDASOfClass(TCHAR* CLSID)
{
TCHAR* retval = new TCHAR[_tcslen(CLSID)+_tcslen(_T("CLSID\\"))+
_tcslen(_T("\\Implemented Categories\\"))+
_tcslen(_T("{13E85B3C-9508-11D2-AB63-00C04FA35CFA}\\"))+1];
_tcscpy(retval, _T("CLSID\\"));
_tcscat(retval, CLSID);
_tcscat(retval, _T("\\Implemented Categories\\"));
_tcscat(retval, _T("{13E85B3C-9508-11D2-AB63-00C04FA35CFA}\\"));
return retval;
}
/* must be freed by caller. Since the caller won't have a clue how
to free it if it's not of type REG_SZ, free it manually and return
an error in that case. */
char* GetRegStringValue(const TCHAR* Key, const TCHAR* ValueName)
{
char* retval = new char[256];
DWORD Length = 256*sizeof(char);
HKEY tempKey;
DWORD TypeCode;
HRESULT hr = RegOpenKey(HKEY_CLASSES_ROOT, Key, &tempKey);
if SUCCEEDED(hr)
{
hr = RegQueryValueEx(tempKey, ValueName, 0, &TypeCode, (Byte*)retval, &Length);
}
if (TypeCode != REG_SZ)
{
delete[] retval;
retval = NULL;
}
return retval;
}
} // END namespace
//-----------------------------------------------------------------------------//
// Externs
// Pointer to save Initialization Procedure when using VCL
// (CBuilder3 backward compatibility)
//
void* SaveInitProc = 0;
// Helper used by IPersistStreamInit implementation to save component
//
void __fastcall SaveVCLComponentToStream(TComponent *vclInstance, LPSTREAM pStrm)
{
TPtr<TStream> pStream = new TOleStream(_di_IStream(pStrm));
TPtr<TWriter> pWriter = new TWriter(pStream, 4096);
pWriter->IgnoreChildren = true;
pWriter->WriteDescendent(vclInstance, 0);
}
// Helper used by IPersistStreamInit implementation to load component
//
void __fastcall LoadVCLComponentFromStream(TComponent *vclInstance, LPSTREAM pStrm)
{
TPtr<TStream> pStream = new TOleStream(_di_IStream(pStrm));
TPtr<TReader> pReader = new TReader(pStream, 4096);
pReader->ReadRootComponent(vclInstance);
}
// Helper used by framework to create a reflector object
//
TWinControl* CreateReflectorWindow(HWND parent, Controls::TControl* Control)
{
return new TReflectorWindow(parent, Control);
}
//----------------------------------------------------------------------------//
// Register (or unregisters) Remote data module
//
HRESULT TRemoteDataModuleRegistrar::UpdateRegistry(bool bRegister)
{
HRESULT hr = TTypedComServerRegistrar::UpdateRegistry(bRegister);
#if 0
#if!defined UNICODE && !defined(_UNICODE)
LPCSTR pClassID = m_ClassIDstr;
LPCSTR pProgID = m_ProgID;
#else
TAPtr<CHAR> pClassID = WideToAnsi(m_ClassIDstr);
TAPtr<CHAR> pProgID = WideToAnsi(m_ProgID);
#endif
#endif
if (bRegister ) hr = doRegister();
else hr=doUnRegister();
return hr;
}
/* Unregister the keys added if the data module supported pooling. */
HRESULT TRemoteDataModuleRegistrar::doUnRegisterPooled()
{
TCHAR* pClassID = m_ClassIDstr;
TCHAR* tempKey = TInternalRegistrationHelpers::CreateKeyOfClass(pClassID);
try
{
OLECHECK(TInternalRegistrationHelpers::DeleteRegValue(tempKey, _T("Pooled")));
OLECHECK(TInternalRegistrationHelpers::DeleteRegValue(tempKey, _T("MaxObjects")));
OLECHECK(TInternalRegistrationHelpers::DeleteRegValue(tempKey, _T("Timeout")));
OLECHECK(TInternalRegistrationHelpers::DeleteRegValue(tempKey, _T("Singleton")));
delete[] tempKey;
}
catch (...)
{
if (tempKey) delete[] tempKey;
return E_FAIL;
}
return S_OK;
}
/* Register the data module to support pooling */
HRESULT TRemoteDataModuleRegistrar::doRegisterPooled()
{
TCHAR* pClassID = m_ClassIDstr;
TCHAR* tempKey = TInternalRegistrationHelpers::CreateKeyOfClass(pClassID);
char* tempNumber = 0;
try
{
OLECHECK(TInternalRegistrationHelpers::CreateRegKey(tempKey, _T("Pooled"), _T("1")));
tempNumber = new char[33];
itoa(MaxObjects, tempNumber, 10);
#if !defined(UNICODE) && !defined(_UNICODE)
OLECHECK(TInternalRegistrationHelpers::CreateRegKey(tempKey, "MaxObjects", tempNumber));
#else
OLECHECK(TInternalRegistrationHelpers::CreateRegKey(tempKey, TStringConverter<char>::AnsiToWide("MaxObjects"),
TStringConverter<char>::AnsiToWide(tempNumber)));
#endif
memset(tempNumber, '\0', 33);
itoa(Timeout, tempNumber, 10);
#if !defined(UNICODE) && !defined(_UNICODE)
OLECHECK(TInternalRegistrationHelpers::CreateRegKey(tempKey, "Timeout", tempNumber));
#else
OLECHECK(TInternalRegistrationHelpers::CreateRegKey(tempKey, TStringConverter<char>::AnsiToWide("Timeout"),
TStringConverter<char>::AnsiToWide(tempNumber)));
#endif
if (Singleton) OLECHECK(TInternalRegistrationHelpers::CreateRegKey(tempKey, _T("Singleton"), _T("1")));
else OLECHECK(TInternalRegistrationHelpers::CreateRegKey(tempKey, _T("Singleton"), _T("0")));
delete[] tempKey;
delete[] tempNumber;
}
catch (...)
{
if (tempKey) delete[] tempKey;
if (tempNumber) delete[] tempNumber;
return E_FAIL;
}
return S_OK;
}
HRESULT TRemoteDataModuleRegistrar::doUnRegister()
{
TCHAR* pClassID = m_ClassIDstr;
ICatRegister* CatReg = 0;
TCHAR* RegKeyTemp = 0;
CLSID MIDASTemp;
GUID RemovalTemp;
// Get a component category manager. */
HRESULT hr = CoCreateInstance(CLSID_StdComponentCategoryMgr, 0,
- 1
- 2
前往页