码迷,mamicode.com
首页 > 编程语言 > 详细

C# 调用C++DLL 传结构体数组

时间:2015-05-10 09:40:42      阅读:214      评论:0      收藏:0      [点我收藏+]

标签:

 C# 调用C++DLL 传结构体数组,注意C#和C++数据类型占用字节数要对应。否则传进去的数组会错位。C++ BOOL 对应C#bool.

1.c++代码

  

//MyDLL.h
#ifndef  MYDLL_H_
#define  MYDLL_H_
#include <iostream>
#include <windows.h>

#ifdef EXTERN_EXPORT
#define EXTERN_EXPORT extern "C"  _declspec(dllimport) 
#else
#define EXTERN_EXPORT extern "C" _declspec(dllexport) 
#endif


using namespace std;

struct Table_Product
{
    int     i1_product_num;

    char   i2_product_code[20];
    char   i3_product_name[20];
    char   i4_name_spell[20];
    char   i5_product_abbr[20];
    char   i6_product_abbr_spell[20];

    int     i7_product_category;
    int     i8_product_department;
    BOOL    i9_product_pcs_flag;
    int        i10_product_default_pcs;

    char     i11_product_unit_text[20];

    int      i12_product_tare_num;
    float    i13_product_stock;
    float    i14_product_stock_low;
    float    i15_product_stock_top;
    float    i16_product_FCL_ratio;
    int     i17_product_score_type;
    float   i18_product_score_ratio;
    float    i19_product_exchange_score;
    float    i20_product_price_lowest;
    float    i21_product_price;
    float    i22_product_price_vip1;
    float    i23_product_price_vip2;
    float    i24_product_price_vip3;
    int     i25_product_priceUnit;
    BOOL    i26_product_changePrice;
    BOOL    i27_product_discount;
    int     i28_product_tax_num;

    char     i29_product_ingredient[20];

    float    i30_product_period;
    float    i31_product_recommend;
    int     i32_product_labelFormat;
    BOOL    i33_product_trace;
    char     i34_product_image[20];
    char     i35_product_area[20];
    int     i36_product_temp;
    char     i37_product_remark1[20];
    char     i38_product_remark2[20];
    BOOL    i39_product_fastKey;
    BOOL    i40_product_disabled;

};

EXTERN_EXPORT int  Test1(Table_Product *tp);


#endif
#include "my_dll.h"

int Test1(Table_Product *tp)
{
    for (unsigned int i = 0; i < 2; i++)
    {
        string s = tp[i].i2_product_code;
    }


    return 0;
}

C#代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
namespace CSharpAndCPPDLL
{
    public class CPPDLL
    {

        [StructLayoutAttribute(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1), Serializable]
        public struct Table_Product
        {
            
