MarshalAs for DWORD array?

P

Paul Harvey

Hi,
I have some VB6 code that calls unmanaged DLLs, and I am converting it to
C#. There's a couple of structs I need to define. I've had no problems with
this one:

Public Type A
sz(128) As Byte
End Type

The above VB6 translates happily into C# as the following:

public struct A {
[MarshalAs(UnmanagedType.ByValTStr,SizeConst=128)]
public string sz;
}


However, I'm not sure what to do with this one:

Public Type B
dwData(34) As Long
End Type

I'm sure dwData(34) is a DWORD array of lengh 34, and a DWORD is a 32-bit
unsigned integer, so I tried this:

public struct B {
[MarshalAs(UnmanagedType.U4,SizeConst=34)]
public uint dwData;
}

Unfortunately, it isn't working. How should I define a DWORD array in C#?
Any help would be appreciated!

Paul
 
D

Dustin Campbell

However, I'm not sure what to do with this one:
Public Type B
dwData(34) As Long
End Type
I'm sure dwData(34) is a DWORD array of lengh 34, and a DWORD is a
32-bit unsigned integer, so I tried this:

public struct B {
[MarshalAs(UnmanagedType.U4,SizeConst=34)]
public uint dwData;
}
Unfortunately, it isn't working. How should I define a DWORD array in
C#? Any help would be appreciated!

Use the UnmanagedType.ByValArray enumeration element like this:

public struct B {
[MarshalAs(UnmanagedType.ByValArray, ArraySubType=UnmangedType.U4, SizeConst=34)]
public uint[] dwData;
}

Best Regards,
Dustin Campbell
Developer Express Inc.
 
P

Paul Harvey

Hi,
Finally got my code working. Thanks a lot for your help! I was actually
still having a lot of trouble until I found out that a Long in VB6 is only
32-bit!! I made leaps of progress when I changed my C# longs to ints!

Paul
 

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

Top