How to launch an external application on BN_CLICKED? - c++

I'm fairly new to Windows programming. I'm doing a simple launcher app for WinCE using VC++ (not MFC). So far I've created the basic interface and buttons and stuff. I just wanted to know the best way to launch an external application when the user clicks the button (on BN_CLICKED).
I found some methods such as ShellExecute, CreateProcess and others. But I couldn't get it to work (yet?). Any suitable reference or simple example on this?

The question don't matter that it happens inside the event of a button click, but...
ShellExecute is a good way to start programs (and the default program for any other type of files) in Windows, but use CreateProcess if you need a return code, or if you need the ability to wait for the program to finish.

Nevermind. I found a working example on MSDN - a comment by a user.
For anyone interested, you can go to this CreateProcess() article and scroll down to a comment entitled "Working code for creating two processes "p1.exe" and "p2.exe" using CreateProcess() Method"
Thanks!

Related

Windows 10 detect keyboard layout change

I'm trying to implement a service that would monitor language/layout changes. I switch between English and Russian languages. So far I've found this question and tried to implement and install both sinks suggested there.
However, there are problems. ITfActiveLanguageProfileNotifySink::OnActivated is not triggered at all; ITfLanguageProfileNotifySink::OnLanguageChange is not triggered, too; ITfLanguageProfileNotifySink::OnLanguageChanged is triggered only when the main window of my program is in foreground, but it doesn't contain any information on the language. Is there any way to monitor input language change event globally?
I found another way to detect such changes: to use SetWindowsHookEx with WH_SHELL. One of the available event is HSHELL_LANGUAGE which is exactly what I need and the test project seems to work just fine.
There's an article by Alexander Shestakov that describes a program very similar to what I'm trying to achieve and it also has a link to github project that I use as an example.

Is it possible for a DLL to open explorer?

Im trying to make (what I thought was a simple) extension for Game maker studio 2.
I am restricted to making a DLL app.
I am wondering is there any was to have a dll app open the file explorer have the user locate a file and then return said directory?
I fell like this is a sumb question but one I really need to know the answer too before slaving away coding for hours only to find its not possible.
You do not want to launch the explorer but to open a file dialog that allows the user to select a file.
Depending on the framework you use in your program the solutions may differ.
If you are using Qt framework you may use a QFileDialog for a platform independent mechanism.
If you are okay that it will only works on Windows then you may directly use the WinAPI functions GetOpenFileName or GetSaveFileName (that is a lot easier than the Common Item Dialog that is suggested as replacement on their documentation pages)
On GameMaker terms, you want to use get_open_filename or get_open_filename_ext.
See Dialog Module (marketplace, github) for C++ implementation reference.

Replacing std::cout output with windows form

I made a program in c++ (visual studio 2010) that looks for serial com ports and compares their friendly names with defined text. When there is a match that port is opened/connected and serial communication starts.
The program notifies the user when a com port is found, whether the connection was successful or not and if the data send was successful or not and other useful information. The program uses cout to notify the user.
I want to replace console output window with windows form but cant find much resources online on how to do this. To illustrate, I want this:
To become this:
I included form1.h and other files and tried replacing cout with below line but code is not compiling:
Form1::textBox1->Text = L" Text I want to display";
Can anyone explain how to use textBox1, or a tutorial for this?
"I included form1.h" - You can't just grab random files and hope it works. That is not how C++ works, or computers in general.
How then do you do something like this? The Standard Library provides std::cout and Visual Studio by default includes the Standard Library, so using it is fairly easy. But for graphics, you will need another library. I recommend Qt, if only because there are good tutorials for beginners.
So I finally did achieve the functionality I described above in my question and thought I should post my findings here.
To convert my code from console output to Windows Form I basically had to migrate from c++ to C++/cli.
holowczak.com has a great tutorial on how to get started with windows form (c++/cli) in visual studio.
Next if there is any busy looping(like infinite while-loop) in your c++ code then you would need to run that busy loop on a separate thread or the program can hang. Dr.Dobb's tutorial on how to create and mange threads in c++/cli, can help a lot.
Finally, if you need to access resources (such as textbox and other controls) of windows form from another thread then a thread-safe call has to be made. Microsoft's "HowTo:Make thread-safe call to windows Form controls" explains how the invoke method can be used for updating textbox from another thread.

