Solved

CreateFile fails in VS2005

Posted on 2006-07-23
4
446 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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

Suggested Solutions

Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
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.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

809 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