In WinCE, CreateFile function: File open failed - c++

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

Related

PlaySound works in Visual Studio but not in standalone exe

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.

How to open and read files in a Windows Universal App using fopen

I'm writing a windows app (non-xaml) in c++ and trying to read files in my project directory using:
FILE* mFile = fopen(filename, "rb");
and I also tried:
errno_t err = fopen_s(&mFile, filename, "rb");
In this case, the errno is 2 and in both cases mFile is null afterwards.
I'm having trouble finding c++ code that explains how to simply open and read files.
The filename is such: "./aDirectory/anotherDirectory/thefile"
My solution directory looks like with a lot of things omitted:
AppName.Shared
AppName.Windows
aDirectory
anotherDirectory
thefile
AppName.WindowsPhone
I've seen something called a FilePicker in the API but it looks a little excessive and has more to do with UI than I need.
What is the appropriate way to include files into my project and then load them in Windows Runtime?
I found the fix. It turns out that my "directories" in my project structure were not actual directories, and that in Visual Studio they are "filters." So if I made the path "thefile" instead of ""./aDirectory/anotherDirectory/thefile," fopen would work.

Qt, windows cannot find file that Qt open file dialog can

So I have a Qt program that saves files among other things. I saved a few files in my C:/ directory. When I look for the file in windows explorer, cygwin, or my command prompt, I cannot find the file. I checked my folder options and those look fine. Despite not being able to find the file, when I need to load a file in my Qt program, the program is able to find the file. Only my program is able to find the file, windows cannot. I am logged in as an administrator, but could this possibly have something to do with permissions?
Also, it may be worth noting that when I save files in c:/users/me/Documents/folder/folder I don't seem to have an issue.
Code that saves file:
QString saveFileName = QFileDialog::getSaveFileName(this,"Select the file you wish to save to.","","");
QFile saveFile(saveFileName);
if(saveFile.open(QIODevice::WriteOnly))
{
QTextStream stream(&saveFile);
QString stringToSaveToFile;
stream << stringToSaveToFile;
saveFile.close();
}
else
{
QMessageBox::warning(this, "Error", "Cannot open file.");
}
Code that opens file:
QString selectedFile = QFileDialog::getOpenFileName(this, "Select a .pro file.", "", tr("Profile file (*.pro);;All (*.*)"));
QString fileContents;
QFile file(selectedFile);
if (file.open(QIODevice::ReadOnly) | (QIODevice::Text))
{
QTextStream in(&file);
fileContents = in.readAll();
}
else
{
QMessageBox::warning(this, "Error", "Unable to open file.");
}
Edit: Just tried this on a different computer. My computer has windows 8, the other computer had windows XP. I could not replicate the problem on the XP machine.
Edit:
I believe I have found what the issue is (http://answers.microsoft.com/en-us/windows/forum/windows_vista-files/windows-explorer-compatibility-files/5b377209-cfe4-4be6-959d-e1de4b8be16d), but I am still trying to find out how to resolve it.
The files I am trying to save to the c:/ directory are actually being saved in C:/users/username/AppData/Local/VirtualStore.
Is there a way to somehow override this?
For now my solution is to warn the user if they save in the C:/, C:/Program Files/, C:/Program Files (x86), or C:/Windows/ directories. All files saved in these directories actually get saved in C:/User/Current User/AppData/Local/VirtualStore/.
If anyone has a better solution let me know.
Even though you might be logged in as an administrator by default most programs are run in unelevated mode (basically, NOT as an administrator). Also by default unelevated programs do not have write access to the root directory on C: drive (same way they do not have permission to write in, for example, C:\Program Files\). This was not always the case - I do not remember exactly at the moment but I think write to program files was already protected in Windows XP, and C: became protected only afterwards, I think at least since Windows 7? Anyway, for compatibility reasons write to those protected directories is virtualized.
Try running your Qt application in elevated mode (right click -> Run as administrator, or right click -> Properties -> Compatibility -> Run this program as an administrator) if you want to write directly in C:\, but really, you shouldn't.
If you are really worried about users being confused by this, you can implement a check for write access - after user chooses location for the file, but before an actual write - and display a warning or let them choose another location.

Open file to display content in C++

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.

Opening a file with a certain program changes that programs working directory?

I have a file saved to my desktop, when I open it with my program the working directory changes to the desktop, this means my program can not load in some files it needs as it searches for these in the working directory. Is there a way I can stop the working directory from changing like this?
There's a flag you can set to avoid the current directory from changing called OFN_NOCHANGEDIR
http://msdn.microsoft.com/en-us/library/ms646839(v=vs.85).aspx
You can just save your working directory at startup and use absolute paths. In fact, it's better to always open files with absolute paths, unless you really want to rely on the current working directory.
You would be better off determining the processes location, then using it as the key for where to find the other files? There are many ways that programs can be launched, which effect the working directory.
See: The answer here for a good description of how to get the processes location and strip out the executable filename (look in the comments)
Essentially, you use:
GetModuleFileName or GetModuleFileNameEx.
and then:
PathRemoveFileSpec to remove the file name
Opening a file doesn't change your current directory. Perhaps you using the common open file dialog? Here is an article that will explain all about how that changes your current directory.
use SetCurrentDirectory to do that.
You can locate the executable by using GetModuleFileName
TCHAR szFileName[MAX_PATH];
GetModuleFileName( NULL, szFileName, MAX_PATH )
... then compute the correct directory
SetCurrentDirectory(path);