Solved

NTFS file compression

Posted on 1997-11-11
8
499 Views
Last Modified: 2008-03-04
I need to compress a file on an NTFS volume using NT's compression attributes. It can't be done by changing the compressed attribute (2048). I've found API calls CreateFile and DeviceIoControl can do the trick, but I cant get them to work correctly.
0
Comment
Question by:pokrakam
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
8 Comments
 
LVL 8

Expert Comment

by:MikeP090797
ID: 1440461
Which one doesn't work?
0
 

Author Comment

by:pokrakam
ID: 1440462
I got this line of C code which apparently does the trick:
bRet = DeviceIoControl(hFile, FSCTL_SET_COMPRESSION, &uMode, sizeof(uMode), NULL, 0, &dwAttributes, NULL)
Can't getit to work with VB as:
nRet = DeviceIoControl(nHandle, &HD0003, nMode, 4&, 0&, 4&, 0&, 0&)
Tried various combinations of parameters.
0
 
LVL 8

Expert Comment

by:MikeP090797
ID: 1440463
Maybe just write a dll and call it from vb
0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 6

Expert Comment

by:alamo
ID: 1440464
That's not a very good answer - with a few rare exceptions if it can be done from a DLL it can be done from VB.

pokrakam, I just figured out how to do it - I'll post the code as soon as I can post it as an answer (i.e. as soon as you reject the current one). What you had is close, who knows, maybe by the time you read this you might have figured it out. (If so, please give me a chance to post the answer anyway and get the points, since I spent a lot of time figuring this one out). Thanks!
0
 

Author Comment

by:pokrakam
ID: 1440465
alamo's comment was about the most useful answer thus far, so I'll reopen the question.
0
 
LVL 6

Accepted Solution

by:
alamo earned 150 total points
ID: 1440466
Thanks pokrakam. For completeness I'll include the declares and definitions I am using, even though you probably have most of them already.

Private Declare Function DeviceIoControl Lib "kernel32" (ByVal hDevice As Long, ByVal dwIoControlCode As Long, lpInBuffer As Any, ByVal nInBufferSize As Long, lpOutBuffer As Any, ByVal nOutBufferSize As Long, lpBytesReturned As Long, lpOverlapped As Any) As Long ' note lpOverlapped as Any so can use null
Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, lpSecurityAttributes As Any, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Any) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long

Const FILE_READ_DATA = &H1
Const FILE_WRITE_DATA = &H2
Const OPEN_EXISTING = 3
Const FILE_SHARE_READ = &H1
Const FILE_SHARE_WRITE = &H2
Const FSCTL_SET_COMPRESSION As Long = &H9C040 ' This was the tricky one to figure out
Const COMPRESSION_FORMAT_NONE = 0
Const COMPRESSION_FORMAT_DEFAULT = 1

Dim nHandle As Long
Dim nBytesReturned As Long
Dim nMode As Long

nHandle = CreateFile("d:\path\file", FILE_READ_DATA Or FILE_WRITE_DATA, FILE_SHARE_READ Or FILE_SHARE_WRITE, ByVal 0&, OPEN_EXISTING, 0, ByVal 0&)
If Err.LastDllError <> 0 Then GoTo ShowErr ' error handler

nMode = COMPRESSION_FORMAT_DEFAULT
nRet = DeviceIoControl(nHandle, FSCTL_SET_COMPRESSION, nMode, Len(nMode), ByVal 0&, 0&, nBytesReturned, ByVal 0&)
If nRet = 0 Then GoTo ShowErr ' error handler
CloseHandle (nHandle)

Leave a comment if you have any problems with this, but I suspect  it'll work fine for you. Good luck!
0
 

Author Comment

by:pokrakam
ID: 1440467
Sorry about delay - 'puter got trashed, back online now.
All workes fine - I'd love to know where you got
FSCTL_SET_COMPRESSION As Long = &H9C040
from???

0
 
LVL 6

Expert Comment

by:alamo
ID: 1440468
I computed FSCTL_SET_COMPRESSION from the header file definitions. I did however, compute it the first time incorrectly (as &HD0003, same as you). Having seen &HD0003 in your post I didn't question my initial computation and  wasted 2-3 hours trying things. Then I went back and double-checked and found that in:

#define CTL_CODE( DeviceType, Function, Method, Access )
(((DeviceType) << 16) | ((Access) << 14) | ((Function) << 2) | Method))

the order of the arguments and the order they are used are different, the middle two are swapped. If you don't notice they are swapped you get &HD003. When you do eventually notice you get
the right answer... oh well.
0

Featured Post

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

690 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question