Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a cpp file. This is in that file...
The whole code....

C++
#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>


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


EnumExplorers(); come from a header file...it returns this...
C++
wprintf(L"\n   Current Website URL: %s", wsURL.c_str());
printf("\n   Current Website IP:%s", addr2);


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.
Posted
Updated 9-Nov-11 11:40am
v2

C++
// 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>
 
Share this answer
 
Comments
Member 7766180 9-Nov-11 19:15pm    
Thank you Jack, So does this replace everything I have posted in the question? Also how do I reference the hostIP in mycpp file? I'm sorry, kinda new only been doing this since the springtime.
Member 7766180 9-Nov-11 19:57pm    
OK I moved the addr2/currentaddress to a global variable in the header file and I can now access it. Thank you.
JackDingler 10-Nov-11 0:18am    
I tend to avoid using globals unless there's a good reason to use them. If I need singletons, I'll try to wrap them in a scope or make them static for a class so that there's little confusion on the purpose and fewer name collisions. Further with multi-threaded programming, the over use of global variables can lead to long weekends, working with no sleep. As you see in my example, I pass the variables to the function so that it doesn't have to rely on any global data. If you need a global variable for some other reason, then you can pass it as an argument.
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.
 
Share this answer
 
Comments
Member 7766180 9-Nov-11 16:15pm    
Thank you. OK I think I get what you are saying. I will try it now.

Have this...

addr2 = inet_ntoa(sockAddr2.sin_addr);
char currentaddress[100] = { 0 };
strcpy( currentaddress, inet_ntoa(sockAddr2.sin_addr ) );
printf("\n Current Website IP:%s", currentaddress);

char getCurrAdd(char currentaddress);
{
return currentaddress;
}

ut it says return value does not match function type?
JackDingler 9-Nov-11 17:21pm    
const char * getCurrAdd(const char * currentaddress);
{
return currentaddress;
}
Member 7766180 9-Nov-11 17:25pm    
Thank you Jack. It gives me the same error.
Return Value Type Doesn't Match Function Type. Am I putting it in the wrong place? Or what...........?

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')
{
}
const char * getCurrAdd(const char * currentaddress);
{
return currentaddress;
}
JackDingler 9-Nov-11 17:31pm    
I just notice the semicolon at the end of const char * getCurrAdd(const char * currentaddress);


It's not clear from your code snippet, what you're trying to do. It looks like you're declaring a function inside the body of another function that we can't see the details to. Then your returning a string pointer to a string declared on the stack. There's bad mojo in doing that.
Member 7766180 9-Nov-11 17:38pm    
Thank you Jack. What I am trying to do is get the value of currentaddress so that I can use it in my cpp file. Right now it's in a header file. If you want I can post the entire function, if that helps.

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