cpp文件中的两个main() [英] Two main() s in a cpp file

查看:102
本文介绍了cpp文件中的两个main()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要重命名int main()但我不能.每当我尝试代码无法编译时,问题是我的应用程序中已经有一个main().

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.

#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;
}

推荐答案

我不明白您遇到的问题.
您可以将函数重命名为任意名称.这是一个示例(对代码的其他部分进行了一些更正)

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)

#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
}



只需从任何地方调用EnumExplorers();,它将起作用.如果您需要从另一个.cpp文件中调用它,只需使用
创建一个新的头文件



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

#pragma once
void EnumExplorers();


并正常包含该文件.


And include that file as normal.


这篇关于cpp文件中的两个main()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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