Marshaling C# struct for C++ dll function

0

I have a c++ dll function that takes as a parameter a pointer to this struct:

struct tLBCSHREP_PARAMS
{
  BYTE Ps;
  char* Shift;
  char* Cashier;
  char* CashRegNr;
};

, where BYTE is an 8 bit integer.

I am calling this c++ function in C# code. I have created C# equivalent for that c++ struct:

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
    public struct tLBCSHREP_PARAMS
    {
        public byte Ps;
        public IntPtr Shift;
        public IntPtr Cashier;
        public IntPtr CashRegNr;
    };

I am creating an instatce of this struct and then a pointer:

tLBCSHREP_PARAMS p_tLBCSHREP_PARAMS = new tLBCSHREP_PARAMS();
 p_tLBCSHREP_PARAMS = rapkas;

 rapkas.Ps = (byte)ps;
 rapkas.Shift =  Marshal.StringToHGlobalAnsi(shift);
 rapkas.Cashier = Marshal.StringToHGlobalAnsi(cashier);
 rapkas.CashRegNr = Marshal.StringToHGlobalAnsi(cashregnr);

IntPtr ptrLBTSRLN = Marshal.AllocHGlobal(Marshal.SizeOf(rapkas));
Marshal.StructureToPtr(rapkas, ptrLBTSRLN, false);

After passing pointer to the dll funcion I get error: 0xC0000001. To my knowledge it means segmentation fault, so probably struct is not created in right way. I have tried many times adding differrent attributes to the struct, adding '[MarshalAs(UnmanagedType.U1)]' phrase before 'public byte Ps;' variable and many more. Nothing worked ;(

c#
c++
dll
struct
marshalling
asked on Stack Overflow Aug 26, 2015 by Jakub Wieczorek

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0