Problem with Interop DllImport Memory Corruption and AccessViolationException

Hi,

I am having problems with the following operation to call GetConnectionInfo in VC++ 6 from c# 2.0

C++ Constructs as follows:

typedef struct _OUTPUTPOINT {

BYTE cDevicePath[MAX_PATH +8]; //DevicePath

BYTE cLinkPath[1024]; //Use Lib.

} OUTPUTPOINT, *POUTPUTPOINT;

typedef DWORD ( CALLBACK *pfnGetConnectionInfo )( POUTPUTPOINT );

C# Constructs:

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]

public class OutPutPoint

{

public char[] cDevicePath;

public char[] cLinkPath;

}

IntPtr outArray;

uint retVal = GetConnInfo(out outArray);

OutPutPoint output = new OutPutPoint();

Marshal.PtrToStructure(outArray, output);

MessageBox.Show(outArray.ToString());

Just want to get the POUTPOINT struct[] to come back with info that I can use, I am a beginner at Interop and p/invoke operations and any help would be hugely appreciated

Thanks in advance.




Answer this question

Problem with Interop DllImport Memory Corruption and AccessViolationException

  • Monish Nagisetty

    Fixed that MAX_PATH + 8 is actually 268 this pushed the heap along

  • Neil Tippett

    I now get a 'FatalExecutionEngineError exception

    Code as follows:

    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]

    public class OutPutPoint

    {

    [MarshalAs(UnmanagedType.ByValArray, SizeConst=264)]

    public char[] cDevicePath;

    [MarshalAs(UnmanagedType.ByValArray, SizeConst=1024)]

    public char[] cLinkPath;

    }

    [DllImport("CTRL150.DLL", CallingConvention = CallingConvention.Winapi,

    EntryPoint = "GetConnectionInfo")]

    public static extern uint GetConnectionInfo(out IntPtr outArray);

    uint retVal = GetConnectionInfo(out outArray);

    MessageBox.Show(outArray.ToString());



  • Deerhake44

    You've almost got it. Remember that in C++ an array is just a pointer so in your case you are marshalling two pointers to C++ but it is expecting buffers. Therefore you need to modify your C# structure definition to define the size of the arrays (MAX_PATH+8 and 1024) respectively. One way to do this is use the MarshalAs attribute and specify the SizeConst option to specify the length:

    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
    public class OutPutPoint
    {
    [MarshalAs(UnmanagedType.ByValArray, SizeConst=264)]
    public char[] cDevicePath;

    [MarshalAs(UnmanagedType.ByValArray, SizeConst=1024)]
    public char[] cLinkPath;
    }

    Michael Taylor - 12/8/06


  • PaulSw

    Got that too work by changing removing out from extern call

    I assume its because its a class so marshalled byref by default

    Now getting wierd characters at the start of cLinkPath does anyone have any suggestions

    Thanks in advance



  • Biggo

    I've now got a System.AccessViolationException with the following code

    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]

    public class OutPutPoint

    {

    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 264)]

    public char[] cDevicePath;

    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 1024)]

    public char[] cLinkPath;

    }

    public class PrinterManager

    {

    public OutPutPoint[] appOutputPoints;

    [DllImport("CTRL150.DLL", CallingConvention = CallingConvention.StdCall,

    EntryPoint = "GetConnectionInfo", ExactSpelling = false)]

    public static extern uint wGetConnectionInfo(out IntPtr outArray);

    public void GetConnectionInfo()

    {

    IntPtr outArray;

    uint retVal = wGetConnectionInfo(out outArray);

    }

    }



  • Problem with Interop DllImport Memory Corruption and AccessViolationException