I am trying to open a file from VB.NET with ZwCreateFile, but I get
STATUS_DATATYPE_MISALIGNMENT returned. It's hard to know which bits to post,
so apologies for the dump below:
<header stuff>
<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Auto, Pack:=1)> _
Structure OBJECT_ATTRIBUTES
Dim Length As Int32
Dim RootDirectory As IntPtr
Dim ObjectName As IntPtr
Dim Attrs As ATTRIBUTES
Dim SecurityDescriptor As IntPtr
Dim SecurityQualityOfService As IntPtr
End Structure
<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Auto, Pack:=1)> _
Structure IO_STATUS_BLOCK
Dim Status As NTSTATUS
Dim Information As INFO
End Structure
<DllImport("ntdll", SetLastError:=True)> _
Friend Function ZwCreateFile(ByRef FileHandle As IntPtr, _
ByVal DesiredAccess As ACCESS_MASK, _
ByVal ObjectAttributes As IntPtr, _
ByRef IoStatusBlock As IntPtr, _
ByVal AllocationSize As IntPtr, _
ByVal FileAttributes As FILE_ATTRIBUTE, _
ByVal ShareAccess As SHARE_ACCESS, _
ByVal CreateDisposition As
CREATE_DISPOSITION, _
ByVal CreateOptions As CREATE_OPTIONS, _
ByVal EaBuffer As IntPtr, _
ByVal EaLength As Integer) As NTSTATUS
End Function
Public Sub InitializeObjectAttributes(ByRef InitializedAttributes As
OBJECT_ATTRIBUTES, _
ByVal ObjectName As String, _
ByVal Attrs As ATTRIBUTES, _
ByVal RootDirectory As IntPtr, _
ByVal SecurityDescriptor As
IntPtr)
InitializedAttributes.Length = Marshal.SizeOf(InitializedAttributes)
InitializedAttributes.RootDirectory = RootDirectory
InitializedAttributes.Attrs = Attrs
InitializedAttributes.ObjectName =
Marshal.StringToHGlobalUni(ObjectName)
InitializedAttributes.SecurityDescriptor = SecurityDescriptor
InitializedAttributes.SecurityQualityOfService = New IntPtr(0)
End Sub
</header stuff>
<code extract>
Dim oa As OBJECT_ATTRIBUTES
Dim objName As String
Dim allocation As ATTRIBUTES
objName = "test.txt"
allocation = ATTRIBUTES.OBJ_CASE_INSENSITIVE Or
API.ATTRIBUTES.OBJ_OPENIF
InitializeObjectAttributes(oa, objName, allocation, New IntPtr(0),
New IntPtr(0))
Dim da As ACCESS_MASK
Dim poa As IntPtr
Dim ns As NTSTATUS
Dim fileHandle As IntPtr
Dim piosb As IntPtr
Dim palloc As IntPtr
Dim fa As FILE_ATTRIBUTE
Dim sa As SHARE_ACCESS
Dim cd As CREATE_DISPOSITION
Dim co As CREATE_OPTIONS
Dim eb As IntPtr
Dim el As Integer
Dim iosb As IO_STATUS_BLOCK
' Set up da
da = API.ACCESS_MASK.SYNCHRONIZE Or API.ACCESS_MASK.GENERIC_READ
' set up poa
poa = Marshal.AllocHGlobal(Marshal.SizeOf(oa))
Marshal.StructureToPtr(oa, poa, False)
' set up piosb
piosb = Marshal.AllocHGlobal(Marshal.SizeOf(iosb))
' Set up palloc
palloc = New IntPtr(0)
fa = API.FILE_ATTRIBUTE.FILE_ATTRIBUTE_NORMAL
sa = API.SHARE_ACCESS.FILE_SHARE_READ Or
API.SHARE_ACCESS.FILE_SHARE_WRITE
cd = API.CREATE_DISPOSITION.FILE_OPEN
co = API.CREATE_OPTIONS.FILE_NON_DIRECTORY_FILE Or
API.CREATE_OPTIONS.FILE_SYNCHRONOUS_IO_NONALERT
eb = New IntPtr(0)
el = 0
ns = ZwCreateFile(fileHandle, da, poa, piosb, palloc, fa, sa, cd,
co, eb, el)
</code extract>
Has anybody got an idea why I get this return code? I am quite prepared to
believe that some of my definitions might not be quite right, but I have
tried all sorts of things, all to no avail.
An example of how it should be done - in any language - would be every bit
as helpful.
TIA
Charles
Charles Law - 31 Mar 2006 09:32 GMT
Does anyone have any ideas about this?
Thanks.
Charles
>I am trying to open a file from VB.NET with ZwCreateFile, but I get
>STATUS_DATATYPE_MISALIGNMENT returned. It's hard to know which bits to
[quoted text clipped - 119 lines]
>
> Charles