一个调用当前IE页面元素的类库(BCB)

代码:
//---------------------------------------------------------------------------
// 作者:雨中漫步
// 写于:2004-12-26, 19:07:56
// 网址:http://www.wzszf.com/
//---------------------------------------------------------------------------
#ifndef IEInvokeH
#define IEInvokeH
#include <vcl.h>
#include <utilcls.h>
#include <atl/atlbase.h>
#include <oleacc.h>
#include <mshtml.h>
#include <comdef.h>
#pragma hdrstop
#define CLASS_SIZE   500

//---------------------------------------------------------------------------
class TIEInvoke :
public TObject
{
private:
CComPtr<IHTMLDocument2> FIEDoc;
HANDLE       FIEHandle;
void __fastcall SetIEDoc(CComPtr<IHTMLDocument2> value);
CComPtr < IHTMLDocument2 > __fastcall GetIEDoc(void);
String __fastcall GetSystemErrorMessage(DWORD dwError);
void __fastcall   SetIEHandle(HANDLE value);
HANDLE __fastcall GetIEHandle(void);
protected:
public:
__fastcall   TIEInvoke(void);
__property CComPtr<IHTMLDocument2>   IEDoc = { read = GetIEDoc, write = SetIEDoc };
__property HANDLE IEHandle = { read = GetIEHandle, write = SetIEHandle };
CComPtr < IHTMLDocument2 > __fastcall FindIE(void);
bool __fastcall CallJs(String jsName, TStringList *args, CComVariant *pVarResult);
bool __fastcall SetTextInput(String textInputName, String &textInputValue);
bool __fastcall SetTextAreaInput(String textAreaInputName, String &textAreaInputValue);
bool __fastcall SetDropDownSelect(String dropDownName, String dropDownValue);
bool __fastcall GetTextInput(String textInputName, String &textInputValue);
bool __fastcall GetDropDownSelect(String dropDownName, String &dropDownValue);
bool __fastcall GetTextAreaInput(String textAreaInputName, String &textAreaInputValue);
};

/* */
BOOL CALLBACK FindIEInst(HWND hwnd, LPARAM lParam)
{
char   wndowclass[CLASS_SIZE];

if(GetClassName(hwnd, wndowclass, CLASS_SIZE) == 0)
{
  return TRUE;
}

String   strTemp(wndowclass);
if(strTemp == String("Internet Explorer_Server"))
{
  CoInitialize(NULL);

HINSTANCE hInst = ::LoadLibrary(_T("OLEACC.DLL"));
  String   strTemp;
  CComPtr<IHTMLDocument2> spDoc;
  LRESULT lRes;

strTemp = "";

UINT   nMsg = ::RegisterWindowMessage(_T("WM_HTML_GETOBJECT"));
  ::SendMessageTimeout(hwnd, nMsg, 0L, 0L, SMTO_ABORTIFHUNG, 1000, (DWORD *) &lRes);

LPFNOBJECTFROMLRESULT pfObjectFromLresult = (LPFNOBJECTFROMLRESULT)::GetProcAddress(hInst, _T("ObjectFromLresult"));
  if(pfObjectFromLresult != NULL)
  {
    HRESULT hr;
    hr = (*pfObjectFromLresult) (lRes, IID_IHTMLDocument2, 0, (void **) &spDoc);
    if(SUCCEEDED(hr))
    {
    TIEInvoke *ie = (TIEInvoke *) lParam;
    if(ie != NULL)
    {
      ie->IEDoc = spDoc;
      ie->IEHandle = hwnd;
    }

::FreeLibrary(hInst);
    CoUninitialize();
    return FALSE;
    }
  }

::FreeLibrary(hInst);
  CoUninitialize();
}

return TRUE;
}

//---------------------------------------------------------------------------
#endif

代码:
//---------------------------------------------------------------------------
// 作者:雨中漫步
// 写于:2004-12-26, 19:07:56
// 网址:http://www.chinabcb.com
//---------------------------------------------------------------------------
#include "IEInvoke.h"

/* */