            public int i1_product_num;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 20)]
            public string i2_product_code;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 20)]
            public string i3_product_name;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 20)]
            public string i4_name_spell;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 20)]
            public string i5_product_abbr;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 20)]
            public string i6_product_abbr_spell;

         
            public int i7_product_category;
          
            public int i8_product_department;
          
            public bool i9_product_pcs_flag;
           
            public int i10_product_default_pcs;

         
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 20)]
            public string i11_product_unit_text;
            
            public int i12_product_tare_num;
           
            public float i13_product_stock;
          
            public float i14_product_stock_low;
          
            public float i15_product_stock_top;
          
            public float i16_product_FCL_ratio;
           
            public int i17_product_score_type;
           
            public float i18_product_score_ratio;
           
            public float i19_product_exchange_score;
            
            public float i20_product_price_lowest;
           
            public float i21_product_price;
           
            public float i22_product_price_vip1;
           
            public float i23_product_price_vip2;
          
            public float i24_product_price_vip3;
         
            public int i25_product_priceUnit;
           
            public bool i26_product_changePrice;
            
            public bool i27_product_discount;
          
            public int i28_product_tax_num;

            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 20)]
            public string i29_product_ingredient;
           
            public float i30_product_period;
          
            public float i31_product_recommend;
           
            public int i32_product_labelFormat;
           
            public bool i33_product_trace;

            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 20)]
            public string i34_product_image;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 20)]
            public string i35_product_area;
          
            public int i36_product_temp;

            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 20)]
            public string i37_product_remark1;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 20)]
            public string i38_product_remark2;

            public bool i39_product_fastKey;
            public bool i40_product_disabled;

        };


        [DllImport("TestDLL.dll", EntryPoint = "Test1", CallingConvention = CallingConvention.Cdecl)]
       public static extern int Test1([In]Table_Product[] tp);


    }
}
        private void button1_Click(object sender, EventArgs e)
        {

            CPPDLL.Table_Product[] tp = new CPPDLL.Table_Product[2];

            tp[0] = new CPPDLL.Table_Product();
        
            tp[0].i1_product_num = 111;

            tp[0].i2_product_code = "211";
            tp[0].i3_product_name = "樱桃";
            tp[0].i4_name_spell = "yt";
            tp[0].i5_product_abbr = "59999";
            tp[0].i6_product_abbr_spell = "699999";

            tp[0].i7_product_category = 7;
            tp[0].i8_product_department = 8;
            tp[0].i9_product_pcs_flag = false;
            tp[0].i10_product_default_pcs = 910;

            tp[0].i11_product_unit_text = "牛奶";

            tp[0].i12_product_tare_num = 12;
            tp[0].i13_product_stock = 9.13f;
            tp[0].i14_product_stock_low = 9.14f;
            tp[0].i15_product_stock_top = 9.15f;
            tp[0].i16_product_FCL_ratio = 9.16f;
            tp[0].i17_product_score_type = 17;
            tp[0].i18_product_score_ratio = 9.18f;
            tp[0].i19_product_exchange_score = 9.19f;
            tp[0].i20_product_price_lowest = 9.20f;
            tp[0].i21_product_price = 9.21f;
            tp[0].i22_product_price_vip1 = 9.22f;
            tp[0].i23_product_price_vip2 = 9.23f;
            tp[0].i24_product_price_vip3 = 9.24f;
            tp[0].i25_product_priceUnit = 25;
            tp[0].i26_product_changePrice = false;
            tp[0].i27_product_discount = false;
            tp[0].i28_product_tax_num = 28;

            tp[0].i29_product_ingredient = "929";

            tp[0].i30_product_period = 9.30f;
            tp[0].i31_product_recommend = 9.31f;
            tp[0].i32_product_labelFormat = 932;
            tp[0].i33_product_trace = false;
            tp[0].i34_product_image = "934image";
            tp[0].i35_product_area = "中国35";
            tp[0].i36_product_temp = 36;
            tp[0].i37_product_remark1 = "remark1";
            tp[0].i38_product_remark2 = "remark2积极向上";
            tp[0].i39_product_fastKey = false;
            tp[0].i40_product_disabled = false;


            tp[1] = new CPPDLL.Table_Product();
            tp[1].i1_product_num = 122;

            tp[1].i2_product_code = "222";
            tp[1].i3_product_name = "樱桃";
            tp[1].i4_name_spell = "yt";
            tp[1].i5_product_abbr = "59999";
            tp[1].i6_product_abbr_spell = "699999";

            tp[1].i7_product_category = 7;
            tp[1].i8_product_department = 8;
            tp[1].i9_product_pcs_flag = false;
            tp[1].i10_product_default_pcs = 910;

            tp[1].i11_product_unit_text = "牛奶";

            tp[1].i12_product_tare_num = 12;
            tp[1].i13_product_stock = 9.13f;
            tp[1].i14_product_stock_low = 9.14f;
            tp[1].i15_product_stock_top = 9.15f;
            tp[1].i16_product_FCL_ratio = 9.16f;
            tp[1].i17_product_score_type = 17;
            tp[1].i18_product_score_ratio = 9.18f;
            tp[1].i19_product_exchange_score = 9.19f;
            tp[1].i20_product_price_lowest = 9.21f;
            tp[1].i21_product_price = 9.21f;
            tp[1].i22_product_price_vip1 = 9.22f;
            tp[1].i23_product_price_vip2 = 9.23f;
            tp[1].i24_product_price_vip3 = 9.24f;
            tp[1].i25_product_priceUnit = 25;
            tp[1].i26_product_changePrice = false;
            tp[1].i27_product_discount = false;
            tp[1].i28_product_tax_num = 28;

            tp[1].i29_product_ingredient = "929";

            tp[1].i30_product_period = 9.31f;
            tp[1].i31_product_recommend = 9.31f;
            tp[1].i32_product_labelFormat = 932;
            tp[1].i33_product_trace = false;
            tp[1].i34_product_image = "934image";
            tp[1].i35_product_area = "中国35";
            tp[1].i36_product_temp = 36;
            tp[1].i37_product_remark1 = "remark1";
            tp[1].i38_product_remark2 = "remark2积极向上";
            tp[1].i39_product_fastKey = false;
            tp[1].i40_product_disabled = false;

            int ret=CPPDLL.Test1(tp);
        }

 

C# 调用C++DLL 传结构体数组

标签:

原文地址:http://www.cnblogs.com/ike_li/p/4491762.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!