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.

byte array in struct to unmanaged - sizeof

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
The Real Andy - 20 Mar 2006 11:33 GMT
I have been playing with some interop over the last copule of days and
have a small problem. I am trying to use DeviceIOControl to talk to a
custom driver. I have successfull called CreateFile and have used
DeviceIOControl to initialise the device. The problem I have is when i
try to send data.

I have a struct

typedef struct POLL_STRUCT {
    UINT    nSize;            // size of struct
    UINT    nPollSize;        // number of bytes to send
    UINT    nMaxSize;        // max chars expected
    BYTE    zBuffer[MAX_RX_SIZE];    // (poll->driver,
driver->poll+response)
}    TPOLL_STRUCT;

MAX_RX_SIZE is always 2048 bytes.

I have declared the c# struct as:

[StructLayout(LayoutKind.Sequential)]
struct POLL_STRUCT {
    public uint nSize;
    public uint nPollSize;
    public uint nMaxSize;
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 2048)]
    byte [] zBuffer;
    };

Now I thought that MarshalAs attribute would create the zbuffer
instance, but when i try to reference it it is null?

Now when i try and marshal the TPOLL_STRUCT;

lpPollBuffer =
Marshal.AllocHGlobal(Marshal.SizeOf(typeof(TPOLL_STRUCT)));

I seems to work fine, however, if i try to copy data to the array,

Array.Copy(fooArray,abyPollBuffer,fooArray.Length); <-- Exception

The Exception is an argument null exception. This makes sense, as i
already know that zBuffer is null. But AllocHGlobal has obviously
allocated the 2048 bytes on the heap, where is it in the managed code?

If I create and instance of the buffer like so;

abyPollBuffer.zbuffer = new byte[2048];

does this data get marshaled to the unmanaged heap?

I can for the life of me find and way to view the actual memory in the
c# debugger either, so I cant confirm.

Furthermore, If I was to make a struct like so:
[StructLayout(LayoutKind.Sequential)]
struct FOO{
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 2048)]
    byte [] data;
    uint uiMoreData;
    };

Is uiMoreData 2048 bytes after of data[0] ?? How in VS2005 do I view
the addresses of the data?

Sorry if these questions sound stupid. I was all excited about moving
to c# (from c++) now I am getting increasingly frustrated. I am sure
that once I adjust all will be good. I do miss my pointers though ;).

   
The Real Andy - 20 Mar 2006 12:20 GMT
>I have been playing with some interop over the last copule of days and
>have a small problem. I am trying to use DeviceIOControl to talk to a
[quoted text clipped - 66 lines]
>
>   

One more question,

If I make struct FOO{..};

class FOO {
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 2048)]
    public byte [] data;
    public uint uiMoreData;

    FOO()
    {
        data = new data[2048];
    }

};

Will this marhsal correctly to the c version of the struct?

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.