比较两个值 [英] Comparing Two Values

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

问题描述

我有一个cpp文件.这是在那个文件中...
整个代码....

I have a cpp file. This is in that file...
The whole code....

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

#pragma once
void EnumExplorers();
#pragma warning(disable: 4192)
#pragma warning(disable: 4146)
#import <mshtml.tlb>
#import <shdocvw.dll>
 
void PrintBrowserInfo(IWebBrowser2 *pBrowser) {
	WORD wVersionRequested;
	WSADATA wsaData;
	int err;
 	wVersionRequested = MAKEWORD( 2, 2 );
	err = WSAStartup( wVersionRequested, &wsaData );
	
	BSTR bstr;
 	pBrowser->get_LocationURL(&bstr);
	std::wstring wsURL;
	wsURL = bstr;
 	
	size_t DSlashLoc = wsURL.find(L"://");
	if (DSlashLoc != wsURL.npos)
		{
		wsURL.erase(wsURL.begin(), wsURL.begin() + DSlashLoc + 3);
		}
	DSlashLoc = wsURL.find(L"www.");
	if (DSlashLoc == 0)
		{
		wsURL.erase(wsURL.begin(), wsURL.begin() + 4);
		}
	DSlashLoc = wsURL.find(L"/");
	if (DSlashLoc != wsURL.npos)
		{
		wsURL.erase(DSlashLoc);
		}
		wprintf(L"\n   Current Website URL: %s", wsURL.c_str());
		char     ipSrc[20];
		int Newlength = WideCharToMultiByte (CP_ACP, WC_COMPOSITECHECK, wsURL.c_str(), -1, NULL, 0,  NULL, NULL); 
		std::string NewLogURL(Newlength+1, 0); 
		int Newresult = WideCharToMultiByte (CP_ACP, WC_COMPOSITECHECK, wsURL.c_str(), -1, &NewLogURL[0],Newlength+1,  NULL, NULL); 
		
		HOSTENT *pHostEnt2;
		int  **ppaddr2;
		SOCKADDR_IN sockAddr2;
		char* addr2;
		pHostEnt2 = gethostbyname(NewLogURL.c_str());
		ppaddr2 = (int**)pHostEnt2->h_addr_list;
		sockAddr2.sin_addr.s_addr = **ppaddr2;
		addr2 = inet_ntoa(sockAddr2.sin_addr);
		char currentaddress[100] = { 0 };
		strcpy( currentaddress, inet_ntoa(sockAddr2.sin_addr ) );
		if( currentaddress != NULL && currentaddress[0] == '\0')
		{  
		}
		printf("\n   Current Website IP:%s", currentaddress);
		
		//////////////This is for the log file only//////////////////////////
		int length = WideCharToMultiByte (CP_ACP, WC_COMPOSITECHECK, wsURL.c_str(), -1, NULL, 0,  NULL, NULL); 
		std::string LogURL(length+1, 0); 
		int result = WideCharToMultiByte (CP_ACP, WC_COMPOSITECHECK, wsURL.c_str(), -1, &LogURL[0],length+1,  NULL, NULL); 
		myfile << "\n   Current Website URL:" << LogURL;
		myfile << "\n   Current Website IP:"<< addr2;
		/////////////////////////////////////////////////////////////////////
	SysFreeString(bstr);
}
 
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");
	}
}</shdocvw.dll></mshtml.tlb></string></exdisp.h></windows.h></wchar.h></stdio.h>





EnumExplorers();
printf("\n   Source      IP: %s", ipSrc);
myfile << "\n   Source      IP:" << ipSrc;



EnumExplorers();来自头文件...它返回这个...



EnumExplorers(); come from a header file...it returns this...

wprintf(L"\n   Current Website URL: %s", wsURL.c_str());
printf("\n   Current Website IP:%s", addr2);



问题是我需要将头文件中的addr与cpp文件中的ipSrc进行比较.我该怎么办?
谢谢.



The problem is that I need to compare the addr from the header file with ipSrc from the cpp file. How can I do this?
Thank you.

推荐答案

// CodeProjectHelp1.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <string>
#include <winsock2.h>
#include <ws2tcpip.h>

bool GetHostIPAddress(std::string & DestAddress, const std::string & wsUrl)
{
	HOSTENT *pHostEnt2; 
	int **ppaddr2; 
	SOCKADDR_IN sockAddr2; 
	char* addr2; 
	
	pHostEnt2 = gethostbyname(wsUrl.c_str());
	if (!pHostEnt2)
		return false;

	ppaddr2 = (int**)pHostEnt2->h_addr_list; 
	sockAddr2.sin_addr.s_addr = **ppaddr2; 
	addr2 = inet_ntoa(sockAddr2.sin_addr); 
	
	DestAddress = inet_ntoa(sockAddr2.sin_addr); 

	if (DestAddress == "")
		return false;

	return true;
}


int _tmain(int argc, _TCHAR* argv[])
{
	WSADATA wsaData;
	int iResult;

    // Initialize Winsock
    iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
    if (iResult != 0) {
        printf("WSAStartup failed: %d\n", iResult);
        return 1;
    }

	std::string NewLogURL = "www.codeproject.com";
	std::string DestAddress;

	if (GetHostIPAddress(DestAddress, NewLogURL))
		printf("Address is %s\r\n", DestAddress.c_str());
	else
		printf("Unable to parse address.\r\n");

	return 0;
}
</ws2tcpip.h></winsock2.h></string>


EnumExplorers()不会返回"任何东西.它打印"了一些东西,但该函数未返回值.

如果要从EnumExplorers()获得数据,则必须教它返回结果,可能是某种字符串.

哦,等等,名称"Enum"的含义是枚举",因此它可能找到并打印许多行,因此它可能无法返回简单的字符串,可能是字符串数组以及它们的计数或列表或一些动态变量.数组.

您明白了,您将必须编写代码以捕获每个"addr2",并将其放置在返回的位置,然后编写一些代码以将结果(一个或多个)与调用方中的ipSrc进行比较.
EnumExplorers() doesn''t "return" anything. It "prints" some stuff but the function does not return a value.

If you want the data from EnumExplorers(), you''ll have to teach it to return results, probably some sort of string.

Oh wait, the name "Enum" sort of implies "enumerate" so it may find and print many lines so it may not be able to return a simple string, maybe an array of strings and the count of them or a list or some dynamic array.

You get the idea, you''ll have to write code to capture each "addr2" and put it somewhere for the return and then write some code to compare the result (one or many) to ipSrc in the caller.


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

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