__fastcall TIEInvoke::TIEInvoke(void)
{
FIEDoc = NULL;
FIEHandle = NULL;
}

/* */
bool __fastcall TIEInvoke::SetTextInput(String textInputName, String &textInputValue)
{
CComPtr<IHTMLElementCollection> pAllColl;
HRESULT hr;
hr = FIEDoc->get_all((IHTMLElementCollection **) &pAllColl);
if(hr == S_OK)
{
  LONG   length = 0;
  hr = pAllColl->get_length(&length);
  if(hr == S_OK)
  {
    for(int i = 0; i < length; i++)
    {
    VARIANT varIndex;
    varIndex.vt = VT_UINT;
    varIndex.lVal = i;

VARIANT var2;
    VariantInit(&var2);

IDispatch *pDisp;
    hr = pAllColl->item(varIndex, var2, &pDisp);   //Get an element
    if(hr == S_OK)
    {
      IHTMLElement   *pElem;
      hr = pDisp->QueryInterface(IID_IHTMLElement, (void **) &pElem);
      if(hr == S_OK)
      {
        BSTR   bstr;

hr = pElem->get_tagName(&bstr);

String   strTag;
        strTag = bstr;

String   mStr = "";
        IHTMLInputTextElement *pUser;
        hr = pDisp->QueryInterface(IID_IHTMLInputTextElement, (void **) &pUser);
        if(hr == S_OK)
        {
        pUser->get_name(&bstr);
        mStr = bstr;
        if(mStr == textInputName)
        {
          pUser->put_value(WideString(textInputValue));

pUser->Release();
          pElem->Release();
          pDisp->Release();

return true;
        }

pUser->Release();
        }

pElem->Release();
      }

pDisp->Release();
    }
    }
  }
}

return false;
}

/* */
bool __fastcall TIEInvoke::SetTextAreaInput(String textAreaInputName, String &textAreaInputValue)
{
CComPtr<IHTMLElementCollection> pAllColl;
HRESULT hr;
hr = FIEDoc->get_all((IHTMLElementCollection **) &pAllColl);
if(hr == S_OK)
{
  LONG   length = 0;
  hr = pAllColl->get_length(&length);
  if(hr == S_OK)
  {
    for(int i = 0; i < length; i++)
    {
    VARIANT varIndex;
    varIndex.vt = VT_UINT;
    varIndex.lVal = i;

VARIANT var2;
    VariantInit(&var2);

IDispatch *pDisp;
    hr = pAllColl->item(varIndex, var2, &pDisp);   //Get an element
    if(hr == S_OK)
    {
      IHTMLElement   *pElem;
      hr = pDisp->QueryInterface(IID_IHTMLElement, (void **) &pElem);
      if(hr == S_OK)
      {
        BSTR   bstr;

hr = pElem->get_tagName(&bstr);

String   strTag;
        strTag = bstr;

String   mStr = "";
        IHTMLTextAreaElement   *pUser;
        hr = pDisp->QueryInterface(IID_IHTMLTextAreaElement, (void **) &pUser);
        if(hr == S_OK)
        {
        pUser->get_name(&bstr);
        mStr = bstr;
        if(mStr == textAreaInputName)
        {
          pUser->put_value(WideString(textAreaInputValue));

pUser->Release();
          pElem->Release();
          pDisp->Release();

return true;
        }

pUser->Release();
        }

pElem->Release();
      }

pDisp->Release();
    }
    }
  }
}

return false;
}

