ReadFile using winapi with pinvoke (image a physical disk)


i'm trying read files powershell using winapi (createfile, readfile, closehandle). of course there exist easier ways read files, ultimetaly want read physical disk, that's why. familiar api's , have done many times in other script language. i'm learning powershell.. anyways, believe problem in creation of structure readfile put content. i've put getlasterror in there return code 5 (access denied). other api's seem work fine. appreciated. code;

$pinvoke = add-type -name pinvoke21 -passthru -memberdefinition @'    [dllimport("kernel32.dll", charset = charset.auto, callingconvention = callingconvention.stdcall, setlasterror = true)]    public static extern intptr createfile(          string lpfilename,          uint dwdesiredaccess,          uint dwsharemode,          intptr securityattributes,          uint dwcreationdisposition,          uint dwflagsandattributes          );            [dllimport("kernel32.dll", setlasterror = true)]  public static extern bool readfile(intptr hfile, [out] byte[] lpbuffer, uint nnumberofbytestoread, ref int lpnumberofbytesread, intptr lpoverlapped);         [dllimport("kernel32.dll", charset = charset.auto, setlasterror=true)]  public static extern bool closehandle(intptr hobject);    [dllimport("kernel32.dll", charset = charset.auto, setlasterror=true)]  public static extern uint getfilesize(intptr hfile, intptr lpfilesizehigh);    [dllimport("kernel32.dll", setlasterror = true)]  public static extern uint getlasterror();    '@    $lpfilename = "c:\tmp\dummytest.txt"  $dwdesiredaccess = 0x2  $dwsharemode = 0x6  $lpsecurityattributes = 0  $dwcreationdisposition = 0x4  $dwflagsandattributes = 0x0   $hfile = $pinvoke::createfile($lpfilename, $dwdesiredaccess, $dwsharemode, $lpsecurityattributes, $dwcreationdisposition, $dwflagsandattributes)  $hfile  $filesize = $pinvoke::getfilesize($hfile,0)  $filesize  add-type @"  public struct teststruct {  public byte filebuff;  }  "@  $filebuffer1 = new-object teststruct  $filebuffer = new-object $filebuffer1.filebuff  $rfile = $pinvoke::readfile($hfile, $filebuffer, $filesize, [ref]$nbytes, 0)  $pinvoke::getlasterror()  $rfile  $filebuffer  $pinvoke::closehandle($hfile)    

 

joakim



$pinvoke = add-type -name pinvoke -passthru -memberdefinition @'  [dllimport("kernel32.dll", charset = charset.auto, callingconvention = callingconvention.stdcall, setlasterror = true)]   public static extern intptr createfile(         string lpfilename,         uint dwdesiredaccess,         uint dwsharemode,         intptr securityattributes,         uint dwcreationdisposition,         uint dwflagsandattributes         );           [dllimport("kernel32.dll", charset = charset.auto, setlasterror=true)] public static extern bool closehandle(intptr hobject);  [dllimport("kernel32.dll", setlasterror = true)] public static extern uint getlasterror();  [dllimport("kernel32.dll", charset = charset.auto, setlasterror=true)] public static extern uint getfilesize(intptr hfile, intptr lpfilesizehigh);  [dllimport("kernel32.dll", setlasterror = true)] public static extern bool writefile(intptr hfile, byte [] lpbuffer, uint nnumberofbytestowrite, out uint lpnumberofbyteswritten, intptr lpoverlapped);  [dllimport("kernel32.dll", setlasterror = true)] public static extern bool readfile(intptr hfile, [out] byte[] lpbuffer, uint nnumberofbytestoread, ref int lpnumberofbytesread, intptr lpoverlapped); '@  $lpfilename = "f:\test.txt" $dwdesiredaccess = 2147483648 $dwsharemode = 0x1 $lpsecurityattributes = 0 $dwcreationdisposition = 3 $dwflagsandattributes = 0x0  $hfile = $pinvoke::createfile($lpfilename, $dwdesiredaccess, $dwsharemode, $lpsecurityattributes, $dwcreationdisposition, $dwflagsandattributes) $filesize = $pinvoke::getfilesize($hfile,0)  $buffer = new-object byte[] $filesize $read = [uint32]::minvalue $pinvoke::readfile($hfile,$buffer,$filesize,[ref]$read,0) [text.encoding]::ascii.getstring($buffer) 
test.txt contains
ps >  gc f:\test.txt hello! how you? 
output:
ps >  $buffer = new-object byte[] $filesize ps >  $read = [uint32]::minvalue ps >  $pinvoke::readfile($hfile,$buffer,$filesize,[ref]$read,0) true ps >  [text.encoding]::ascii.getstring($buffer) hello! how you? 





Windows Server  >  Windows PowerShell



Comments

Popular posts from this blog

Group Policy Event ID 1058 Error Code 1326 (The user name or password is incorrect)

Suspicious event log Event ID: 4905

DCOM received error "2147746132" from...