Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

CreateFile returns ERROR_INVALID_HANDLE

Posted on 1998-07-16
4
Medium Priority
?
1,691 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 400 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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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 shows how to make a Windows 7 gadget that accepts files dropped from the Windows Explorer.  It also illustrates how to give your gadget a non-rectangular shape and how to add some nifty visual effects to text displayed in a your gadget.…
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and 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…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

610 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