/* */
bool __fastcall TIEInvoke::CallJs(String jsName, TStringList *args, CComVariant *pVarResult)
{
CComPtr<IDispatch>   spScript;
HRESULT hr = FIEDoc->get_Script(&spScript);
ATLASSERT(SUCCEEDED(hr));
if(!SUCCEEDED(hr))
{
  return false;
}

BSTR   bstrMember = WideString(jsName);
DISPID   dispid = NULL;
hr = spScript->GetIDsOfNames(IID_NULL, &bstrMember, 1, LOCALE_SYSTEM_DEFAULT, &dispid);
if(FAILED(hr))
{
  return false;
}

const int   arraySize = args->Count;

DISPPARAMS   dispparams;
memset(&dispparams, 0, sizeof dispparams);
dispparams.cArgs = arraySize;
dispparams.rgvarg = new VARIANT[dispparams.cArgs];

for(int i = 0; i < arraySize; i++)
{
  CComBSTR   bstr = WideString(args->Strings[arraySize - 1 - i]).Copy(); // back reading
  bstr.CopyTo(&dispparams.rgvarg.bstrVal);
  dispparams.rgvarg.vt = VT_BSTR;
}

dispparams.cNamedArgs = 0;

EXCEPINFO excepInfo;
memset(&excepInfo, 0, sizeof excepInfo);

CComVariant vaResult;
UINT       nArgErr = (UINT) - 1; // initialize to invalid arg
hr = spScript->Invoke(dispid, IID_NULL, 0, DISPATCH_METHOD, &dispparams, &vaResult, &excepInfo, &nArgErr);

delete[] dispparams.rgvarg;
if(FAILED(hr))
{
#ifdef _DEBUG
  ShowMessage(GetSystemErrorMessage(hr));
#endif
  return false;
}

if(pVarResult)
{
  *pVarResult = vaResult;
}

return true;
}

/* */
CComPtr<IHTMLDocument2> __fastcall TIEInvoke::FindIE(void)
{
HANDLE   HWND = FindWindow("IEFrame", NULL);
if(HWND == NULL)
{
  return NULL;
}

EnumChildWindows(HWND, (WNDENUMPROC) FindIEInst, (LPARAM) this);
return FIEDoc;
}

/* */
bool __fastcall TIEInvoke::GetTextInput(String textInputName, String &textInputValue)
{
CComPtr<IHTMLElementCollection> pAllColl;
HRESULT hr;
hr = FIEDoc->get_all((IHTMLElementCollection **) &pAllColl);
if(hr == S_OK)
{
  LONG   length = 0;
  hr = pAllColl->get_length(&length);
  if(hr == S_OK)
  {
    for(int i = 0; i < length; i++)
    {
    VARIANT varIndex;
    varIndex.vt = VT_UINT;
    varIndex.lVal = i;

VARIANT var2;
    VariantInit(&var2);

IDispatch *pDisp;
    hr = pAllColl->item(varIndex, var2, &pDisp);   //Get an element
    if(hr == S_OK)
    {
      IHTMLElement   *pElem;
      hr = pDisp->QueryInterface(IID_IHTMLElement, (void **) &pElem);
      if(hr == S_OK)
      {
        BSTR   bstr;

hr = pElem->get_tagName(&bstr);

String   strTag;
        strTag = bstr;

String   mStr = "";
        IHTMLInputTextElement *pUser;
        hr = pDisp->QueryInterface(IID_IHTMLInputTextElement, (void **) &pUser);
        if(hr == S_OK)
        {
        pUser->get_name(&bstr);
        mStr = bstr;
        if(mStr == textInputName)
        {
          WideString   inputValue = "";
          pUser->get_value(&inputValue);
          textInputValue = inputValue;
          pUser->Release();
          pElem->Release();
          pDisp->Release();

return true;
        }

pUser->Release();
        }

pElem->Release();
      }

pDisp->Release();
    }
    }
  }
}

return false;
}

/* */
bool __fastcall TIEInvoke::SetDropDownSelect(String dropDownName, String dropDownValue)
{
return false;
}

/* */
bool __fastcall TIEInvoke::GetDropDownSelect(String dropDownName, String &dropDownValue)
{
return false;
}

