Solved

CreateFile returns ERROR_INVALID_HANDLE

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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

In this article, I will show how to use the Ribbon IDs Tool Window to assign the built-in Office icons to a ribbon button.  This tool will help us to find the OfficeImageId that corresponds to our desired built-in Office icon. The tool is part of…
For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
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…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

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