none
Marshalling RRS feed

  • Question

  • Hello all,

    I need to use several native dll struct written in C++ in may application :

    typedef struct _UF01_INFORMATIONS{
            BYTE Type[MAX_MODULE];
            BYTE Nombre_Module;
            BYTE Nb_Voies[MAX_MODULE];
            BYTE Modele[MAX_MODULE];
            char *Name[MAX_MODULE];
            char *Comments[MAX_MODULE];
            BOOL UseInterrupt;
    } UF01_INFORMATIONS, *PUF01_INFORMATIONS;

    So I wrote this :

    [System.Runtime.InteropServices.DllImport(@"C:\Program Files (x86)\SELIAtec\UF01\UF01.dll", EntryPoint = "UF01_OpenDevices", CallingConvention = System.Runtime.InteropServices.CallingConvention.StdCall)]
            unsafe static extern Boolean UF01_OpenDevices(ref byte devices);
    
            [System.Runtime.InteropServices.DllImport(@"C:\Program Files (x86)\SELIAtec\UF01\UF01.dll", EntryPoint = "UF01_CloseAll", CallingConvention = System.Runtime.InteropServices.CallingConvention.StdCall)]
            static extern void UF01_CloseAll();
    
            [System.Runtime.InteropServices.DllImport(@"C:\Program Files (x86)\SELIAtec\UF01\UF01.dll", EntryPoint = "UF01_UD01_8Entrees", CallingConvention = System.Runtime.InteropServices.CallingConvention.StdCall)]
            unsafe static extern Boolean UF01_UD01_8Entrees(byte Device, byte module, ref byte data);
    
            [System.Runtime.InteropServices.DllImport(@"C:\Program Files (x86)\SELIAtec\UF01\UF01.dll", EntryPoint = "UF01_UD01_8Entrees", CallingConvention = System.Runtime.InteropServices.CallingConvention.StdCall)]
            unsafe static extern Boolean UF01_GetInfosModule(byte Device, bool Watchdog, ref _UF01_INFORMATIONS uf01);
    
            public Form1()
            {
                InitializeComponent();
    
            }
    
    
            [StructLayout(LayoutKind.Sequential)]
            public struct _UF01_INFORMATIONS
            {
                [MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
                public byte[] Type;
                public byte Nombre_Module;
                [MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
                public byte[] Nb_Voies;
                [MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
                public byte[] Modele;
                [MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
                public string[] Name;
                [MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
                public string[] Comments;
                public bool UseInterrupt;
            }      
    
            private unsafe void button1_Click(object sender, EventArgs e)
            {
                byte nb_Device= new byte();
    
                _UF01_INFORMATIONS m_infos = new _UF01_INFORMATIONS();
                m_infos.Type = new byte[8];
                m_infos.Nb_Voies = new byte[8];
                m_infos.Modele = new byte[8];
                m_infos.Name = new string[8];
                m_infos.Comments = new string[8];
                m_infos.Nombre_Module = new byte();
                m_infos.UseInterrupt = false;
                int iSize = Marshal.SizeOf(m_infos);
    
    
                if (UF01_OpenDevices(ref nb_Device))
                {
                    this.label1.Text = "Number of CPU: " + nb_Device;
    
                    if (UF01_GetInfosModule(nb_Device, false, ref m_infos))
                    {
                        //m_infos = (_UF01_INFORMATIONS)(Marshal.PtrToStructure(uf01, typeof(_UF01_INFORMATIONS)));                    
                        label2.Visible = true;
                        label2.Text = "NNumber of modules : " + m_infos.Nombre_Module;
                    }
    
                }
            }

    The UF01_OpenDevices works but I get false with

    if (UF01_GetInfosModule(nb_Device, false, ref m_infos))

    Can someone help me please ?

    Thanx :)



    Thursday, January 10, 2019 7:30 PM

All replies

  • Did you try this: ‘if( UF01_GetInfosModule( 0, false, ref m_infos))’ or ‘if( UF01_GetInfosModule( 1, false, ref m_infos))’?

    • Edited by Viorel_MVP Thursday, January 10, 2019 8:02 PM
    • Proposed as answer by Stanly Fan Monday, January 14, 2019 1:37 AM
    Thursday, January 10, 2019 7:56 PM