/* */
bool __fastcall TIEInvoke::GetTextAreaInput(String textAreaInputName, String &textAreaInputValue)
{
CComPtr<IHTMLElementCollection> pAllColl;
HRESULT hr;
hr = FIEDoc->get_all((IHTMLElementCollection **) &pAllColl);
if(hr == S_OK)
{
  LONG   length = 0;
  hr = pAllColl->get_length(&length);
  if(hr == S_OK)
  {
    for(int i = 0; i < length; i++)
    {
    VARIANT varIndex;
    varIndex.vt = VT_UINT;
    varIndex.lVal = i;

VARIANT var2;
    VariantInit(&var2);

IDispatch *pDisp;
    hr = pAllColl->item(varIndex, var2, &pDisp);   //Get an element
    if(hr == S_OK)
    {
      IHTMLElement   *pElem;
      hr = pDisp->QueryInterface(IID_IHTMLElement, (void **) &pElem);
      if(hr == S_OK)
      {
        BSTR   bstr;

hr = pElem->get_tagName(&bstr);

String   strTag;
        strTag = bstr;

String   mStr = "";
        IHTMLTextAreaElement   *pUser;
        hr = pDisp->QueryInterface(IID_IHTMLTextAreaElement, (void **) &pUser);
        if(hr == S_OK)
        {
        pUser->get_name(&bstr);
        mStr = bstr;
        if(mStr == textAreaInputName)
        {
          WideString   inputValue = "";
          pUser->get_value(&inputValue);
          textAreaInputValue = inputValue;

pUser->Release();
          pElem->Release();
          pDisp->Release();

return true;
        }

pUser->Release();
        }

pElem->Release();
      }

pDisp->Release();
    }
    }
  }
}

return false;
}

/* */
void __fastcall TIEInvoke::SetIEDoc(CComPtr<IHTMLDocument2> value)
{
FIEDoc = value;
}

/* */
CComPtr<IHTMLDocument2> __fastcall TIEInvoke::GetIEDoc(void)
{
return FIEDoc;
}

/* */
String __fastcall TIEInvoke::GetSystemErrorMessage(DWORD dwError)
{
String   strError;
LPTSTR   lpBuffer;

if
(
  !FormatMessage
    (
    FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
    NULL,
    dwError,
    MAKELANGID(LANG_NEUTRAL, SUBLANG_SYS_DEFAULT),
    (LPTSTR) & lpBuffer,
    0,
    NULL
    )
)
{
  strError = "FormatMessage Netive Error";
}
else
{
  strError = lpBuffer;
  LocalFree(lpBuffer);
}

return strError;
}

/* */
void __fastcall TIEInvoke::SetIEHandle(HANDLE value)
{
FIEHandle = value;
}

/* */
HANDLE __fastcall TIEInvoke::GetIEHandle(void)
{
return FIEHandle;
}

使用例子:
代码:
TIEInvoke *ie = new TIEInvoke();
CComPtr<IHTMLDocument2> IEDoc = ie->FindIE();
String     subject = "邮件主题";

//ie->SetTextInput("TextBoxSubject", subject);
//ie->GetTextInput("TextBoxFromEmail", subject);
//ie->SetTextAreaInput("TextBoxDescription",subject);
//ie->GetTextAreaInput("TextBoxDescription",subject);
TStringList *args = new TStringList();
args->Add("2");
args->Add("3");

CComVariant *pVarResult = new CComVariant();
ie->CallJs("add", args, pVarResult);

int result = pVarResult->intVal;
delete pVarResult;
pVarResult = NULL;
delete args;
args = NULL;
delete ie;
ie = NULL;
ShowMessage(result);