How to Redirect to Another Application in C++?

I've researched it all over StackOverflow and Google but only get results about C# and irrelevant topics, so now I have to ask. I want after 20 seconds of the application being open to close itself and open another application that is in the same folder. After I have debugged, I copy the application to the folder on my Desktop that I will eventually ZIP up. But I want the app to open another application in the folder. Say for instance ApplicationA.exe Opens and after 20 seconds it closes and ApplicationB.exe opens.
You can use the CreateProcess() winapi function which acts like fork()+exec() and then exit() the ApplicationA. Also you can use Sleep(miliseconds) for the delay.
Here is the information about CreateProcess():
http://msdn.microsoft.com/en-us/library/windows/desktop/ms682425(v=vs.85).aspx
You can open it with system("ApplicationB.exe"); and then exit. This will start ApplicationB.exe and exit ApplicationA.exe.
Check out: http://www.dreamincode.net/forums/topic/18057-run-external-executable/
I believe C++ is not really the best way to go for system calls like the ones you need to do here. Maybe shell scripting would be a better option.
Anyway, the easiest way is probably writing a third program that opens ApplicationA.exe, closes it and starts ApplicaionB.exe.
I believe in C++ you'd have to use system, which you find in <cstdlib>. Yes, it is a C feature, not a C++ one, and its use is not encouraged, to put it mildly.
I would recommend using the ShellExecute function. http://msdn.microsoft.com/en-us/library/windows/desktop/bb762153(v=vs.85).aspx

Block alt+shift event or disable changing language in programming way

I need to block alt+shift keys event using C++, or some way to block changing language.
Thanks in advance.
I would go about it a bit differently. I would catch current langauge settings at startup than change it for desired one.
At the even of alt+shift i would just set it back to the desired type again.
It should be fairy easy to do with net framework.
Here is a short article about manipulating languages: How to change input language programmatically
And main class on msdna: InputLanguage documentation
To actually prevent alt+shift from moving into windows system you would have to play with hooks.
Here is an article about blocking keystrokes before windows process them.
Just an idea. You can catch WM_KEYDOWN message and call ActivateKeyboardLayout to switch language.
Using C++ you can install a keyboard hook procedure like the one suggested here and filter (swallow/don't propagate) the key(s) you want to forbid.
My understanding of MSDN is that you can pretend to process WM_INPUTLANGCHANGEREQUEST and then do nothing, so that Windows will not do anything further and the language will not actually change. But some users say that doesn't work any more.
http://msdn.microsoft.com/en-us/library/ms632630(VS.85).aspx
Maybe you can implement ITfInputProcessorProfileActivationSink::OnActivated, and when you get called you can change back to the previous language by calling ITfInputProcessorProfiles::ActivateLanguageProfile. At the beginning of your app you would call ITfInputProcessorProfiles::GetActiveLanguageProfile.
Maybe you can implement ITfLanguageProfileNotifySink::OnLanguageChange, set *pfAccept
to FALSE and return S_OK.
http://msdn.microsoft.com/en-us/library/windows/desktop/ms628770(v=vs.85).aspx
All of the above have a problem. If the user intentionally changes languages on the client, for example by clicking on the client's task bar instead of pressing Alt+Shift, the above methods will prevent their change anyway.
I wonder if RegisterHotKey would let you register Alt+Shift for your own window even though the system already had it defined.
The thing you are trying to implement is keyboard hook. The detailed explanation with source code in C/C++ can be found here:
http://www.codeproject.com/Articles/67091/Mouse-and-KeyBoard-Hooking-utility-with-VC
Also other helpful examples can be found here:
http://www.codeproject.com/Articles/1264/KeyBoard-Hooks
http://www.codeproject.com/Articles/9513/Disable-keyboard-and-show-images-for-the-children
Hope this helps.
Kind regards,
Bo