Solved

CreateFile fails in VS2005

Posted on 2006-07-23
4
452 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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
iSeries DB2 Query 2 98
Doc'in system (example?) BA 7 120
matchUp  challenge 9 125
ASP.NET C# MessageBox.Show Showing a modal dialog box or form when the application ... 2 184
Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

749 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