Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Interop / November 2005

Tip: Looking for answers? Try searching our database.

Issue with WriteEncryptedFileRaw API

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
MVB - 08 Nov 2005 22:19 GMT
Hi,

I am writing a code to restore encrypted files using c#.NET
All my functions are successful.But in the target the file shows size 0KB.
i.e.,even if ReadFile API reads all the bytes,they are not written using the
call back function.Can someone please suggest me what am I doing wrong?
Below is my code.

private delegate int ImportCallBack(IntPtr pbdata,IntPtr
pvCallbackContext,int ulLength);

[DllImport("advapi32.dll", SetLastError=true) ]
private extern static int
WriteEncryptedFileRaw([MarshalAs(UnmanagedType.FunctionPtr)] ImportCallBack
pfImportCallback,
IntPtr pvCallbackContext,IntPtr pvContext);

[DllImport("advapi32.dll", CharSet=CharSet.Auto,SetLastError=true) ]
public extern static int
OpenEncryptedFileRaw([MarshalAs(UnmanagedType.LPTStr)]
string lpFileName,int ulFlags,out IntPtr pvContext);

[DllImport("advapi32.dll", SetLastError=true) ]
public extern static void CloseEncryptedFileRaw(IntPtr pvContext);

[DllImport("kernel32.dll", SetLastError=true)]
static extern IntPtr CreateFile(string lpFileName, uint dwDesiredAccess,
uint dwShareMode, uint lpSecurityAttributes,uint dwCreationDisposition, uint
dwFlagsAndAttributes, uint hTemplateFile);

[DllImport("kernel32.dll", CharSet=CharSet.Auto)]
public extern static bool CloseHandle(IntPtr handle);

public static void start()
{
    string sSourceFile = @"D:\errr.txt"; //Encrypted file to be restored
    string  sTargetFile = @"\\Server1\c$\errr1.txt";

   hRawFile = CreateFile(sSourceFile,GENERIC_READ, 0, 0,
OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0);
if(hRawFile.ToInt32() == INVALID_HANDLE_VALUE)
{
   Console.WriteLine("Error Opening File : " + sSourceFile );
   CloseEncryptedFileRaw(pvContext);
   return;
}
if(OpenEncryptedFileRaw(sTargetFile,CREATE_FOR_IMPORT,out pvContext)!= 0)
{
        Console.WriteLine("Open File Failed with {0}",
Marshal.GetLastWin32Error());
        Console.WriteLine("Error Opening File : " + sTargetFile);
         return;
}
           
ImportCallBack Imcb = new ImportCallBack(WriteCallBack);
int i = WriteEncryptedFileRaw(Imcb,hRawFile,pvContext);
CloseEncryptedFileRaw(pvContext);
CloseHandle(hRawFile);

}

public static int WriteCallBack(IntPtr pbdata, IntPtr pvCallbackContext ,
int ulLength)
{
if(!doneonce)
{
buffdata = new byte[ulLength];
Marshal.Copy(pbdata, buffdata, 0, 8600);
}
IntPtr phrawFile = pvCallbackContext;
int i = ReadFile(phrawFile, buffdata,ulLength,ref nBytesRead,IntPtr.Zero);
if(i == 0)
{
Console.WriteLine("Read File Failed with {0}", Marshal.GetLastWin32Error());
}
ulLength = nBytesRead;
nTotalBytes+=nBytesRead;

Console.WriteLine("Total Bytes Read are : " + nTotalBytes);
doneonce = true;
return 0;
}
Mattias Sjögren - 09 Nov 2005 09:36 GMT
> private delegate int ImportCallBack(IntPtr pbdata,IntPtr
> pvCallbackContext,int ulLength);

ulLength should be a ref parameter.

Mattias

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.