从标题调用函数 [英] Calling A Function From A Header

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

问题描述

我在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;
}

推荐答案

好吧,这听起来很奇怪,但是头文件没有任何特殊"之处.它们实际上只是在适当位置编译的文本文件.认为它们就像您只是在#include语句所在的位置粘贴"了头文件的内容一样.

创建头文件的原因是允许多个源文件共享相同的定义和声明,而不必一遍又一遍地粘贴"相同的内容或在多个文件中具有相同的内容,然后在需要时尝试使其保持最新状态改变一些东西.

因此,知道可执行文件没有特殊的标头文件输出",标头中的任何内容都包含在CPP文件的目标文件中.

通用约定说不将代码"放入头文件,而仅在其中放置声明和定义,而将代码保留在cpp模块中.这是为了防止您使用例程的多个副本来填充可执行文件.实际上,您可能会遇到链接器错误,或者链接器足够聪明,可以折叠多个条目,具体取决于函数是什么.而且,实际上,Microsoft始终将代码粘贴在其头文件中.

因此,您不必在标头中执行某些操作",而应在CPP文件的目标文件中执行.但是,如果让您更舒适地将其保存在CPP文件中,只需从标题复制/剪切它,然后将其粘贴到CPP文件中即可.

(现在有一个预编译的头文件,但这只是通过避免针对不同的CPP模块重复头文件的解析来加快编译速度的一种方式,但是我们可以避免这种怪癖直到第二天,这与本次讨论并不相关).

而且由于您原来的问题说它可以运行,所以我只是在解决您有关使其从CPP文件运行"的问题.
OK, this is going to sound strange but there''s nothing "special" about header files. All they really are are text files compiled in place. Think of them as if you just "pasted" the content of the header file right where the #include statement is.

The reason to create a header file is to allow multiple source files to share the same definitions and declarations without "pasting" the same stuff over and over or having the same thing in multiple files and then trying to keep them up to date when you want to change something.

So, knowing that, there''s no special "output of the header file" that is part of the executable, anything in the header is included in the object file of the CPP file.

Common conventions say not to put "code" into header files but to put only declarations and definitions there, leaving the code to be in the cpp modules. This is to prevent you bulking up your executable with multiple copies of routines. In reality, you will either get linker errors or the linker will be smart enough to collapse the multiple entries, depending on what the function is. And, in reality, Microsoft sticks code in their header files all the time.

So, you don''t "execute something in the header", it''s in the CPP file''s object file. However, if it makes you feel more comfortable to have it in the CPP file, just copy / cut it from the header and paste it into the CPP file.

(now there is such a thing as a precompiled header file but all that is is just a way of making the compilation go faster by avoiding the parsing of the header file over and over for different CPP modules but we can avoid that quirk until another day, it''s not really relevant to this discussion).

And since your original question says you got it to run, I''m only addressing your questions about getting it to "run from the CPP file"


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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