Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Interop / March 2006

Tip: Looking for answers? Try searching our database.

ZwCreateFile returns STATUS_DATATYPE_MISALIGNMENT

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Charles Law - 22 Mar 2006 01:21 GMT
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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.