By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,678 Members | 1,146 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,678 IT Pros & Developers. It's quick & easy.

DeviceIoControl to compress files

P: n/a
Everyone,

greetings, newbee here. What I'm trying to do is to compress files on NTFS
from C#.
I've been fishing around for info and gathered the hereafter. I'd like to
know what's wrong with it.

[DllImport("kernel32.dll")]
public static extern int DeviceIoControl(
IntPtr hDevice,
[MarshalAs(UnmanagedType.U4)] int dwIoControlCode,
IntPtr lpInBuffer,
[MarshalAs(UnmanagedType.U4)] int nInBufferSize,
IntPtr lpOutBuffer,
[MarshalAs(UnmanagedType.U4)] int nOutBufferSize,
[MarshalAs(UnmanagedType.U4)] ref int lpBytesReturned,
IntPtr lpOverlapped);

private static int FSCTL_SET_COMPRESSION = 0x9C040;
private static IntPtr COMPRESSION_FORMAT_DEFAULT = new IntPtr(1);

FileStream f = File.Open(fileName, System.IO.FileMode.Open,
System.IO.FileAccess.ReadWrite, System.IO.FileShare.None)

int result = DeviceIoControl(f.Handle, FSCTL_SET_COMPRESSION,
COMPRESSION_FORMAT_DEFAULT, IntPtr.Size, IntPtr.Zero, 0, ref
lpBytesReturned, IntPtr.Zero);

It compiles, it even runs, but the file is not compressed, and result = 0.
Any ideas? Many thanks,
Jules
Nov 13 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Jules,

You're currently passing a pointer with the value 1 to the lpInBuffer
parameter. If I read the docs right, you should be passing a pointer
to a short containing the value 1 instead. So try to change the
declaration to

[DllImport("kernel32.dll")]
public static extern int DeviceIoControl(IntPtr hDevice, int
dwIoControlCode, ref short lpInBuffer, int nInBufferSize, IntPtr
lpOutBuffer, int nOutBufferSize, ref int lpBytesReturned, IntPtr
lpOverlapped);

And theen call it like this

short COMPRESSION_FORMAT_DEFAULT = 1;

int result = DeviceIoControl(f.Handle, FSCTL_SET_COMPRESSION,
ref COMPRESSION_FORMAT_DEFAULT, 2 /*sizeof(short)*/, IntPtr.Zero, 0,
ref lpBytesReturned, IntPtr.Zero);

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.
Nov 13 '05 #2

P: n/a
Hi Mattias.

many thanks for the answer: it works.
Cheers,
Jules

"Mattias Sjögren" <ma********************@mvps.org> wrote in message
news:uk**************@TK2MSFTNGP12.phx.gbl...
Jules,

You're currently passing a pointer with the value 1 to the lpInBuffer
parameter. If I read the docs right, you should be passing a pointer
to a short containing the value 1 instead. So try to change the
declaration to

[DllImport("kernel32.dll")]
public static extern int DeviceIoControl(IntPtr hDevice, int
dwIoControlCode, ref short lpInBuffer, int nInBufferSize, IntPtr
lpOutBuffer, int nOutBufferSize, ref int lpBytesReturned, IntPtr
lpOverlapped);

And theen call it like this

short COMPRESSION_FORMAT_DEFAULT = 1;

int result = DeviceIoControl(f.Handle, FSCTL_SET_COMPRESSION,
ref COMPRESSION_FORMAT_DEFAULT, 2 /*sizeof(short)*/, IntPtr.Zero, 0,
ref lpBytesReturned, IntPtr.Zero);

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.

Nov 13 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.