WPPROV.CPP

/*------------------------------------------------*\ 
*
*Copyright (c) 1997 Microsoft Corporation
*
*Module Name:
*
*wpprov.cpp
*
*Abstract:
*
*Implementation of DLL Exports
*
\*------------------------------------------------*/

#include "stdafx.h"
#include "resource.h"
#include "initguid.h"
#include "wpprov.h"
#include "wppobj.h"
#include "regkeyex.h"
#include "util.h"
#include "priority.h"
#include "wppdefs.h"

#define IID_DEFINED
#include "wpspi_i.c"
#include "wpprov_i.c"


CComModule _Module;
HINSTANCE g_hInstance;

BEGIN_OBJECT_MAP(ObjectMap)
OBJECT_ENTRY(CLSID_CWPProvider, CWPProvider)
END_OBJECT_MAP()

/////////////////////////////////////////////////////////////////////////////
// DLL Entry Point

extern "C"
BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID /*lpReserved*/)
{
if (dwReason == DLL_PROCESS_ATTACH)
{
g_hInstance = hInstance;
_Module.Init(ObjectMap, hInstance);
DisableThreadLibraryCalls(hInstance);
}
else if (dwReason == DLL_PROCESS_DETACH)
_Module.Term();
return TRUE; // ok
}

/////////////////////////////////////////////////////////////////////////////
// Used to determine whether the DLL can be unloaded by OLE

STDAPI DllCanUnloadNow(void)
{
return (_Module.GetLockCount()==0) ? S_OK : S_FALSE;
}

/////////////////////////////////////////////////////////////////////////////
// Returns a class factory to create an object of the requested type

STDAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID* ppv)
{
return _Module.GetClassObject(rclsid, riid, ppv);
}

/////////////////////////////////////////////////////////////////////////////
// DllRegisterServer - Adds entries to the system registry

STDAPI DllRegisterServer(void)
{
CRegKeyExhKey;
WCHARwsProvCLSID[MAX_PATH];
CHARsProvCLSID[MAX_PATH];
CHARsProvKey[MAX_PATH];
CHARsPathKey[MAX_PATH];
CHARsProvDesc[MAX_PATH];
CHARsProvPath[MAX_PATH];
DWORDdwBufLen;
DWORDdwResult;
HRESULThResult;

//Registers object, typelib and all interfaces in typelib

hResult = _Module.RegisterServer(TRUE);
if (S_OK != hResult)
return(hResult);

//Get string version of Provider CLSID

if (0 == StringFromGUID2(CLSID_CWPProvider, wsProvCLSID, MAX_PATH))
return(HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER));

g_wcstombs(sProvCLSID, wsProvCLSID, MAX_PATH);

//construct regkey names

lstrcpyA(sPathKey, REGKEY_CLASSES_A);
lstrcatA(sPathKey, "\\");
lstrcatA(sPathKey, sProvCLSID);
lstrcatA(sPathKey, "\\");
lstrcatA(sPathKey, REGKEY_INPROCSERVER_A);

lstrcpyA(sProvKey, REGSUBKEY_WPP_A);
lstrcatA(sProvKey, "\\");
lstrcatA(sProvKey, sProvCLSID);

//Get DLL path

dwResult = hKey.Create(HKEY_CLASSES_ROOT, sPathKey);
hResult = HRESULT_FROM_WIN32(dwResult);
if (FAILED(hResult))
return(hResult);

dwBufLen = MAX_PATH;
dwResult = hKey.QueryValue(sProvPath, &dwBufLen, "");
hResult = HRESULT_FROM_WIN32(dwResult);
if (FAILED(hResult))
return(hResult);

//Create WebPost Provider key

dwResult = hKey.Create(HKEY_WPP, sProvKey);
hResult = HRESULT_FROM_WIN32(dwResult);
if (FAILED(hResult))
return(hResult);

//Load Provider description string

if (0 == LoadString(g_hInstance, IDS_WPPROVIDER_DESC, sProvDesc, MAX_PATH))
{
dwResult = GetLastError();
return(HRESULT_FROM_WIN32(dwResult));
}

//Set Provider key values

dwResult = hKey.SetValue(sProvDesc, REGVAL_WPP_NAME_A);
hResult = HRESULT_FROM_WIN32(dwResult);
if (FAILED(hResult))
return(hResult);

dwResult = hKey.SetValue(sProvPath, REGVAL_WPP_DLLPATH_A);
hResult = HRESULT_FROM_WIN32(dwResult);
if (FAILED(hResult))
return(hResult);

dwResult = hKey.SetValue(WPP_PRIORITY, REGVAL_WPP_PRIORITY_A);
hResult = HRESULT_FROM_WIN32(dwResult);
if (FAILED(hResult))
return(hResult);

dwResult = hKey.SetValue((DWORD) 0, REGVAL_WPP_POSTINFO_REQUIRED_A);
hResult = HRESULT_FROM_WIN32(dwResult);
if (FAILED(hResult))
return(hResult);

return(NO_ERROR);
}

/////////////////////////////////////////////////////////////////////////////
// DllUnregisterServer - Removes entries from the system registry

STDAPI DllUnregisterServer(void)
{
CRegKeyExhKey;
WCHARwsProvCLSID[MAX_PATH];
CHARsProvCLSID[MAX_PATH];
HRESULThResult;
DWORDdwResult;

//Get string version of Provider CLSID

if (0 == StringFromGUID2(CLSID_CWPProvider, wsProvCLSID, MAX_PATH))
return(HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER));

g_wcstombs(sProvCLSID, wsProvCLSID, MAX_PATH);

//Remove WebPost registry entries

dwResult = hKey.Open(HKEY_WPP, REGSUBKEY_WPP_A);
hResult = HRESULT_FROM_WIN32(dwResult);
if (FAILED(hResult))
return(hResult);

dwResult = hKey.DeleteSubKey(sProvCLSID);
hResult = HRESULT_FROM_WIN32(dwResult);
if (FAILED(hResult))
return(hResult);

//Do ATL unregistration

_Module.UnregisterServer();

return(NO_ERROR);
}