#pragma once
// WAC.com Class Liblary for Visual C++
// Copyright (C) WAC.com Inc. All rights reserved.
//
// This file is a part of the WAC.com Class Liblary.
// The use and distribution terms for this software are covered by the
// Common Public License 1.0 (http://opensource.org/licenses/cpl.php)
// which can be found in the file CPL.TXT at the root of this distribution.
// By using this software in any fashion, you are agreeing to be bound by
// the terms of this license. You must not remove this notice, or
// any other, from this software.
// '07.06.08 : 新規作成 (sha)
// '07.06.11 : コードの整備 (sha)
// '07.06.13 : コードの整備 (sha)
// '07.06.21: コードの整備 (sha)
#include <exdispid.h>
#include <DispEx.h>
// Native Compiler COM Support
#include <comdef.h> // _com_error
#include <comutil.h> // _bstr_t, _variant_t
#include <comip.h> // _com_ptr_t
#pragma comment(lib, "comsupp.lib")
#pragma comment(lib, "comsuppw.lib")
#if 0
#pragma warning(push)
#pragma warning(disable: 4192)
#pragma warning(disable: 4278)
#pragma warning(disable: 4279)
#import <shdocvw.dll>
#import <mshtml.tlb>
#pragma warning(pop)
#else
#include <ExDisp.h> // IWebBrowser2
#include <MsHTML.h> // IHTMLDocument2
#endif
#if _MSC_VER < 1400
#if !defined(_countof)
#define _countof(_Array) (sizeof(_Array) / sizeof(_Array[0]))
#endif
#endif
/////////////////////////////////////////////////////////////////////////////
// CIEScriptHelper
class CIEScriptHelper
{
protected:
typedef CIEScriptHelper thisClass;
protected:
CIEScriptHelper(); // 実装しない
virtual ~CIEScriptHelper();
public:
#if 0
// Scriptオブジェクトを取得
static IDispatchExPtr GetScript(SHDocVw::IWebBrowser2Ptr spWebBrowser2)
{
if (spWebBrowser2 == NULL)
{
ATLASSERT(0);
return NULL;
}
IDispatchExPtr spResult;
try
{
MSHTML::IHTMLDocument2Ptr spHTMLDocuemt2 = spWebBrowser2->Document;
if (spHTMLDocuemt2 == NULL)
{
return NULL;
}
spResult = spHTMLDocuemt2->Script;
if (spResult == NULL)
{
return NULL;
}
}
catch (...)
{
ATLASSERT(0);
return NULL;
}
return spResult;
}
#else
// Scriptオブジェクトを取得
static IDispatchExPtr GetScript(IWebBrowser2Ptr spWebBrowser2)
{
if (spWebBrowser2 == NULL)
{
ATLASSERT(0);
return NULL;
}
HRESULT hr = S_OK;
IDispatchPtr spDispHTMLDocuemt2;
hr = spWebBrowser2->get_Document(&spDispHTMLDocuemt2);
if (FAILED(hr))
{
return NULL;
}
IHTMLDocument2Ptr spHTMLDocuemt2 = spDispHTMLDocuemt2;
if (spHTMLDocuemt2 == NULL)
{
return NULL;
}
IDispatchPtr spDispScript;
hr = spHTMLDocuemt2->get_Script(&spDispScript);
if (FAILED(hr))
{
return NULL;
}
IDispatchExPtr spScript = spDispScript;
if (spScript == NULL)
{
return NULL;
}
return spScript;
}
#endif
public:
// プロパティを登録
// Python...setattr()
static HRESULT RegisterProperty(IDispatchExPtr spDispatchEx, LPCTSTR szName, _variant_t varValue)
{
if (spDispatchEx == NULL)
{
ATLASSERT(0);
return E_FAIL;
}
HRESULT hr = S_OK;
DISPID dispID = 0;
hr = spDispatchEx->GetDispID(_bstr_t(szName), fdexNameEnsure | fdexNameCaseSensitive, &dispID);
if (FAILED(hr))
{
return hr;
}
DISPID dispidNamedArgs = DISPID_PROPERTYPUT;
DISPPARAMS dispParams = { &varValue, &dispidNamedArgs, 1, 1 };
EXCEPINFO excepInfo = { 0 };
hr = spDispatchEx->InvokeEx(dispID, LOCALE_USER_DEFAULT, DISPATCH_PROPERTYPUT, &dispParams, NULL, &excepInfo, NULL);
if (FAILED(hr))
{
return hr;
}
return S_OK;
}
// プロパティを登録解除
static HRESULT UnregisterProperty(IDispatchExPtr spDispatchEx, LPCTSTR szName)
{
USES_CONVERSION;
if (spDispatchEx == NULL)
{
ATLASSERT(0);
return E_FAIL;
}
HRESULT hr = S_OK;
#if 1
LPOLESTR szNames[] = { T2W(szName) };
DISPID dispID = 0;
hr = spDispatchEx->GetIDsOfNames(IID_NULL, szNames, _countof(szNames), LOCALE_USER_DEFAULT, &dispID);
if (FAILED(hr))
{
return hr;
}
hr = spDispatchEx->DeleteMemberByDispID(dispID);
if (FAILED(hr))
{
// NOTE: E_NOTIMPLになる。。
return hr;
}
#else
hr = spDispatchEx->DeleteMemberByName(_bstr_t(szName), fdexNameCaseSensitive);
if (FAILED(hr))
{
// NOTE: E_NOTIMPLになる。。
return hr;
}
#endif
return S_OK;
}
// プロパティを取得
// Python...getattr()
static HRESULT GetProperty(IDispatchExPtr spDispatchEx, LPCTSTR szName, _variant_t& varValue)
{
USES_CONVERSION;
if (spDispatchEx == NULL)
{
ATLASSERT(0);
return E_FAIL;
}
HRESULT hr = S_OK;
LPOLESTR szNames[] = { T2W(szName) };
DISPID dispID = 0;
hr = spDispatchEx->GetIDsOfNames(IID_NULL, szNames, _countof(szNames), LOCALE_USER_DEFAULT, &dispID);
if (FAILED(hr))
{
return hr;
}
DISPPARAMS dispParams = { NULL, NULL, 0, 0 };
_variant_t var;
hr = spDispatchEx->Invoke(dispID, IID_NULL, LOCALE_USER_DEFAULT, DISPATCH_PROPERTYGET, &dispParams, &var, NULL, NULL);
if (FAILED(hr))
{
return hr;
}
return S_OK;
}
// プロパティは存在するか?
// Python...hasattr()
static HRESULT ExistProperty(IDispatchExPtr spDispatchEx, LPCTSTR szName)
{
USES_CONVERSION;
if (spDispatchEx == NULL)
{
ATLASSERT(0);
return E_FAIL;
}
HRESULT hr = S_OK;
LPOLESTR szNames[] = { T2W(szName) };
DISPID dispID = 0;
hr = spDispatchEx->GetIDsOfNames(IID_NULL, szNames, _countof(szNames), LOCALE_USER_DEFAULT, &dispID);
if (FAILED(hr))
{
return (hr == DISP_E_UNKNOWNNAME) ? S_FALSE : hr;
}
return S_OK;
}
};