Solved

CreateFile fails in VS2005

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
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.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

760 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now