Solved

CreateFile returns ERROR_INVALID_HANDLE

Posted on 1998-07-16
4
1,673 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

This article shows how to make a Windows 7 gadget that extends its U/I with a flyout panel -- a window that pops out next to the gadget.  The example gadget shows several additional techniques:  How to automatically resize a gadget or flyout panel t…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
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…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

790 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