(BCB) CComPtrIHTMLDocument2 FIEDoc;相关推荐

  1. BCB key事件中判断Shift、Alt、Ctrl状态

    BCB key事件中判断Shift.Alt.Ctrl状态: 类似此事件中 void __fastcall TForm1::keydown(TObject *Sender, WORD &Key, ...

  2. BCB 编写 DLL 终极手册

    BCB 编写 DLL 终极手册 一. 编写 DLL File/New/Dll 生成 Dll 的向导,然后可以添加导出函数和导出类 导出函数:extern "C" __declspe ...

  3. bcb中使用pas文件

    因为以前多用delphi开发,最近需要用c++builder 开发一些小软件,正好bcb可以使用delphi的源码,所以直接添加,因为delphi的pas源码中使用了部分的raize控件,所以bcb中 ...

  4. 深入BCB理解VCL的消息机制

    深入BCB理解VCL的消息机制 引子:本文所谈及的技术内容都来自于Internet的公开信息.由笔者在闲暇之际整理 后,贴出来以飴网友,姑且妄称原创.每次在国外网站上找到精彩文章的时候,心中都 会暗自 ...

  5. BCB 串口控件的使用 TComm

    昨天工作用到了串口通信,MMP的,昨天懵逼了一下午,今天终于整通了,身为菜鸟,大师们是不懂这些心痛的. 进入主题:使用BCB提供的控件TComm编程方便且简单,TComm位于System分类里面.   ...

  6. bcb 如何在DLL中捕捉系统级异常

    bcb 如何在DLL中捕捉系统级异常 http://topic.csdn.net/t/20031023/09/2385627.html -------------------------------- ...

  7. BCB中的RTTI机制

    BCB中的RTTI机制 RTTI在BCB中其实有两个意义.首先,针对一般标准的C++中所定义的,RTTI是所谓的Run-Time Type Identification的缩写.传统的程序语言中,所谓的 ...

  8. 关于如何在BCB中使用CodeGuard

    关于如何在BCB中使用CodeGuard   点击数:231  录入时间:2007-6-7                一. 为什么写这篇东西 自己在使用 BCB5 写一些程序时需要检查很多东西,例 ...

  9. bcb隐藏窗口_BCB隐藏Windows任务栏的方法

    BCB隐藏Windows任务栏的方法 来源:互联网  宽屏版  评论 2006-01-09 12:27:01 void __fastcall TForm1::FormCreate(TObject *S ...

最新文章

  1. HDFS设计思路,HDFS使用,查看集群状态,HDFS,HDFS上传文件,HDFS下载文件,yarn web管理界面信息查看,运行一个mapreduce程序,mapreduce的demo...
  2. POJChallengeRound2 Guideposts 【单位根反演】【快速幂】
  3. Java框架体系架构的知识,轻松拿下offer
  4. 案例 网络电视精灵 c# 1614265410
  5. Ndarry/Dataframe使用pytorch转为tensor格式
  6. Atitit 云计算体系树.docx Atitit 云计算之道 attilax著 艾龙 著 serverless bomb 1. 什么才是云计算的根本特征.. 2 2. 云计算体系 2 3. “云
  7. python whl文件安装_python whl文件怎么安装
  8. 轻松搞懂【TF-IDF、word2vec、svm、cnn、textcnn、bilstm、cnn+bilstm、bilstm+attention实现】英文长文本分类
  9. mayapython编译器_Maya 安装 Cython(详细)
  10. 2020最新5Sing音乐解析解析接口 简单实用
  11. html画布创建黑白象棋棋盘,canvas应用——中国象棋棋盘
  12. 如何在Chrome中自定义新标签页
  13. 计算机视觉城市排名,计算机视觉技术量化城市变迁
  14. 微信小程序-日期时间选择器
  15. 服务器光盘装centos7系统,光盘安装CentOS 7.4步骤
  16. 【GD32F310开发板试用】PWM+TMC5160驱动步进电机
  17. [Alpha阶段]第十次Scrum Meeting
  18. Clone Graph问题及解法
  19. POI之图片导出到PPT简单实例
  20. EXCEL VBA学习例子之【升降序】

热门文章

  1. CSS中的层叠性、继承性、优先级、权重
  2. 结构体变量和结构体指针变量作为函数参数传递问题
  3. 面试官问你B树和B+树,就把这篇文章丢给他
  4. Zookeeper集群为什么要是单数
  5. 从微信的改版中我看到了微信的焦虑
  6. 2020年抖音创作者生态报告
  7. 懂技术对产品有哪些帮助?
  8. “全息数字人”——健康医疗 大数据应用的新模式
  9. 【Python】Matplotlib使用Cursor实现UI定位
  10. 括号匹配不一定用栈哦(洛谷P1739题题解,Java语言描述)