Web網頁表單

wangjiefein發表於2012-12-18


#include <windows.h>
#include <stdio.h>
#include <exdisp.h>
#include <mshtml.h>
#include <objbase.h>
#include <atlbase.h>
#include <oleacc.h>
//#define _WIN32_DCOM

/*class CComDispatchDriver

{
public:
 IDispatch* p;

       CComDispatchDriver()    

       {      p = NULL;    }
     

CComDispatchDriver(IDispatch * lp)   

              {

                     if((p = lp) != NULL)

                            p->AddRef();

              }

       CComDispatchDriver(IUnknown *lp)

       {//從這個函式可以看出,如果QueryInterface失敗,這個類是不會發出通知的

              p = NULL;

              if(lp != NULL)

                     lp->QueryInterface(IID_IDispatch,(void**)&p);

       }
};*/

LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;  //視窗函式宣告
BOOL CALLBACK EnumChildProc(HWND hwnd,LPARAM lParam);  
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,PSTR szCmdLine, int iCmdShow)   //WinMain函式說明
{
 
      static TCHAR szAppName[] = TEXT ("HelloWin") ;
      HWND hwnd ; //視窗控制程式碼
      MSG msg ; //訊息結構
      WNDCLASS wndclass ; //視窗類結構


      //下面是視窗類的定義
      wndclass.style = CS_HREDRAW | CS_VREDRAW ;
      wndclass.lpfnWndProc = WndProc ;
      wndclass.cbClsExtra = 0 ;
      wndclass.cbWndExtra = 0 ;
      wndclass.hInstance = hInstance ;
      wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ;//載入圖示供程式使用
      wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ; //載入滑鼠指標供程式使用
      wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;//獲取一個圖形物件,在這個例子中,是獲取繪製視窗背景的刷子
      wndclass.lpszMenuName = NULL ;
      wndclass.lpszClassName = szAppName ;


     //下面是視窗類的註冊
      if (!RegisterClass (&wndclass))//為程式視窗註冊視窗類
     {
           return 0 ;
     }


     //根據視窗類建立視窗
     hwnd = CreateWindow (szAppName, TEXT ("一個簡單的Win32程式"),
                  WS_OVERLAPPEDWINDOW,
                  CW_USEDEFAULT,
                  CW_USEDEFAULT,
                  CW_USEDEFAULT,
                  CW_USEDEFAULT,
                  NULL,
                  NULL,
                  hInstance,
                  NULL
                ) ;
 //InitCommonControls();//link comctl32.lib

 CoInitialize(0);
 IWebBrowser2*  pWebBrowser2=NULL;
 IWebBrowser2* ptst;
 HWND hWnd;

 HINSTANCE hInst=::LoadLibrary("OLEACC.DLL");
   hWnd=FindWindow("IEFrame",NULL);

    IStream* pStream=NULL;
  HWND hWndChild;
  ::EnumChildWindows(hWnd,EnumChildProc,(LPARAM)&hWndChild);
  if(hWndChild)
  {
   HRESULT hr;
   CComPtr<IHTMLDocument2> spDoc;

  /*LPFNACCESSIBLEOBJECTFROMWINDOW pfAccessibleObjectFromWindow=(LPFNACCESSIBLEOBJECTFROMWINDOW)::GetProcAddress(hInst,"AccessibleObjectFromWindow");
  CComPtr<IAccessible> spAccess;
   hr=pfAccessibleObjectFromWindow(hWnd,0,IID_IAccessible,(void**)&spAccess);
   CComPtr<IServiceProvider> spServiceProv;
   hr=spAccess->QueryInterface(IID_IServiceProvider,(void**)&spServiceProv);

  CComPtr<IHTMLWindow2> spWin;
   hr=spServiceProv->QueryService(IID_IHTMLWindow2,IID_IHTMLWindow2,(void**)&spWin);*/

  
  LRESULT lRes;
  UINT nMsg=::RegisterWindowMessage("WM_HTML_GETOBJECT");
  ::SendMessageTimeout(hWndChild,nMsg,0L,0L,SMTO_ABORTIFHUNG,1000,(DWORD*)&lRes);
  LPFNOBJECTFROMLRESULT pfObjectFromLresult=(LPFNOBJECTFROMLRESULT)::GetProcAddress(hInst,"ObjectFromLresult");
  
  hr=(*pfObjectFromLresult)(lRes,IID_IHTMLDocument2,0,(void**)&spDoc);
  if(SUCCEEDED(hr))
  {
   CComPtr<IHTMLWindow2> spWnd2;
   CComPtr<IServiceProvider> spServiceProv;
  
   hr=spDoc->get_parentWindow((IHTMLWindow2**)&spWnd2);
   if(SUCCEEDED(hr))
   {
   hr=spWnd2->QueryInterface(IID_IServiceProvider,(void**)&spServiceProv);
     if(SUCCEEDED(hr))
     {
      hr=spServiceProv->QueryService(IID_IWebBrowserApp,IID_IWebBrowser2,(void**)&pWebBrowser2);
     if(SUCCEEDED(hr))
     {
      ptst=pWebBrowser2;
      

 USES_CONVERSION;
 CComPtr<IDispatch> spDispDoc;
 hr=ptst->get_Document(&spDispDoc);
 CComQIPtr<IHTMLDocument2> spDocument2;
 spDispDoc->QueryInterface(IID_IHTMLDocument2,(void**)&spDocument2);

  

 CComPtr<IHTMLElementCollection> spElementCollection;
 hr=spDocument2->get_forms(&spElementCollection);//get form number
 long nFormCount=0;
 hr=spElementCollection->get_length(&nFormCount);
 for(long i=0; i<nFormCount; i++)
 {
  IDispatch* pDisp=NULL;
  hr=spElementCollection->item(CComVariant(i),CComVariant(),&pDisp);
  if(FAILED(hr))
   continue;
  CComQIPtr<IHTMLFormElement> spFormElement;
  hr=pDisp->QueryInterface(IID_IHTMLFormElement,(void**)&spFormElement);
  if(FAILED(hr))
   continue;
  pDisp->Release();

  long nElemCount=0;
  hr=spFormElement->get_length(&nElemCount);//get form area域 number
  if(FAILED(hr))
   continue;

 

  CComQIPtr<IHTMLElement> pElement;
  for(long j=0; j<nElemCount; j++)
  {
   IDispatch* spDispatch=NULL;
   hr=spFormElement->item(CComVariant(j),CComVariant(),&spDispatch);
   if(FAILED(hr))
    continue;
   pElement=spDispatch;
  
   spDispatch->Release();

   BSTR bstr;
   hr=pElement->get_tagName(&bstr);
   if(FAILED(hr)) continue;
   VARIANT vv;
   hr=pElement->getAttribute(CComBSTR("type"),0,&vv);//"text" "hidden" "submit"
   if(FAILED(hr)) continue;
   
   
  CComBSTR tst,tes1;
  BSTR tes,tes2;
    if(*vv.bstrVal==*SysAllocString(L"text"))
    {
     CComPtr <IHTMLInputTextElement> pInputTextElement;
     hr=pElement->QueryInterface(IID_IHTMLInputTextElement,(void**)&pInputTextElement);
     if(FAILED(hr))
      continue;
     CComBSTR sInputText;
     hr=pInputTextElement->get_type(&sInputText);
     if(FAILED(hr))
      continue; 
     
     
      tes=sInputText.m_str;
     
     tes1.Attach(SysAllocString(L"text"));
      tes2=tes1.m_str;
     if(*tes==*tes2)
     {
      
      
      BSTR myAttachBSTR = SysAllocString(L"359412094@qq.com");  
      tst.Attach(myAttachBSTR);  
      pInputTextElement->put_value(tst);
     }
    }
    
    else if(*vv.bstrVal==*SysAllocString(L"password"))
    {
     CComPtr <IHTMLInputTextElement> pInputPassElement;
     hr=pElement->QueryInterface(IID_IHTMLInputTextElement,(void**)&pInputPassElement);
     if(FAILED(hr))
      continue;
     CComBSTR sInputPass;
     hr=pInputPassElement->get_type(&sInputPass);
     if(FAILED(hr))
      continue;
     
     tes=sInputPass.m_str;
     tes1.Attach(SysAllocString(L"password"));
     tes2=tes1.m_str;
     if(*tes==*tes2)
     {   
      tst.Attach(SysAllocString(L"112101jiefei"));  
      pInputPassElement->put_value(tst);
     }
    }
    else if(*vv.bstrVal==*SysAllocString(L"submit"))
    {
     CComPtr <IHTMLElement> pClickElement;
     hr=pElement->QueryInterface(IID_IHTMLElement,(void**)&pClickElement);
     if(FAILED(hr))
      continue;
     hr=pClickElement->click();
     
    }
   
  }
 }

 
 

    
     }
     }
   }
  }
  pWebBrowser2->Release();
  ptst->Release();
   ::FreeLibrary(hInst); 
  }
  CoUninitialize();
 

     //顯示視窗
  
 
  ShowWindow(hwnd,iCmdShow);
 ShowWindow(hWnd,SW_MAXIMIZE);
     UpdateWindow (hwnd) ; //指示視窗重新整理自身
  UpdateWindow (hWnd) ; //指示視窗重新整理自身
 
 

     //訊息迴圈

     while (GetMessage (&msg, NULL, 0, 0))
     {
          TranslateMessage (&msg) ; //轉換某些鍵盤訊息
           DispatchMessage (&msg) ; //將訊息傳送給視窗過程
     }
     return msg.wParam;
}
//視窗函式
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{ HDC hdc ;
    PAINTSTRUCT ps ;
    RECT rect ;

    switch (message)
    {
         case WM_PAINT:
                hdc = BeginPaint (hwnd, &ps) ; //開始視窗繪製
                GetClientRect (hwnd, &rect) ; //獲取視窗客戶區的尺寸
               // DrawText (hdc, TEXT ("Hello World!"), -1, &rect,
               //  DT_SINGLELINE | DT_CENTER | DT_VCENTER) ; //顯示Hello World,可以修改程式你要顯示的內容
  //navigate("http://uasm.banzhu.com/");
                EndPaint (hwnd, &ps) ; //結束視窗繪製
                return 0 ;

         case WM_DESTROY:
                PostQuitMessage (0) ; //在訊息佇列中插入一條“退出”訊息
         
    return 0 ;

 case WM_CHAR:
    char szWang[20];
    sprintf(szWang,"char is %d.",wParam);
    ::MessageBox(hwnd,szWang,NULL,IDOK);
    return 0;

    } 
     return DefWindowProc (hwnd, message, wParam, lParam);//執行預設的訊息處理 
}
BOOL CALLBACK EnumChildProc(HWND hwnd,LPARAM lParam)  
{
 TCHAR buf[100]; 
    ::GetClassName( hwnd, (LPTSTR)&buf, 100 ); 
    if ( strcmp( buf, ("Internet Explorer_Server") ) == 0 )  
    { 
        *(HWND*)lParam = hwnd; return FALSE;  
    }  
   else return TRUE;  
}

相關文章