Home » U++ Library support » U++ Core » LaunchWebBrowser() problem and perhaps not the best solution 
	
		
		
			| LaunchWebBrowser() problem and perhaps not the best solution [message #35714] | 
			Wed, 14 March 2012 11:40   | 
		 
		
			
				
				
				  | 
					
						  
						koldo
						 Messages: 3453 Registered: August 2008 
						
					 | 
					Senior Veteran  | 
					 | 
		 
		 
	 | 
 
	
		Hello all 
 
In my XP LaunchWebBrowser() does not work because of ShellExecute. 
 
A possible solution proposed here ( http://stackoverflow.com/questions/1193873/which-reasons-cou ld-make-shellexecute-fail) is to create a separate thread for executing ShellExecute.  
The reason could be this one(Calling Shell Functions and Interfaces from a Multithreaded Apartment:  http://support.microsoft.com/?scid=kb%3Ben-us%3B287087&x =16&y=15). 
 
The solution works for me. First call to ShellExecuteOpen() fails but second one in a separate thread, works.  
However I am not sure if it is a right implementation. Please check it before approve. 
 
 
#if defined(PLATFORM_WIN32) && !defined(PLATFORM_WINCE)
bool ShellExecuteOpen(const char *str)
{
	return 32 < int(ShellExecuteW(NULL, L"open", ToSystemCharsetW(str), NULL, L".", SW_SHOWDEFAULT));
}
void LaunchWebBrowser(const String& url)
{
	if (!ShellExecuteOpen(url)) {
		HANDLE handle = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)&ShellExecuteOpen, (LPVOID)~url, 0, NULL);
		if (handle == NULL)
			return;
		WaitForSingleObject(handle, 500);
		CloseHandle(handle);
	} 
}
#endif 
		
		
  Best regards 
Iñaki
		
 |  
	| 
		
	 | 
 
 
 |  
	
		
		
			| Re: LaunchWebBrowser() problem and perhaps not the best solution [message #35731 is a reply to message #35714] | 
			Wed, 14 March 2012 22:37    | 
		 
		
			
				
				
				  | 
					
						  
						mirek
						 Messages: 14271 Registered: November 2005 
						
					 | 
					Ultimate Member  | 
					 | 
		 
		 
	 | 
 
	
		| koldo wrote on Wed, 14 March 2012 06:40 |   Hello all 
 
In my XP LaunchWebBrowser() does not work because of ShellExecute. 
 
A possible solution proposed here (  http://stackoverflow.com/questions/1193873/which-reasons-cou ld-make-shellexecute-fail) is to create a separate thread for executing ShellExecute.  
The reason could be this one(Calling Shell Functions and Interfaces from a Multithreaded Apartment:   http://support.microsoft.com/?scid=kb%3Ben-us%3B287087&x =16&y=15). 
 
The solution works for me. First call to ShellExecuteOpen() fails but second one in a separate thread, works.  
However I am not sure if it is a right implementation. Please check it before approve. 
 
 
#if defined(PLATFORM_WIN32) && !defined(PLATFORM_WINCE)
bool ShellExecuteOpen(const char *str)
{
	return 32 < int(ShellExecuteW(NULL, L"open", ToSystemCharsetW(str), NULL, L".", SW_SHOWDEFAULT));
}
void LaunchWebBrowser(const String& url)
{
	if (!ShellExecuteOpen(url)) {
		HANDLE handle = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)&ShellExecuteOpen, (LPVOID)~url, 0, NULL);
		if (handle == NULL)
			return;
		WaitForSingleObject(handle, 500);
		CloseHandle(handle);
	} 
}
#endif 
  |  
  
 
Looks quite OK with me. Maybe the WaitForSingleObject is not needed... 
 
Mirek
		
		
		
 |  
	| 
		
	 | 
 
 
 |  
	
		
		
			| Re: LaunchWebBrowser() problem and perhaps not the best solution [message #35737 is a reply to message #35731] | 
			Thu, 15 March 2012 08:41    | 
		 
		
			
				
				
				  | 
					
						  
						koldo
						 Messages: 3453 Registered: August 2008 
						
					 | 
					Senior Veteran  | 
					 | 
		 
		 
	 | 
 
	
		Hello Mirek 
 
In fact WaitForSingleObject() is useless as it always wait the timeout. 
 
However I realized that sometimes the string passed is released BEFORE the thread is really run so second call to ShellExecuteW() fails as LaunchWebBrowser() has ended before  . 
 
The proposed solution is this one: 
 
void ShellExecuteOpen(char *str)
{
	ShellExecuteW(NULL, L"open", ToSystemCharsetW(str), NULL, L".", SW_SHOWDEFAULT);
	free((void *)str);
}
void LaunchWebBrowser(const String& url)
{
	if (int(ShellExecuteW(NULL, L"open", ToSystemCharsetW(url), NULL, L".", SW_SHOWDEFAULT)) <= 32) {
		char *curl = (char *)malloc(url.GetLength()+1);
		strcpy(curl, url);
		HANDLE handle = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)&ShellExecuteOpen, (LPVOID)curl, 0, NULL);
		if (handle == NULL)
			return;
		CloseHandle(handle);
	} 
} 
 
The thread receives the string and releases it.
		
		
  Best regards 
Iñaki
		
 |  
	| 
		
	 | 
 
 
 |  
	| 
		
 |  
	| 
		
 |  
	| 
		
 |  
	| 
		
 |   
Goto Forum:
 
 Current Time: Tue Nov 04 01:36:11 CET 2025 
 Total time taken to generate the page: 0.15787 seconds 
 |