Click here to Skip to main content
15,920,896 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm getting the address from the location bar of my current browser window, like this "http://www.codeproject.com/KB/webforms/FileUploadWithProgrss.aspx"
However I just want this: www.codeproject.com

How would I do this?

Below is my code.
C++
#include "stdafx.h"
#include <stdio.h>
#include <wchar.h>
#include <windows.h>
#include <exdisp.h>

#pragma warning(disable: 4192)
#pragma warning(disable: 4146)
#import <mshtml.tlb>
#import <shdocvw.dll>
 
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);
	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'm using multi-byte character set.
Posted
Updated 7-Nov-11 9:25am
v3

You can try this...

C++
std::wstring wsURL = bstr;

size_t DSlashLoc = wsURL.find(L"//");

if (DSlashLoc >= 0)
{
    wsURL.erase(wsURL.begin(), wsURL.begin() + DSlashLoc + 2);
    wsURL.erase(wsURL.begin() + wsURL.find(L'/'));
}

wprintf(L"  URL: %s\n\n", wsURL.c_str());
 
Share this answer
 
Comments
Member 7766180 7-Nov-11 15:47pm    
This looks doable for me (at my level) except I'm getting an error on this.
std::wstring wsURL = bstr;
wtring is not a member of std.
JackDingler 7-Nov-11 15:52pm    
include string in your header list.
Member 7766180 7-Nov-11 15:58pm    
OK, I ran it and I am getting this..The original

URL: http://www.codeproject.com/Questions/279676/Getting-The-Basic-Web-Address



With your code
URL: www.codeproject.comQuestions/279676/Getting-The-Basic-Web-Address
Member 7766180 7-Nov-11 16:14pm    
Should be a way to erase everything to the right of "/" including "/"?
JackDingler 7-Nov-11 16:46pm    
This line should erase everything starting at '/';
wsURL.erase(wsURL.begin() + wsURL.find(L'/'));

Be sure your wprintf statement doesn't have the original bstr in it.
 
Share this answer
 
Comments
Member 7766180 7-Nov-11 15:42pm    
Another great answer!
Sergey Alexandrovich Kryukov 7-Nov-11 15:43pm    
I'm glad if you like it.
--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