c# - CreateFile in Kernel32.dll returns an invalid handle -
i'm trying create safe file handle "c:" using createfile method of kernel32.dll returns me invalid handle.
any on doing wrong here?"c:
createfile( lpfilename: "c:", dwdesiredaccess: fileaccess.readwrite, dwsharemode: fileshare.readwrite, lpsecurityattributes: intptr.zero, dwcreationdisposition: filemode.openorcreate, dwflagsandattributes: fileattributes.normal, htemplatefile: intptr.zero); [dllimport("kernel32.dll", setlasterror = true, charset = charset.auto)] public static extern safefilehandle createfile( string lpfilename, [marshalas(unmanagedtype.u4)] fileaccess dwdesiredaccess, [marshalas(unmanagedtype.u4)] fileshare dwsharemode, intptr lpsecurityattributes, [marshalas(unmanagedtype.u4)] filemode dwcreationdisposition, [marshalas(unmanagedtype.u4)] fileattributes dwflagsandattributes, intptr htemplatefile);
there couple of parameters not quite right.
- to open volume, must prefix drive letter
\\.\. - you can open volume read privileges.
try code:
safefilehandle handle = createfile( lpfilename: @"\\.\c:", dwdesiredaccess: fileaccess.read, dwsharemode: fileshare.readwrite, lpsecurityattributes: intptr.zero, dwcreationdisposition: filemode.openorcreate, dwflagsandattributes: fileattributes.normal, htemplatefile: intptr.zero ); note open volume handle read privileges, must running administrator, otherwise access denied (error code 5). nik bougalis , createfile documentation points out, if specify dwdesiredaccess 0 administrator privileges not required.
if parameter zero, application can query metadata such file, directory, or device attributes without accessing file or device, if generic_read access have been denied.
Comments
Post a Comment