Click here to Skip to main content
15,905,144 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I need to rename int main() but I can't. Whenever I try the code won't compile, the problem is I already have a main() in my app.

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 main() {
	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 19:52pm
v3
Comments
Richard MacCutchan 8-Nov-11 5:21am    
Your sample does not illustrate the problem you are having. Try a smaller sample without all the extraneous code.

1 solution

I don't understand the issue you are having.
You can rename the function to whatever you like. Here is an example (with a few corrections to other portions of 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;
 	/*//This code was used to detect if the explorer found was Internet Explorer or Windows Explorer (my computer)
	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."); //Note: not all URLs begin with "www."
 	if (DSlashLoc != wsURL.npos) //size_t is an unsigned type, it's value cannot be negative
	{
		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);
}

//Changed return type, we don't really need a return from this
void EnumExplorers() {
	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");
	}
	//Deleted pause and return
}


Just call EnumExplorers(); from anywhere and it will work. If you need to call it from another .cpp file, just create a new header file with
C++
#pragma once
void EnumExplorers();

And include that file as normal.
 
Share this answer
 
Comments
Member 7766180 8-Nov-11 11:50am    
Thank you Andrew. I made a new header file and include what you suggested and it works fine. I did that before and called PrintBrowserInfo(IWebBrowser2 *pBrowser) was I calling the wrong thing? Was that not callable? I see what you did. I wil remember this in the future. Thank you. BTW I have never seen a web address without www. what other options are there? Thank you once again.

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