Open URL with ShellExecute - SW_SHOWMAXIMIZED dont active window in C++ - c++

I used this function to open new tab in Chrome and active it:
ShellExecuteA(0,0,"chrome.exe","http://google.com --incognito",0,SW_SHOWMAXIMIZED);
but Chrome only open new tab but it doesnt active window.
(I call this function from global keyboard-hook of an application with no user interface, if user press specified key).
How I can fix it?

Looks like a bug in chrome.exe. I could repro with your ShellExecute call from a simple console app, if a regular (non-incognito) chrome.exe session was running and no incognito session was running. In other words, if a new incognito chrome session needed to be spawned, the regular session did not appear to correctly propagate the ShowWindow flags to the spawned incognito process. Another factor was that the activation failure also required the regular chrome session to be active before the test app ran. If any other app was active (say notepad.exe), then activation of the incognito session succeeded. The same behavior occurs with ShellExecuteEx and CreateProcess. Observing in Process Explorer (from sysinternals), it's clear that chrome.exe is forking the child process as necessary and then terminating itself. This makes it difficult to intercept an hProcess or processId in order to ultimately call SetActiveWindow.

Its not possible. You have to make Google Chrome as default browser and than try this:
(only tested on WinXP using
IE6)
first a function, that finds the default app for any File extension:**
enter code here
#include<Registry.hpp>
AnsiString GetDefaultApp(AnsiString ext)
{
TRegistry* reg = new(TRegistry);
reg->RootKey = HKEY_CURRENT_USER;
if(!reg->OpenKeyReadOnly("Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\FileExts\\."+ext+"\\OpenWithList"))
return(NULL);
try
{
AnsiString MRUList = reg->ReadString("MRUList");
AnsiString ret = reg->ReadString(AnsiString(char(MRUList[1])));
return(ret);
}
catch(...)
{
return(NULL);
}
}
now the code to launch the default app for html files and giving
the URL as parameter:**
#include<shellapi>
void OpenURL(AnsiString URL)
{
AnsiString app = GetDefaultApp("html");
if(app == NULL)
return;
ShellExecute(NULL,"open",app.c_str(),URL.c_str(),NULL,SW_SHOWDEFAULT);
}
Now you can open a URL in a new Browser using this command:
OpenURL("http://www.AlgorithMan.de/");

Related

How to create a session for Run Dialog in Appium?

By using powershell command I can get the run dialog program id, which is Microsoft.Windows.Shell.RunDialog. However, I can't get this working with the code below. Any idea?
DesiredCapabilities desktopCapabilities = new DesiredCapabilities();
desktopCapabilities.SetCapability("app", "Microsoft.Windows.Shell.RunDialog");
desktopCapabilities.SetCapability("deviceName", "WindowsPC");
desktopCapabilities.SetCapability("platformName", "Windows");
session = new WindowsDriver<WindowsElement>(new Uri("http://127.0.0.1:4723"), desktopCapabilities);
Take a look at the answers from this post about the run dialog. My best guess is that winappdriver is calling rundll32.exe and that's not the dialog itself.
You could try your luck changing this line
desktopCapabilities.SetCapability("app", "Microsoft.Windows.Shell.RunDialog");
into
desktopCapabilities.SetCapability("app", "c:\windows\system32\rundll32.exe shell32.dll,#61");
Alternatively, you could get the desktop session and send "windows key + r" to it. Here is how you can get the desktop session.

How to activate already opened application when user tries to run it for the second time?

I am using Mutex to limit my application to only one instance. This is the code:
HANDLE hMutex;
hMutex = CreateMutex(NULL, FALSE, "MyTestApp");
if(hMutex == NULL)
ShowMessage(GetLastError());
else
if(GetLastError() == ERROR_ALREADY_EXISTS) {
ShowMessage("Application already running!");
// activate already running instance ?!
return -1;
}
I would like to expand it to activate the already running instance. How to do it? Thanks.
Assuming it's Win32 application:
1) Use FindWindow function with your window name and its class name you gave.
2) Use SetForegroundWindow with HWND FindWindow returned.
Once you have determined that no other instance of your application is running, overload the WndProc of your Main form handle a unique message (Like const unsigned int myMsgId = WM_USER + 100; for example) or you can use RegisterWindowMessage. When the 2nd instance of the application starts, broadcast a message with that message identifier PostMessage(NULL, myMsgId, 0, 0) before creating the main form and quit. The 1st instance of the app can react to it and bring itself to foreground using BringWindowToTop (if possible, vista and above do not allow this for obvious reasons). If bringing the app to foreground is not allowed, the status bar icon of your app will start flashing to get the attention of the user.
Sam

Check Dialog is open or not in MFC VC ++

