PlaySound works in Visual Studio but not in standalone exe - c++

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.

Related

SW_HIDE doesn't hide my second application C++

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.

How are file/directory paths inferred from within an executable in windows?

When we specify the path to a file or an executable in C++ source code how does it get resolved when the object files for the source code is packaged into an executable?
For example if I were to use _popen in my program and wanted to run another executable how would I provide the path to that executable.
Here's what I tried:
Put the external executable in the same directory as the source
code: works as expected in debug mode within visual studio.
Put the external executable inside the Debug / Release directory of
the project, then the executable which I am building will not see
the external executable I am trying to invoke using _popen.
In java world, I could infer a jar file as another file system and I could read the paths just like how I would do on any other file system.
Sorry if the question is a bit crude. I have just jumped into windows application development using visual C++ from being a java/ruby web developer. So I am not even able to come up with the right keywords to search on the internet.
Hope this compiles, it is close:
TCHAR szExePath[MAX_PATH];
GetModuleFileName(0, szExePath, _countof(szExePath)); // 0 for handle is the process handle shortcut for this API
TCHAR szDrive[MAX_PATH] szDir[MAX_PATH]
_tsplitpath(szExePath, szDrive, szDir, NULL, NULL);
TCHAR szNewExePath;
_tmakepath(szNewExePath, szDrive, szDir, _T("numerodos"), _T(".exe")); // change numerodos do something real
TCHAR flags = _T("wt"); // or whatever
_popen(szNewExePath, flags);
// really, on Windows I'd probably use CreateProcess

Playsound() in C++ playing windows error sound, I have included winmm.lib

I'm trying to use Playsound() in C++.
I've looked at everything but I always get the default windows error sound.
PlaySound(TEXT("D:\\Pokemon Game Stuff\\Programming\\Tests\\city simulator\\Sound.wav"), NULL, SND_FILENAME | SND_ASYNC);
winmm.lib has been set up in linker settings.
Full location and just filename has been tested.
I'm Using Code::Blocks and compiling with C++ 14 standards.
Per the PlaySound() documentation:
SND_FILENAME
The pszSound parameter is a file name. If the file cannot be found, the function plays the default sound unless the SND_NODEFAULT flag is set.
...
PlaySound searches the following directories for sound files: the current directory; the Windows directory; the Windows system directory; directories listed in the PATH environment variable; and the list of directories mapped in a network. If the function cannot find the specified sound and the SND_NODEFAULT flag is not specified, PlaySound uses the default system event sound instead. If the function can find neither the system default entry nor the default sound, it makes no sound and returns FALSE.
Double check that the file actually exists at the path you are specifying, and that the user that is running your app has permissions to access the file.

VC++ :Having trouble with opening a file inside a folder using fopen() in

I'm having trouble opening a file for reading, but only when it is inside a folder. I'm using Visual Studio 2010, and placing the folders with the files inside the "Debug" folder with the .exe, and running it just by clicking the .exe (not from within VS).
The code that is failing to work is:
FILE* FileHandle = fopen("./maps/1.area", "r");
if(FileHandle == NULL) {
return false;
}
FileHandle is always coming up as NULL. The code works fine if I move 1.area to the same folder as the exe, but if I move it down a level, it doesn't work. This same filepath style works when I use an SDL method to open a bitmap, so I can't figure out why it's failing with fopen.
Yeah, buddy, I'm goin' with this: fopen() is part of the C standard library, not the "SDL standard library". Though SDL may be permissive with slash styles, I'm betting fopen() is not.

In WinCE, CreateFile function: File open failed

I need to do file operations in WinCE in a Windows mobile 6 project in visual studio 2008.
For opening a file i did like below.
HANDLE hFile = CreateFile(__TEXT("\\1.txt"), GENERIC_READ ,
FILE_SHARE_READ, NULL, OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL, NULL);
But hFile is coming as 0xffffffff. File open failed.
File exists in d:\
I tried paths like d:\\1.txt , 1.txt
But none of them are working.
Filename path should be relative to which directory ?
Kindly help me.
EDIT: The problem is it is trying to search the path in WinCE emulator.
But my file is present in System harddisk d:\
So i am unable to access files present in my computer when using WinCE.
Now the question changes to, How to copy files in Computer to Emulator ?
My guess is that your file is not there.
In order to share a folder from your harddisk with the emulator, go to your emulator menu:
"File -> Configure ... -> General -> Shared folder:". This folder will be seen as "Storage Card".
Lets say you map d:\wm, then you put 1.txt in d:\wm, afterwards the file path for ::CreateFile becomes: L"\\storage card\\1.txt".
Trying to guess at the problem isn't useful. Ask Windows what was wrong, use GetLastError(). Lookup the error code in WinUser.h or use FormatMessage().
Wny you are not using Boost Filesystem library?
It is very simple and good for all kind of works with files and directories.
http://www.boost.org/doc/libs/1_44_0/libs/filesystem/v2/doc/index.htm