Solved

CreateFile returns ERROR_INVALID_HANDLE

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone 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

This article surveys and compares options for encoding and decoding base64 data.  It includes source code in C++ as well as examples of how to use standard Windows API functions for these tasks. We'll look at the algorithms — how encoding and decodi…
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…
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…

696 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