Question

The program works fine when it is run, but when I try to step through it, I get:

"An unhandled exception of type 'System.StackOverflowException' occurred in Unknown Module."

Here is the code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace PInvokeTest
{
    class Program
    {
        static void Main(string[] args)
        {
            int session_handle = 0;
            int flag = 0;
            int didsetup = 0;
            int defPort = 0;
            int i = 0, j = -1;
            short[] ROM;
            ROM = new short[9];
            short type_test = 0;
            short port_num = 0, port_type = 1;
            byte[] state_buf = new byte[5125];
            StringBuilder ID_buf = new StringBuilder();
            StringBuilder serial = new StringBuilder();
            StringBuilder serialtmp = new StringBuilder();



            //Finds default device type and port
            defPort = TMReadDefaultPort(out port_num, out port_type);

            // get the TMEX driver version
            Get_Version(ID_buf); // STACKOVERFLOW EXCEPTION HERE

            ...

            Console.ReadKey();
        }

        [DllImport("IBFS32.dll")]
        public static extern int TMExtendedStartSession(
            short PortNum,
            short PortType,
            IntPtr EnhancedOptions
        );

        [DllImport("IBFS32.dll")]
        public static extern short TMReadDefaultPort(
            out short port_num,
            out short port_type
        );

        [DllImport("IBFS32.dll")]
        public static extern short Get_Version(
            [MarshalAs(UnmanagedType.LPStr)]StringBuilder ID_buf
        );

        [DllImport("IBFS32.dll")]
        public static extern short TMGetTypeVersion(
            short port_type,
            [MarshalAs(UnmanagedType.LPStr)]StringBuilder ID_buf
        );

        [DllImport("IBFS32.dll")]
        public static extern short TMSetup(
            int session_handle
        );

        [DllImport("IBFS32.dll")]
        public static extern short TMNext(
            int session_handle,
            byte[] state_buf
        );

        [DllImport("IBFS32.dll")]
        public static extern short TMRom(
            int session_handle,
            byte[] state_buf,
            short[] ROM
        );

        [DllImport("IBFS32.dll")]
        public static extern short TMEndSession(
            int session_handle
        );
    }
}

What would cause it to only work when you run it without stepping? I have another program that is using very similar code (dll functions are in a different class), but it gets a stackoverflow exception no matter how it is run.

EDIT:

When I use the 64 bit version of this dll and build my program in x64, it works fine all of the time... I don't know if that info is any help to you though.

I need to get the 32 bit version running.

Was it helpful?

Solution

You must specify the initial capacity for your StringBuilder (via the constructor) to ensure it is big enough to store the result. You are calling the default constructor.

you can, instead, pass a System.Text.StringBuilder object; a pointer will be passed by the marshaler into the unmanaged function that can be manipulated. The only caveat is that the StringBuilder must be allocated enough space for the return value, or the text will overflow, causing an exception to be thrown by P/Invoke.

Intro to P/Invoke

StringBuilder ID_buf = new StringBuilder(MaxVersionLength);

OTHER TIPS

What would cause it to only work when you run it without stepping?

Most probably you run code in Release, but step over in Debug and in these cases, due the additional information required during Debug mode, available stack size is reduced, so available memory on stack for concrete code execution became smaller, which generates stackoverflow exception, as stack is full.

You have, by the way, a stackoverflow dangerous exception, so it's better to fix it untill you get the same problems in Release, so in production.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top