Solved

NTFS file compression

Posted on 1997-11-11
8
489 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
  • 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
 
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

760 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now