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.

  1. to open volume, must prefix drive letter \\.\.
  2. 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

Popular posts from this blog

python - How to create a legend for 3D bar in matplotlib? -

java - Multi-Label Document Classification -

php - Dynamic url re-writing using htaccess -