一个调用当前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);