V
Vaughn
What code would I need to write to this struct? The following code
deserializes the struct so the function runs correctly. But in order to
write to the struct before doing the function call, I need to add code in
System.IntPtr ICustomMarshaler.MarshalManagedToNative(object ManagedObj) and
void ICustomMarshaler.CleanUpNativeData(System.IntPtr pNativeData)
Any help would really be appreciated.
Zip4Parm zip4parm;
Zip4Parm.z4adrinq(out zip4parm); // DLL function call
//now use zip4parm here
public class Zip4Parm {
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi)]
private struct ZIP4_PARM {
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=4)]
public string rsvd0;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=26)]
public string footer;
}
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi)]
public struct ADDR_REC {
public byte detail_code;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=6)]
public string zip_code;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=11)]
public string update_key;
public byte action_code;
public byte rec_type;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=5)]
public string carr_rt;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=3)]
public string pre_dir;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=29)]
public string str_name;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=5)]
public string suffix;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=3)]
public string post_dir;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=11)]
public string prim_low;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=11)]
public string prim_high;
public byte prim_code;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=41)]
public string sec_name;
}
private class Zip4ParmMarshaler: ICustomMarshaler {
private int arrayLength;
private Zip4ParmMarshaler(int arrayLength) {
this.arrayLength=arrayLength;
}
object ICustomMarshaler.MarshalNativeToManaged(System.IntPtr
pNativeData) {
ZIP4_PARM
zip4_parm=(ZIP4_PARM)Marshal.PtrToStructure(pNativeData, typeof(ZIP4_PARM));
pNativeData=new
IntPtr(pNativeData.ToInt32()+Marshal.SizeOf(typeof(ZIP4_PARM)));
ADDR_REC[] stack=new ADDR_REC[arrayLength];
for (int i=0; i<arrayLength; i++) {
stack=(ADDR_REC)Marshal.PtrToStructure(pNativeData, typeof(ADDR_REC));
pNativeData=new
IntPtr(pNativeData.ToInt32()+Marshal.SizeOf(typeof(ADDR_REC)));
}
return new Zip4Parm(zip4_parm, stack);
}
int ICustomMarshaler.GetNativeDataSize() {
return
Marshal.SizeOf(typeof(ZIP4_PARM))+Marshal.SizeOf(typeof(ADDR_REC))*arrayLeng
th;
}
System.IntPtr ICustomMarshaler.MarshalManagedToNative(object
ManagedObj) {
throw new NotImplementedException();
}
void ICustomMarshaler.CleanUpManagedData(object ManagedObj)
{}
void ICustomMarshaler.CleanUpNativeData(System.IntPtr
pNativeData) {}
public static ICustomMarshaler GetInstance(string cookie) {
return new Zip4ParmMarshaler(cookie=="" ? 10 :
int.Parse(cookie));
}
}
[DllImport("ZIP4_W32.DLL")]
public static extern int
z4adrinq([Out][MarshalAs(UnmanagedType.CustomMarshaler,
MarshalTypeRef=typeof(Zip4ParmMarshaler))] out Zip4Parm zip4_parm);
public readonly string rsvd0;
public readonly string footer;
public readonly ADDR_REC[] stack;
private Zip4Parm(ZIP4_PARM zip4_parm, ADDR_REC[] stack) {
rsvd0=zip4_parm.rsvd0;
footer=zip4_parm.footer;
this.stack=stack;
}
}
deserializes the struct so the function runs correctly. But in order to
write to the struct before doing the function call, I need to add code in
System.IntPtr ICustomMarshaler.MarshalManagedToNative(object ManagedObj) and
void ICustomMarshaler.CleanUpNativeData(System.IntPtr pNativeData)
Any help would really be appreciated.
Zip4Parm zip4parm;
Zip4Parm.z4adrinq(out zip4parm); // DLL function call
//now use zip4parm here
public class Zip4Parm {
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi)]
private struct ZIP4_PARM {
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=4)]
public string rsvd0;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=26)]
public string footer;
}
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi)]
public struct ADDR_REC {
public byte detail_code;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=6)]
public string zip_code;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=11)]
public string update_key;
public byte action_code;
public byte rec_type;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=5)]
public string carr_rt;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=3)]
public string pre_dir;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=29)]
public string str_name;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=5)]
public string suffix;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=3)]
public string post_dir;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=11)]
public string prim_low;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=11)]
public string prim_high;
public byte prim_code;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=41)]
public string sec_name;
}
private class Zip4ParmMarshaler: ICustomMarshaler {
private int arrayLength;
private Zip4ParmMarshaler(int arrayLength) {
this.arrayLength=arrayLength;
}
object ICustomMarshaler.MarshalNativeToManaged(System.IntPtr
pNativeData) {
ZIP4_PARM
zip4_parm=(ZIP4_PARM)Marshal.PtrToStructure(pNativeData, typeof(ZIP4_PARM));
pNativeData=new
IntPtr(pNativeData.ToInt32()+Marshal.SizeOf(typeof(ZIP4_PARM)));
ADDR_REC[] stack=new ADDR_REC[arrayLength];
for (int i=0; i<arrayLength; i++) {
stack=(ADDR_REC)Marshal.PtrToStructure(pNativeData, typeof(ADDR_REC));
pNativeData=new
IntPtr(pNativeData.ToInt32()+Marshal.SizeOf(typeof(ADDR_REC)));
}
return new Zip4Parm(zip4_parm, stack);
}
int ICustomMarshaler.GetNativeDataSize() {
return
Marshal.SizeOf(typeof(ZIP4_PARM))+Marshal.SizeOf(typeof(ADDR_REC))*arrayLeng
th;
}
System.IntPtr ICustomMarshaler.MarshalManagedToNative(object
ManagedObj) {
throw new NotImplementedException();
}
void ICustomMarshaler.CleanUpManagedData(object ManagedObj)
{}
void ICustomMarshaler.CleanUpNativeData(System.IntPtr
pNativeData) {}
public static ICustomMarshaler GetInstance(string cookie) {
return new Zip4ParmMarshaler(cookie=="" ? 10 :
int.Parse(cookie));
}
}
[DllImport("ZIP4_W32.DLL")]
public static extern int
z4adrinq([Out][MarshalAs(UnmanagedType.CustomMarshaler,
MarshalTypeRef=typeof(Zip4ParmMarshaler))] out Zip4Parm zip4_parm);
public readonly string rsvd0;
public readonly string footer;
public readonly ADDR_REC[] stack;
private Zip4Parm(ZIP4_PARM zip4_parm, ADDR_REC[] stack) {
rsvd0=zip4_parm.rsvd0;
footer=zip4_parm.footer;
this.stack=stack;
}
}