编组 [英] Marshalling

查看:86
本文介绍了编组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello all,


我需要在may应用程序中使用C ++编写的几个本机dll结构:

 typedef struct _UF01_INFORMATIONS {
BYTE Type [MAX_MODULE];
BYTE Nombre_Module;
BYTE Nb_Voies [MAX_MODULE];
BYTE Modele [MAX_MODULE];
char *名称[MAX_MODULE];
char *评论[MAX_MODULE];
BOOL UseInterrupt;
} UF01_INFORMATIONS,* PUF01_INFORMATIONS;

所以我写了这个:

 [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;
公共字节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 =" 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;
}

}
}

UF01_OpenDevices有效,但我错了

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

有人能帮帮我吗?


Thanx:)







解决方案

你试过这个:'if(UF01_GetInfosModule( 0,false,ref m_infos))'
'if(UF01_GetInfosModule(1,false,ref m_infos))'


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 :)



解决方案

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


这篇关于编组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