Hi,
I need to populate a struct in C# and pass it into VB6. It works fine for
almost all data types, but for some reason dates don't get passed through
correctly. The example provided is quite simplified. In the real code,
many different data types including fixed-length strings are marshalled, and
they all show up correctly in the VB6 code except for date types. Note that
I can not change the existing VB6 code.
The struct is defined in C# like this:
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode, Pack=4)]
public struct MyUdt
{
public int id; // Not relevant to the problem at hand
public float /* VB6 Date */ myDate;
}
The struct is created and initialized like this:
// Assume comObject is set to an instance of a COM object. I can debug into
it, so it's working.
public void CallVB6(object comObject, Type comObjectType, DateTime
myDotNetDate)
{
object[] parms = new object[1];
struct myUdt = new MyUdt();
myUdt.id = 42; // Just some value
myUdt.myDate = (float)myDotNetDate.ToOADate();
parms[0] = MakeByteArray(myUdt);
comObjectType.InvokeMember("SetState", BindingFlags.InvokeMember, null,
comObject, parms);
}
// The called VB6 component expects a byte array
private byte[] MakeByteArray(object o)
{
int size = Marshal.SizeOf(o);
byte[] data = new byte[size];
GCHandle handle = GCHandle.Alloc(data, GCHandleType.Pinned);
IntPtr buffer = handle.AddrOfPinnedObject();
Marshal.StructureToPtr(o, buffer, false);
handle.Free();
return data;
}
' Called VB6 code.
Public Type udtMY
Id As Short
MyDate as Date
End Type
Public Type udtMYDATA
Buffer As String * 5 ' 10 bytes
End Type
Dim m_udtProps as udtMY
Public Sub SetState(Buffer As String)
Dim udtData As udtMYDATA
udtData.Buffer = Buffer
LSet m_udtProps = udtData ' Examining m_udtProps at this point shows all
data correctly marshalled, except dates show 12:00 AM, which I believe
corresponds to an internal value of 0.0 in the IEEE float used by VB6 to
represent the date.
End Sub
Thanks!
Eric