Click here to Skip to main content
15,904,822 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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...

C++
#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;
}
Posted
Updated 7-Nov-11 15:17pm
v4
Comments
Member 7766180 7-Nov-11 20:21pm    
I dropped this in and I am getting these errors.

printf("\n Source IP: %s", ipSrc);
myfile << "\n Source IP:" << ipSrc;
PrintBrowserInfo(IWebBrowser2 *pBrowser);

2 IntelliSense: identifier "pBrowser" is undefined
1 IntelliSense: type name is not allowed
Philippe Mori 7-Nov-11 20:27pm    
IntelliSense message are not always errors. Sometime IntelliSense is not smart enough or not yet up-to-date. Try to build the application anyway.
Member 7766180 7-Nov-11 20:51pm    
Tried to several times. Won't run.

Error 2 error C2065: 'pBrowser' : undeclared identifier

Error 3 error C2275: 'IWebBrowser2' : illegal use of this type as an expression
Chuck O'Toole 7-Nov-11 21:13pm    
OK, so you moved the function from a header file to the CPP file. What did you do about including the header file? Were there other things in the header file that were important but you forgot to move, like the definition of "IWebBrowser2" or some nested include?
Member 7766180 7-Nov-11 21:19pm    
No, I have the header file in the includes of the cpp file.

1 solution

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"
 
Share this answer
 
Comments
Member 7766180 7-Nov-11 21:19pm    
Very nice answer! I think I know what to do now. Much appreciated. Thank you.
Sergey Alexandrovich Kryukov 7-Nov-11 21:26pm    
My 5. The question is a sample of the act of "mysterious mind", failure to think from first principles. May it sound silly, but this is demystification.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900