Chinaunix首页 | 论坛 | 博客
  • 博客访问: 160966
  • 博文数量: 4
  • 博客积分: 3023
  • 博客等级: 中校
  • 技术积分: 737
  • 用 户 组: 普通用户
  • 注册时间: 2006-03-09 08:40
文章分类

全部博文(4)

文章存档

2010年(1)

2008年(3)

我的朋友

分类:

2008-05-15 21:17:29

// CardActivex.cpp : Implementation of CCardActivexApp and DLL registration.


#include "stdafx.h"
#include "CardActivex.h"
#include"wdlib/wdlib.h"
#include "comcat.h"
#include "strsafe.h"
#include "objsafe.h"


#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif


CCardActivexApp NEAR theApp;

const GUID CDECL BASED_CODE _tlid =
        { 0xc74171f4, 0xd8ab, 0x45e1, { 0x84, 0xf4, 0xb7, 0x28, 0x47, 0xfb, 0x88, 0xd1 } };
const WORD _wVerMajor = 1;
const WORD _wVerMinor = 0;

const CATID CLSID_SafeItem =
{0x5fff4a7c, 0x4aa2, 0x4106,{ 0x8f, 0x55, 0xa, 0xc9, 0x81, 0x40, 0xa4, 0xcb}};

////////////////////////////////////////////////////////////////////////////

// CCardActivexApp::InitInstance - DLL initialization


BOOL CCardActivexApp::InitInstance()
{
    BOOL bInit = COleControlModule::InitInstance();
    
    if (bInit)
    {
        // TODO: Add your own module initialization code here.

    }
    //初始化WDLib库

    wdlib_init();
    return bInit;
}


////////////////////////////////////////////////////////////////////////////

// CCardActivexApp::ExitInstance - DLL termination


int CCardActivexApp::ExitInstance()
{
    // TODO: Add your own module termination code here.

    wdlib_shutdown();
    return COleControlModule::ExitInstance();
}

VOID GetErrorMessage(DWORD dwMsgId, LPTSTR pstrError, UINT nMaxError)
{
    LPTSTR lpBuffer;
    BOOL bRet=FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM,
        NULL, dwMsgId, MAKELANGID(LANG_NEUTRAL, SUBLANG_SYS_DEFAULT),
        (LPTSTR) &lpBuffer, 0, NULL);
    if (bRet == FALSE)
    {
        _snprintf(pstrError, nMaxError, _T("FormatMessage failed(0x%0X)! ErrorCode=0x%0X"), bRet, dwMsgId);
    }
    else
    {
        lstrcpyn(pstrError, lpBuffer, nMaxError);
        LocalFree(lpBuffer);
    }
}


CString GetErrorMessage(DWORD dwMsgId)
{
    TCHAR buffer[2048] = {0};
    GetErrorMessage(dwMsgId, buffer, sizeof(buffer));
    return (CString)buffer;
}


// HRESULT CreateComponentCategory - Used to register ActiveX control as safe

HRESULT CreateComponentCategory(CATID catid, WCHAR *catDescription)
{
    ICatRegister *pcr = NULL ;
    HRESULT hr = S_OK ;
    
    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.

    CATEGORYINFO catinfo;
    catinfo.catid = catid;
    catinfo.lcid = 0x0409 ; // english

    size_t len;
    // Make sure the provided description is not too long.

    // Only copy the first 127 characters if it is.

    // The second parameter of StringCchLength is the maximum

    // number of characters that may be read into catDescription.

    // There must be room for a NULL-terminator. The third parameter

    // contains the number of characters excluding the NULL-terminator.

    hr = StringCchLengthW(catDescription, STRSAFE_MAX_CCH, &len);
    if (SUCCEEDED(hr))
     {
        if (len>127)
        {
            len = 127;
        }
    }
    else
    {
        // TODO: Write an error handler;

    }
    // The second parameter of StringCchCopy is 128 because you need

    // room for a NULL-terminator.

    hr = StringCchCopyW(catinfo.szDescription, len + 1,
        catDescription);
    // Make sure the description is null terminated.

    catinfo.szDescription[len + 1] = '\0';
    
    hr = pcr->RegisterCategories(1, &catinfo);
    pcr->Release();
    
    return hr;
}