I have an application (Manager) used to send commands to another application (Instructor) through sockets. From the first application I will have configure some data which is useful to invoke and run the second application. Same time im getting reports from the second application to first application.
Let me explain my question, I have the set of exercises which has to be run in second application. Either I can invoke it from First app or from the second app by invoking the exercise dialog. Once I invoked, I can get the report. For that I have one button in my first app.
Here whats happening,before the exercise dialog get invoked,when i press this button, it shows error.
So, I just want to know whether that dialog is opened or not.
I use GetSafeHwnd(), but once the object created for that dialog class, these handle get some value even the dialog is not open.
Here I pasted the code, once I get the button press 'GenXL' command from first app.
extern CPerfScore *oPerfScore;
void CMainFrame::ProcessPendingRead(void)
{
int nRead;
CString strBuf;
CString sCmd;
nRead = m_pCltSocket->Receive( &m_pRecPacket, sizeof(Packet));
if(nRead > 0)
{
// read the message
sCmd = m_pRecPacket.sMessage;
AfxMessageBox(sCmd);
if (sCmd.CompareNoCase("CLOSE") == 0)
{
OnClose();
}
if (sCmd.CompareNoCase("GENXL") == 0)
{
if(oPerfScore->GetSafeHwnd() == 0)
{
oPerfScore->SendMessage(WM_COMMAND, IDC_GENERATE_EXCEL);
}
else
{
AfxMessageBox("Exercise dialog not open");
}
}
}
}
A handle will be valid even if the window isn't currently being shown, as long as it has been created. I think you're looking for the API call IsWindowVisible(). I believe MFC wraps this as a member.

I want to restart my application on restart of the system

I use GetModuleFileName to get the absolute path to my application, open the RunOnce registry key using RegOpenKeyEx and set a value using RegSetValueEx.
if (RegOpenKeyEx (HKEY_CURRENT_USER,
"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\RunOnce",0, KEY_SET_VALUE, &hk1) == ERROR_SUCCESS)
{
RegSetValueEx(hk1, // subkey handle
"", // value name
0, // must be zero
REG_SZ, // value type
(LPBYTE) &path, sizeof(DWORD)); // length of value data
RegCloseKey(hk1);
}
However my application does not start after a system restart.
There are a few methods:
Place your application in your start-up folder. This is a very easy method. When your system (PC) will be restarted, the application will get started (You need to login for this);
Use windows task planner;
Make the application an service.
I prefer the last option if it always needs to run. But you will need to add service handling.
You can create a task using Task Scheduler to run your application when the computer starts.
Open Task Scheduler by clicking the Start button , clicking Control Panel, clicking System and Security, clicking Administrative Tools, and then double-clicking Task Scheduler.‌ If you're prompted for an administrator password or confirmation, type the password or provide confirmation.
Click the Action menu, and then click Create Basic Task.
Type a name for the task and an optional description, and then click Next.
Click When the computer starts, and then click Next.
To schedule a program to start automatically, click Start a program, and then click Next.
Click Browse to find the program you want to start, and then click Next.
Select the Open the Properties dialog for this task when I click Finish check box and click Finish.
In the Properties dialog box, select Run whether user is logged on or not, and then click OK
Source:
Windows 7 - Schedule a task
PS: You must be logged on as an administrator to perform these steps
There are a number of things to keep in mind when using the solution you opted for:
The application does not start when the system starts but rather when the current user logs on.
If you write to the RunOnce key the operation will be performed only once. If you want your application to always start when the user logs on you should instead use the Run key.
In addition to the above, if you want to create a value you will have to give it a name. From the documentation of the lpValueName parameter for RegSetValueEx:
If lpValueName is NULL or an empty string, "", the function sets the type and data for the key's unnamed or default value.
The default (unnamed) value is the one that shows up as (Default) when using regedit. To get this to work you will have to provide a name for the value. This should be unique so that it does not conflict with other values under that key.
On a less technical note, implementing an auto-start feature for an application should only be done after thorough consideration.
You are passing the wrong parameter values to RegSetValueEx(). You need to use it like this instead:
TCHAR path[MAX_PATH+1] = {0}
GetModuleFileName(NULL, path, MAX_PATH);
if (RegOpenKeyEx(HKEY_CURRENT_USER, TEXT("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\RunOnce"), 0, KEY_SET_VALUE, &hk1) == ERROR_SUCCESS)
{
RegSetValueEx(hk1, // subkey handle
TEXT("MyApp"), // value name
0, // must be zero
REG_SZ, // value type
(LPBYTE) path,
(lstrlen(path)+1) * sizeof(TCHAR)); // length of value data, in bytes
RegCloseKey(hk1);
}

When Skype ICallChannelManagerEvents gets fired

I am working on App to App communication using skype. My Requirement is When one skype user place Call/Video Call I wanted to use application stream to send message from One App Plugged in Skype to other App plugged in Skype.
In Separate Sample App I am able to send and receive message using Application Stream from One App to Other App but I wanted to Activate Application Stream When User place call.
Skype4COM expose these three event for ICallChannelManager
ICallChannelManagerEvents::Channels
ICallChannelManagerEvents::Created
ICallChannelManagerEvents::Message
I have registered these three events
hr = m_pCallChannelMgr.CreateInstance(__uuidof(CallChannelManager));
hr = SinkSkypeCallChannelMgrEvents::DispEventAdvise(m_pCallChannelMgr);
hr = m_pCallChannelMgr->CreateApplication(L"");
VARIANT_BOOL flag = m_pCallChannelMgr->GetCreated();
while(true )
{
if ( VARIANT_TRUE == flag) break;
flag = m_pCallChannelMgr->GetCreated();
Sleep(1000);
}
hr = m_pCallChannelMgr->Connect(m_Skypeptr);
when m_pCallChannelMgr->CreateApplication(); is called it fires ICallChannelManagerEvents::Created event.
I am Not sure about,When Other when two event ICallChannelManagerEvents::Channels and ICallChannelManagerEvents::Message gets fired.
Plz help me on this.
Problem Resolved when there is already a call in process and your plugin starts to hook in to Skype ICallChannelManagerEvents gets fired.