Solved

CreateFile returns ERROR_INVALID_HANDLE

Posted on 1998-07-16
4
1,665 Views
Last Modified: 2013-12-03
Hi,

under some circumstances we have a problem when
calling CreateFile(): the return value
is ERROR_INVALID_HANDLE  (6).

We guess in that case we have a environment problem,
but we don't know how to prove it.

The error only occurs if a relative path name was
given. No problem with absolute path names.

.................. Source ..........................
#include <windows.h>
#include <limits.h>

main(argc, argv)
int argc;
char **argv;
{
      extern int getchar();
      extern int printf();

      DWORD                  bufferlength;
      char                   buffer[MAX_PATH+1];
      LPTSTR                  lpbuf;

      HANDLE                  hPipe;
      DWORD                   err;
      SECURITY_ATTRIBUTES      sa;

      sa.nLength              = sizeof(sa);
      sa.bInheritHandle       = TRUE;
      sa.lpSecurityDescriptor = NULL;

      lpbuf = buffer;
       bufferlength = MAX_PATH;

      getchar();

      if (GetCurrentDirectory(bufferlength, buffer) == 0)
      {
        err = GetLastError();
        printf("LastError of GetCurrentDirectory() = %d\n", err);
      }
      else
        printf("CWD = %s\n", buffer);
      
      hPipe = CreateFile(argv[1],
            GENERIC_READ | GENERIC_WRITE,
            0,
            &sa,
            CREATE_ALWAYS,
            FILE_ATTRIBUTE_NORMAL,
            (HANDLE)NULL);

      if (hPipe == INVALID_HANDLE_VALUE)
      {
        err = GetLastError();
        printf("LastError of CreateFile() = %d\n", err);
      }

      return 0;
}

.............the transcript ..............................................

$ mytouch kkk                
                             
CWD = E:\CB\X.BASIC\0        
LastError of CreateFile() = 6            // ERROR_INVALID_HANDLE


$ mytouch E:\CB\X.BASIC\0\kkk
                             
CWD = E:\CB\X.BASIC\0        
                              // NO problem

$ mytouch kkk                
                             
CWD = E:\CB\X.BASIC\0        
LastError of CreateFile() = 6            // ERROR_INVALID_HANDLE  


$          

......................................

Can anybody tell me whats wrong ?


Thanks
0
Comment
Question by:moosach
  • 3
4 Comments
 
LVL 8

Accepted Solution

by:
trestan earned 100 total points
ID: 1410379
As you have known, CreateFile can create various kind of objects. So an absolute path should be given to create a physical file on disk. I think you can append the file input from the console to the end of the path you detected. Use the new string as filename in the CreateFile function.
0
 
LVL 8

Expert Comment

by:trestan
ID: 1410380
Add the following codes in your file. Use FileName in CreateFile function. Run program in dospromp window. Provide the filename to be created along with this program name. Or revise the program to allow input the file name in run time: scanf("Please Input File Name%s", &GetName); Change the argv[1] to GetName.

char FileName[MAX_PATH];
int j=0;

for (int i=0; i<=(int)strlen(buffer); i++)
{
      if((buffer[i])!='\\')
      {
            FileName[j]=buffer[i];
      }
      else
      {
            FileName[j]=buffer[i];
            j++;
            FileName[j]='\\';
      }
      j++;
}
strcat(FileName,"\\\\");
strcat(FileName, argv[1]);


0
 
LVL 8

Expert Comment

by:trestan
ID: 1410381
Also I would like to mention you that this method will work under Windows NT. In Windows 95, you have to create VxD to deal with disk.
0
 
LVL 1

Author Comment

by:moosach
ID: 1410382
Great !
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

With most software applications trying to cater to multiple user needs nowadays, the focus is to make them as configurable as possible. For e.g., when creating Silverlight applications which will connect to WCF services, the service end point usuall…
What my article will show is if you ever had to do processing to a listbox without being able to just select all the items in it. My software Visual Studio 2008 crystal report v11 My issue was I wanted to add crystal report to a form and show…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

759 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

17 Experts available now in Live!

Get 1:1 Help Now