// HRESULT RegisterCLSIDInCategory - Register your component categories information

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);
    }
    
    if (pcr != NULL)
        pcr->Release();
    
    return hr;
}


/////////////////////////////////////////////////////////////////////////////

// DllRegisterServer - Adds entries to the system registry


STDAPI DllRegisterServer(void)
{
   //原来的代码    

   /*AFX_MANAGE_STATE(_afxModuleAddrThis);

    if (!AfxOleRegisterTypeLib(AfxGetInstanceHandle(), _tlid))
        return ResultFromScode(SELFREG_E_TYPELIB);

    if (!COleObjectFactoryEx::UpdateRegistryAll(TRUE))
        return ResultFromScode(SELFREG_E_CLASS);

    return NOERROR;*/

    
    //新替换的代码

     HRESULT hr; // HResult used by Safety Functions


    AFX_MANAGE_STATE(_afxModuleAddrThis);

    if (!AfxOleRegisterTypeLib(AfxGetInstanceHandle(), _tlid))
        return ResultFromScode(SELFREG_E_TYPELIB);

    if (!COleObjectFactoryEx::UpdateRegistryAll(TRUE))
        return ResultFromScode(SELFREG_E_CLASS);

    // Mark the control as safe for initializing.


    hr = CreateComponentCategory(CATID_SafeForInitializing, L"Controls safely initializable from persistent data!");
    if (FAILED(hr))
        return hr;

    hr = RegisterCLSIDInCategory(CLSID_SafeItem, CATID_SafeForInitializing);
    if (FAILED(hr))
        return hr;

    // Mark the control as safe for scripting.


    hr = CreateComponentCategory(CATID_SafeForScripting, L"Controls safely scriptable!");
    if (FAILED(hr))
        return hr;

    hr = RegisterCLSIDInCategory(CLSID_SafeItem, CATID_SafeForScripting);
    if (FAILED(hr))
        return hr;
    
    return NOERROR;
}


// HRESULT UnRegisterCLSIDInCategory - Remove entries from the registry

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);
    }
    
    if (pcr != NULL)
        pcr->Release();
    
    return hr;
}

/////////////////////////////////////////////////////////////////////////////

// DllUnregisterServer - Removes entries from the system registry

STDAPI DllUnregisterServer(void)
{
    //原来的代码

    /*AFX_MANAGE_STATE(_afxModuleAddrThis);

    if (!AfxOleUnregisterTypeLib(_tlid, _wVerMajor, _wVerMinor))
        return ResultFromScode(SELFREG_E_TYPELIB);

    if (!COleObjectFactoryEx::UpdateRegistryAll(FALSE))
        return ResultFromScode(SELFREG_E_CLASS);

    return NOERROR;*/


    //新添加的代码

    HRESULT hr; // HResult used by Safety Functions

    
    AFX_MANAGE_STATE(_afxModuleAddrThis);
    
    if (!AfxOleUnregisterTypeLib(_tlid, _wVerMajor, _wVerMinor))
        return ResultFromScode(SELFREG_E_TYPELIB);
    
    if (!COleObjectFactoryEx::UpdateRegistryAll(FALSE))
        return ResultFromScode(SELFREG_E_CLASS);
    
    // Remove entries from the registry.

    
    hr=UnRegisterCLSIDInCategory(CLSID_SafeItem, CATID_SafeForInitializing);
    if (FAILED(hr))
        return hr;
    
    hr=UnRegisterCLSIDInCategory(CLSID_SafeItem, CATID_SafeForScripting);
    if (FAILED(hr))
        return hr;
    
    return NOERROR;
}

阅读(1220) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~