How would you marshal this?
- From: "Christoffer" <christoffer@xxxxxxxxxx>
- Date: Thu, 29 Mar 2007 15:35:52 +0200
I'm trying to marshal these structs but I keep getting an access
violation...
typedef struct
{
BYTE Line;
BYTE Pr;
char * Name;
tEXTERNAL_NUMERIC Quantity;
char Vat;
tEXTERNAL_NUMERIC Price;
tEXTERNAL_NUMERIC Gross;
tEXTERNAL_NUMERIC Discount;
} tFUNC_PARAMS;
typedef struct
{
tTRANSL_FUNC TranslFuncPtr;
LPVOID ValuePtr;
} tEXTERNAL_NUMERIC;
typedef int ( *tTRANSL_FUNC )( LPVOID, char * );
I tried to Marshal it as
[StructLayout(LayoutKind.Sequential, Pack = 0)]
internal struct tFUNC_PARAMS
{
public byte Line;
public byte Pr;
public string Name;
public tEXTERNAL_NUMERIC Quantity;
public char Vat;
public tEXTERNAL_NUMERIC Price;
public tEXTERNAL_NUMERIC Gross;
public tEXTERNAL_NUMERIC Discount;
}
[StructLayout(LayoutKind.Sequential, Pack = 0)]
internal struct tEXTERNAL_NUMERIC
{
[MarshalAs(UnmanagedType.FunctionPtr)]
public tTRANSL_FUNC TranslFuncPtr;
public IntPtr ValuePtr;
}
internal delegate int tTRANSL_FUNC(IntPtr o, string s);
I'm trying the following code:
byte line = 1;
string name = "kaka";
string quantity = "10";
char vat = 'A';
string price = "10";
string gross = "10";
byte pr = 2;
string discount = null;
tFUNC_PARAMS addParams = new tFUNC_PARAMS();
addParams.Line = line;
addParams.Pr = pr;
addParams.Name = name;
IntPtr quantityPtr = IntPtr.Zero;
IntPtr pricePtr = IntPtr.Zero;
IntPtr grossPtr = IntPtr.Zero;
IntPtr discountPtr = IntPtr.Zero;
quantityPtr = Marshal.StringToHGlobalAuto(quantity);
addParams.Quantity.ValuePtr = quantityPtr;
addParams.Quantity.TranslFuncPtr = null;
addParams.Vat = vat;
pricePtr = Marshal.StringToHGlobalAuto(price);
addParams.Price.ValuePtr = pricePtr;
addParams.Price.TranslFuncPtr = null;
grossPtr = Marshal.StringToHGlobalAuto(gross);
addParams.Gross.ValuePtr = grossPtr;
addParams.Gross.TranslFuncPtr = null;
if (!String.IsNullOrEmpty(discount))
{
discountPtr = Marshal.StringToHGlobalAuto(discount);
}
addParams.Discount.ValuePtr = discountPtr;
addParams.Discount.TranslFuncPtr = null;
NativeMethods.Func(ref addParams); // int Func( tFUNC_PARAMS *paramsPtr )
Where have I gone wrong?
Cheers,
Chris
.
- Follow-Ups:
- Re: How would you marshal this?
- From: Markus Eßmayr
- Re: How would you marshal this?
- Prev by Date: Calling C#.Net COM object from InternetExplorer gives 'Automation Server can't create object'
- Next by Date: Re: C# to C++ Dll with a pointer to string.
- Previous by thread: Calling C#.Net COM object from InternetExplorer gives 'Automation Server can't create object'
- Next by thread: Re: How would you marshal this?
- Index(es):
Relevant Pages
|
Loading