Created
April 3, 2015 22:39
-
-
Save kumatti1/69c2c1b0501389d28a97 to your computer and use it in GitHub Desktop.
(C++)Twitterにログイン
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Shlwapi.h> | |
#include <windows.h> | |
#include <Exdisp.h> | |
#import <shdocvw.dll> | |
#import <mshtml.tlb> | |
#using <System.dll> | |
#using <System.Windows.Forms.dll> | |
using namespace System; | |
using namespace System::Runtime::InteropServices; | |
using namespace System::Windows::Forms; | |
#define ID L"" | |
#define PW L"" | |
#define URL L"https://twitter.com/login" | |
DWORD dwCookie = 0; | |
HANDLE hEvent = 0; | |
public ref class Hoge | |
{ | |
public: | |
[DispId(259)]//DISPID_DOCUMENTCOMPLETE | |
void DocumentComplete(Object^ pDisp, Object^ pvtURL) | |
{ | |
OutputDebugStringW(L"キタ━(゚∀゚)━!"); | |
SetEvent( hEvent ); | |
} | |
}; | |
HRESULT _Twitter(SHDocVw::IWebBrowser2Ptr pBrowser) | |
{ | |
HRESULT hr = S_OK; | |
try{ | |
MSHTML::IHTMLDocument2Ptr pDoc(pBrowser->Document); | |
MSHTML::IHTMLElementCollectionPtr pCol( pDoc->forms ); | |
MSHTML::IHTMLFormElementPtr pForm( pCol->item( _variant_t( 1 ) ) ); | |
MSHTML::IHTMLInputElementPtr pInput_ID( pForm->item( _variant_t( L"session[username_or_email]" ) ) ); | |
pInput_ID->value = ID; | |
MSHTML::IHTMLInputElementPtr pInput_PW( pForm->item( _variant_t( L"session[password]") ) ); | |
pInput_PW->value = PW; | |
pForm->submit(); | |
} | |
catch (_com_error& e) { | |
hr = e.Error(); | |
} | |
return hr; | |
} | |
HRESULT test() | |
{ | |
HRESULT hr = S_OK; | |
try{ | |
SHDocVw::IWebBrowser2Ptr pBrowser; | |
hr = pBrowser.CreateInstance(__uuidof(SHDocVw::InternetExplorer)); | |
if (FAILED(hr)) _com_issue_error(hr); | |
pBrowser->Visible = VARIANT_TRUE; | |
auto obj = gcnew Hoge; | |
IDispatchPtr pDisp; | |
pDisp.Attach( static_cast<IDispatch*>(Marshal::GetIDispatchForObject(obj).ToPointer() )); | |
hr = ConnectToConnectionPoint(pDisp, __uuidof(DWebBrowserEvents2), TRUE, pBrowser, &dwCookie, nullptr); | |
if (FAILED(hr)) _com_issue_error(hr); | |
pBrowser->Navigate( URL ); | |
SECURITY_ATTRIBUTES st={}; | |
hEvent = CreateEventW(&st, FALSE, FALSE, nullptr); | |
while(MsgWaitForMultipleObjects(1, &hEvent, FALSE, INFINITE, QS_ALLINPUT) == WAIT_OBJECT_0 + 1) | |
{ | |
Application::DoEvents(); | |
} | |
hr = ConnectToConnectionPoint( nullptr, __uuidof(DWebBrowserEvents2), FALSE, pBrowser, &dwCookie, nullptr); | |
CloseHandle( hEvent ); | |
_Twitter( pBrowser ); | |
} | |
catch (_com_error& e) { | |
hr = e.Error(); | |
} | |
return hr; | |
} | |
//[MTAThread] | |
int WINAPI WinMain(HINSTANCE, HINSTANCE, PSTR, int) | |
{ | |
CoInitialize(NULL); | |
test(); | |
CoUninitialize(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment