I'm trying to open an .exe file with ShellExecuteA() and then hide it. I managed to open the file, but SW_HIDE doesn't work on it, but when I try to do the same thing with notepad.exe, it works.
ShellExecuteA(0, "open", "myapp.exe", 0, 0, SW_HIDE);
This opens my application, but doesn't hide it.
I tried putting myapp.exe in System32, same as where notepad.exe is located so I don't need to use any path beside the app name and .exe extension, but that also didn't work.
Related
I am trying to play a wav file in C++ using Visual Studio.
I put file "my.wav" in my project directory and use the code
PlaySound(TEXT("my.wav"), NULL, SND_FILENAME | SND_SYNC);
I hit the play button (or F5 or even Ctrl-F5) and it plays the sound fine.
I open a command prompt and go to Debug/ and run MyApp.exe and when it runs it plays the error chime.
Note: Ideally the sound would be bundled in the exe so I can just distribute the exe and it would work. I tried putting it in an Resource.rc but the code I see in all the examples
PlaySound( (char*)IDR_WAVE1, NULL, SND_RESOURCE | SND_SYNC );
doesn't even compile for me. Complains about IDR_WAVE1 even though that is the name of my resource.
I'm a little rusty on old school win32 but it was something like this:
include resource.h in your file and use MAKEINTRESOURCE
PlaySound(MAKEINTRESOURCE(IDR_WAVE1), NULL, SND_RESOURCE | SND_SYNC );
As I recall it you need to 'link' the resource file with a resource script file ".rc file" in Visual Studio to embed it inside the .exe file. Otherwise you need to load it like #wilx points out.
I am using visual studio 2010 to develop a windows form application using c++.
I want to open a text file without the user browsing through the directory to select the required file, that is without using OpenFileDialog tool.
In short
If the user click a button named "Open File" in the form, a file should open rather than a Dialog
The ShellExecute function can be used to do what you're trying to do.
For example, to open a Word document at the location C:\SomeDirectory\MyWord.docx, you could call the following function:
ShellExecute(NULL, L"open", L"C:\\SomeDirectory\\MyWord.docx", NULL, NULL, SW_SHOW);
This will attempt to open the file using the default application associated with its file extension (e.g., Microsoft Word, in the above example; possibly Notepad.exe for text files; etc.).
system( "file path" ) helped me to open a excel application, but then, I lost control from the MFC exe as the cmd.exe holds the control ( with the excel openend). Can anyone please help me in closing the cmd exe alone, and getting the control back to my MFC application. I would also want the openend excel to stay, displayed
Instead of opening the cmd.exe and launching the excel application along with the file path, you can use the ShellExecute API to does it easier.
Here is the example:
ShellExecute(NULL, L"open", L"excel", L"your_file_path", NULL, SW_SHOWNORMAL);
As noted in a commendt, you can use system("start excel \"file path\") that doesn't let the command interpreter to wait for excel to return.
But you can better not use system, and refer to a more congruent process control API, like CreateProcess to call excel directly from your app.
This will prevent some malware to place an excel.cmd file in you path to do what he wants with your application.
I have 2 questions to ask regarding opening files (any kind of files) using C++. I am currently working on a GUI program and I want to add a changelog in txt form. Also I want a menu in my program to open that changelog.txt with the default text editor every user has installed or to simply put it to open that text file. Please keep in mind that I want to open the file for display NOT in the program for input/output.I know I can do that using
system("notepad.exe filepath.txt");
or to open them with the preset program:
system("filepath.txt");
The problem is that both of those open a command line behind the notepad. I know there is another command to open files using Win32 API called CreateProccess() but my compiler doesn't recognise that command (OpenWatcom W32).
So here are my questions:
1) Is there any other command to open files or is there a way to stop the command line from opening when using system command?
2) How do you define in Windows that the text file is in the current program folder? I mean instead of giving the entire filepath which will change from user to user is there any way to "tell" the program that the file is always on the current folder that the program is in?
I am sorry for any errors, if you want any clarification please let me know.
CreateProcess would be the wrong function to use here. That would require you to decide which process to run. The user may prefer to use a text editor other than Notepad, I know I do! The right way to do this on Windows is to ask the shell to open the file with whatever program the user has associated with the file. The ShellExecute function does this.
Call it like this:
ShellExecute(
MainWindowHandle,
"open",
FullyQualifiedTextFileName,
NULL,
NULL,
SW_SHOWNORMAL
);
You will need to include the Shellapi.h header file and link to the Shell32.lib library. If your compiler does not include these files, and I would be surprised if that was the case, then you can get them from the Platform SDK. That said, if you are serious about programming on Windows you should get hold of a tool that gives you access to the Windows API.
I do recommend that you use a fully qualified path for a task like this. Since your text file is located in the same directory as the executable you should simply join that directory to your text file's name. Get hold of the full path to the executable by calling GetModuleFileName passing NULL for the hModule parameter.
Preferably using C++. Or a tool I can use from the command line. So far I've figured out how to extract icons from .exe files, but I can't set icons... Any suggestions?
If you want an icon to show in Explorer in a directory list, you'll need to add an icon to your app's .rc file. It must be the first icon in the resource file.
Do you want an icon to appear in the task bar and have it associated with your app's window? In this case you must set the hIcon member in your registered WNDCLASS structure.
Here's a code sample that might help:
Change Icon of EXE file through code extracting it from other EXE file