本文介绍了从标题调用函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在cpp文件中包含的头文件中有此代码,并且我试图调用此函数,该函数返回当前网页的url.从头文件运行该函数时,该函数起作用,现在如何从cpp文件中运行该函数?该函数是... PrintBrowserInfo()

代码...

I have this code in a header file that I included in my cpp file and I am trying to call this function which returns the url of the current webpage. The function works fime when I run it from the header file, now how do I get it to run from the cpp file? The function is...PrintBrowserInfo()

The code...

#include "stdafx.h"
#include <stdio.h>
#include <wchar.h>
#include <windows.h>
#include <exdisp.h>
#include <string>

#pragma warning(disable: 4192)
#pragma warning(disable: 4146)
#import <mshtml.tlb>
#import <shdocvw.dll>
//using namespace std;

void PrintBrowserInfo(IWebBrowser2 *pBrowser) {
	BSTR bstr;
 	IDispatchPtr spDisp;
	if (pBrowser->get_Document(&spDisp) == S_OK && spDisp != NULL) {
		MSHTML::IHTMLDocument2Ptr spHtmlDocument(spDisp);
		if (spHtmlDocument != NULL) {
			MSHTML::IHTMLElementPtr spHtmlElement;
			spHtmlDocument->get_body(&spHtmlElement);
		}
		spDisp.Release();
	}
	pBrowser->get_LocationURL(&bstr);
	wprintf(L"  URL: %s\n\n", bstr);
	/////////////////////////////////
	std::wstring wsURL;
	wsURL = bstr;
 	size_t DSlashLoc = wsURL.find(L"//www.");
 	if (DSlashLoc >= 0)
	{
		wsURL.erase(wsURL.begin(), wsURL.begin() + DSlashLoc + 6);
	}
		DSlashLoc = wsURL.find(L"/");
			if (DSlashLoc != wsURL.npos)
                 wsURL.erase(DSlashLoc);
 	wprintf(L"  URL: %s\n\n", wsURL.c_str());
	///////////////////////////////////
	SysFreeString(bstr);
}

int urlmain() {
	CoInitialize(NULL);
	SHDocVw::IShellWindowsPtr spSHWinds;
	IDispatchPtr spDisp;
	if (spSHWinds.CreateInstance(__uuidof(SHDocVw::ShellWindows)) == S_OK) {
		long nCount = spSHWinds->GetCount();
		for (long i = 0; i < nCount; i++) {
			_variant_t va(i, VT_I4);
			spDisp = spSHWinds->Item(va);
			SHDocVw::IWebBrowser2Ptr spBrowser(spDisp);
			if (spBrowser != NULL) {
				PrintBrowserInfo((IWebBrowser2 *)spBrowser.GetInterfacePtr());
				spBrowser.Release();
			}
		}
	} else {
		puts("Shell windows failed to initialise");
	}
	system("PAUSE");
	return 0;
}

推荐答案


这篇关于从标题调用函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 18:23
查看更多