GetAttr and SetAttr

M

Marco

WinXPpro, Office XP, Word VBA

I have written a small VBA Add-In that saves information
to a file. In order to prevent tampering, I have set the
file attribute to Read-Only.

Before I write, I use GetAttr to retrieve a Long, then
binary 'and not' it with vbReadOnly and then do a SetAttr
to remove just the read only flag.

On some PC's this does not work. After investigation I
found out that GetAttr is returning a value of 8225 (8192
+ 32 + 1) and SetAttr was trying to use 8224 (8192 + 32)
and terribly crashed the Add-In.

Nor the documentation nor the Internet cover a bitvalue of
8192 (2048 at most). I have no clue how to solve this.

Any help is absolutely welcome.
 
K

Karl E. Peterson

Hi Marco --
I have written a small VBA Add-In that saves information
to a file. In order to prevent tampering, I have set the
file attribute to Read-Only.

Before I write, I use GetAttr to retrieve a Long, then
binary 'and not' it with vbReadOnly and then do a SetAttr
to remove just the read only flag.

So far, so good.
On some PC's this does not work. After investigation I
found out that GetAttr is returning a value of 8225 (8192
+ 32 + 1) and SetAttr was trying to use 8224 (8192 + 32)
and terribly crashed the Add-In.

Yep, looks like SetAttr doesn't like values in that range. I think you'll need to go
to the API for this. You might be interested in something I put together some time
ago -- FileInfo.zip on http://www.mvps.org/vb/samples.htm -- for lots of file
"attribute" type stuff. But, in a nutshell, here's what you need to override VB's
own SetAttr function:

Private Declare Function SetFileAttributes Lib "kernel32" Alias
"SetFileAttributesA" (ByVal lpFileName As String, ByVal dwFileAttributes As Long) As
Long

Public Function SetAttr(FileName As String, ByVal Attributes As Long) As Boolean
' Nothing fancy, just set new attribute and return
SetAttr = SetFileAttributes(FileName, Attributes)
End Function
Nor the documentation nor the Internet cover a bitvalue of
8192 (2048 at most). I have no clue how to solve this.

Here's the whole list:

#define FILE_ATTRIBUTE_READONLY 0x00000001
#define FILE_ATTRIBUTE_HIDDEN 0x00000002
#define FILE_ATTRIBUTE_SYSTEM 0x00000004
#define FILE_ATTRIBUTE_DIRECTORY 0x00000010
#define FILE_ATTRIBUTE_ARCHIVE 0x00000020
#define FILE_ATTRIBUTE_DEVICE 0x00000040
#define FILE_ATTRIBUTE_NORMAL 0x00000080
#define FILE_ATTRIBUTE_TEMPORARY 0x00000100
#define FILE_ATTRIBUTE_SPARSE_FILE 0x00000200
#define FILE_ATTRIBUTE_REPARSE_POINT 0x00000400
#define FILE_ATTRIBUTE_COMPRESSED 0x00000800
#define FILE_ATTRIBUTE_OFFLINE 0x00001000
#define FILE_ATTRIBUTE_NOT_CONTENT_INDEXED 0x00002000
#define FILE_ATTRIBUTE_ENCRYPTED 0x00004000

Be aware that not all can be *set* using SetFileAttribute. For limitations, see:

http://msdn.microsoft.com/library/en-us/fileio/base/setfileattributes.asp

Later... Karl
 
M

Marco

Thx a lot. Got to solve it.
-----Original Message-----
Hi Marco --


So far, so good.


Yep, looks like SetAttr doesn't like values in that
range. I think you'll need to go
to the API for this. You might be interested in
something I put together some time
ago -- FileInfo.zip on
http://www.mvps.org/vb/samples.htm -- for lots of file
"attribute" type stuff. But, in a nutshell, here's what you need to override VB's
own SetAttr function:

Private Declare Function SetFileAttributes Lib "kernel32" Alias
"SetFileAttributesA" (ByVal lpFileName As String, ByVal dwFileAttributes As Long) As
Long

Public Function SetAttr(FileName As String, ByVal Attributes As Long) As Boolean
' Nothing fancy, just set new attribute and return
SetAttr = SetFileAttributes(FileName, Attributes)
End Function


Here's the whole list:

#define FILE_ATTRIBUTE_READONLY 0x00000001
#define FILE_ATTRIBUTE_HIDDEN 0x00000002
#define FILE_ATTRIBUTE_SYSTEM 0x00000004
#define FILE_ATTRIBUTE_DIRECTORY 0x00000010
#define FILE_ATTRIBUTE_ARCHIVE 0x00000020
#define FILE_ATTRIBUTE_DEVICE 0x00000040
#define FILE_ATTRIBUTE_NORMAL 0x00000080
#define FILE_ATTRIBUTE_TEMPORARY 0x00000100
#define FILE_ATTRIBUTE_SPARSE_FILE 0x00000200
#define FILE_ATTRIBUTE_REPARSE_POINT 0x00000400
#define FILE_ATTRIBUTE_COMPRESSED 0x00000800
#define FILE_ATTRIBUTE_OFFLINE 0x00001000
#define FILE_ATTRIBUTE_NOT_CONTENT_INDEXED 0x00002000
#define FILE_ATTRIBUTE_ENCRYPTED 0x00004000

Be aware that not all can be *set* using
SetFileAttribute. For limitations, see:
http://msdn.microsoft.com/library/en- us/fileio/base/setfileattributes.asp

Later... Karl
--
[Microsoft Basic: 1976-2001, RIP]


.
 
F

Felix Lima

Hi Marco,

SetAttr only accepts the attributes outlined in the Help documentation, and
as per Karl's earlier emails, the API technique is the recommended
workaround.

I have made a request to our Content team on this issue for a possible KB
article explaining how SetAttr may return "Invalid Procudure Call or
Argument". I have also asked that our Development team review the current
implementation of the SetAttr statement to put it more in line with the API.

Kind Regards,

Félix Lima
Visual Basic and Visual Basic .NET


This posting is provided "AS IS" with no warranties, and confers no rights.
Please reply to newsgroups only.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top