Skip to content

Instantly share code, notes, and snippets.

@kumatti1
Last active August 29, 2015 14:16
Show Gist options
  • Save kumatti1/36422d319017d2ad10a7 to your computer and use it in GitHub Desktop.
Save kumatti1/36422d319017d2ad10a7 to your computer and use it in GitHub Desktop.
IHTMLDocument2からのSystem::Windows::Forms::HtmlDocument変換
#include <Exdisp.h>
#import <shdocvw.dll>
#import <mshtml.tlb>
#using <System.Windows.Forms.dll>
using namespace System;
using namespace System::Windows::Forms;
using namespace System::Reflection;
using namespace System::Runtime::InteropServices;
#define URL L"https://www.google.co.jp/?gws_rd=ssl"
HRESULT test()
{
HRESULT hr = S_OK;
try{
SHDocVw::IShellWindowsPtr psw;
hr = psw.CreateInstance(__uuidof(SHDocVw::ShellWindows));
if (FAILED(hr)) _com_issue_error(hr);
_variant_t varLoc(_bstr_t(URL));
_variant_t VarRoot;
long h = 0;
IDispatchPtr pDisp = psw->FindWindowSW(&varLoc, &VarRoot, SWC_BROWSER, &h, SWFO_NEEDDISPATCH);
SHDocVw::IWebBrowser2Ptr pBrowser(pDisp);
MSHTML::IHTMLDocument2Ptr pDoc(pBrowser->Document);
Object^ o = Marshal::GetObjectForIUnknown( (IntPtr)pDoc.GetInterfacePtr() );
array<ConstructorInfo^>^ci = HtmlDocument::typeid->GetConstructors( BindingFlags::NonPublic | BindingFlags::Instance );
HtmlDocument ^doc = (HtmlDocument^)ci[0]->Invoke(gcnew array<Object^>{ nullptr, o });
String^ s = doc->Title;
MessageBox::Show( s );
}
catch (_com_error& e) {
hr = e.Error();
}
return hr;
}
int CALLBACK WinMain(
_In_ HINSTANCE hInstance,
_In_ HINSTANCE hPrevInstance,
_In_ LPSTR lpCmdLine,
_In_ int nCmdShow
)
{
CoInitialize(NULL);
test();
CoUninitialize();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment