Solved

CreateFile fails in VS2005

Posted on 2006-07-23
4
450 Views
Last Modified: 2013-11-20
If I create a test MFC dialog app. in VS2005 and in the OnInitDialog() generated code I add the following code just before the return:
DWORD d;
HANDLE h;
SetLastError(0);
h = CreateFile((LPCTSTR)"C:\\document.rtf",FILE_READ_EA,FILE_SHARE_READ,NULL,OPEN_EXISTING,NULL,NULL);
d = GetLastError();

CreateFile fails with d=2.  If I create a the same app. in VC++6 it works fine.  What gives?
0
Comment
Question by:Taurus
  • 2
4 Comments
 
LVL 8

Expert Comment

by:manish_regmi
ID: 17162555
error 2 means: The system cannot find the file specified.

It means the file does not exist.

Also you don't need a type cast.

regards
Manish Regmi
0
 
LVL 8

Expert Comment

by:manish_regmi
ID: 17162562
if you want to create new if the file does not exist use

OPEN_ALWAYS

CreateFile(path, // drive to open
                       GENERIC_READ | GENERIC_WRITE,    
                       FILE_SHARE_READ | FILE_SHARE_WRITE,  // share mode
                       NULL,    // default security attributes
                       OPEN_ALWAYS,  // disposition
                       0,       // file attributes
                       NULL);   // don't copy any file's attributes

regards
Manish Regmi
0
 
LVL 48

Accepted Solution

by:
AlexFM earned 125 total points
ID: 17162775
h = CreateFile(_T("C:\\document.rtf"),FILE_READ_EA,FILE_SHARE_READ,NULL,OPEN_EXISTING,NULL,NULL);

Add _T macro. VC++ creates Unicode application by default, your ANSI string is casted to Unicode string, which gives incorrect result. This also shows that using type casting without reason is dangerous: instead of compilation error you have runtime error.
0
 

Author Comment

by:Taurus
ID: 17189276
I had forgotten about _T, and you are absolutely correct about using typecasting without reason.  Thanks.
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
cat dog challenge 18 126
Doc'in system (example?) BA 7 115
centeredAverage challenge 8 179
Window placement 17 86
This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question