c#调用c ++ dll,这是c ++头文件,如何对c#代码进行编程 [英] c# invoking c++ dll,this is c++ header file,how to program c# code

查看:69
本文介绍了c#调用c ++ dll,这是c ++头文件,如何对c#代码进行编程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 typedef  struct  tagGOODSDATA {
       wchar_t szGoodsName [ 128 ]; //  1.物品名称
       wchar_t szGoodsUnit [ 32 ]; //  2.物品单位
       字符 szGoodsNum [ 24 ]; //  3.物品数量
} GOODSDATA,* PGOODSDATA; 


 typedef  struct  tagINVOICEEXTDATA {
       wchar_t szFpBuyerName [ 128 ]; //  1.购方企业名称
       wchar_t szFpSellerName [ 128 ]; //  2.销方企业名称
       字符 szGoodsRows [ 4 ]; //  3.物品行数
       GOODSDATA szGoodsData [ 8 ]; //  4.物品信息
} INVOICEEXTDATA,* PINVOICEEXTDATA; 



 typedef  struct  tagINVOICEDATA {
    字符 szFpImage [ 258 ]; // 发票扫描图像文件名
    字符 szFpCode [ 20 ]; //  1:发票代码(10个字节)
    字符 szFpSeriaoNo [ 20 ]; //  3:发票印刷序号(8字节)
    字符 szFpCode1 [ 20 ];
    字符 szFpSeriaoNo1 [ 20 ]; //  3:发票印刷序号(8字节)
    字符 szFpEnVersion [ 10 ]; // 加密版本号(2字节)??? 
    字符 szFpDate [ 20 ]; //  2:发票开票日期(6字节)
    字符 szFpTaxRate [ 10 ]; //  8:税率!!! 
    字符 szFpBuyerTaxNo [ 30 ]; //  4:购方税收号(15/17/18/20字节)
    字符 szFpSellerTaxNo [ 30 ]; //  7:销方税收号(15/17/20字节)
    字符 szFpTotal [ 40 ]; // 价税合计
    字符 szFpSum [ 40 ]; //  5:金额
    字符 szFpTax [ 40 ]; //  6:税额!!! 
    字符 szFpCryptograph [ 1024 ]; //  0:密文数据(84/108/780字节)
} INVOICEDATA,* PINVOICEDATA; 


int WINAPI FpGetVersion(int nCompanyCode,int nScannerType);
int WINAPI FpScannerInitialize(void);
int WINAPI FpScannerTerminate(int wAction);
int WINAPI FpGetNewPage(PINVOICEDATA lpFpBuffer,PINVOICEEXTDATA lpFpExtBuffer,int * isBarCode);

解决方案

此处的关键字是DllImport
检查一下- http://msdn.microsoft.com/zh-CN /library/aa288468(v=vs.71).aspx [ ^ ]


 [StructLayout(LayoutKind.Sequential,CharSet = CharSet.Unicode, Pack =  1 )]
        公共 结构 tagGoodNameUnit
        {
            // 物品名称
            [MarshalAs(UnmanagedType.ByValTStr,SizeConst =  128 )]]
            公共 字符串 szGoodsName;
            // 物品单位
            [MarshalAs(UnmanagedType.ByValTStr,SizeConst =  32 )]]
            公共 字符串 szGoodsUnit;
        } 


 [StructLayout(LayoutKind.Sequential,CharSet = CharSet.Unicode,Pack =  1 )]]
        公共 结构 tagFpBuyerSellerName
        {
            // 购方企业名称
            [MarshalAs(UnmanagedType.ByValTStr,SizeConst =  128 )]]
            公共 字符串 szFpBuyerName;
            // 销方企业名称
            [MarshalAs(UnmanagedType.ByValTStr,SizeConst =  128 )]]
            公共 字符串 szFpSellerName;
        } 


 [StructLayout(LayoutKind.Sequential,Pack =  1 )]]
        公共 结构 TagGOODSDATA
        {
            [MarshalAs(UnmanagedType.Struct)]
            公共 tagGoodNameUnit szGoodsNameUnit;
            // 物品数量
            [MarshalAs(UnmanagedType.ByValTStr,SizeConst =  24 )]]
            公共 字符串 szGoodsNum;
        } 


 [StructLayout(LayoutKind.Sequential,Pack = 1)]
        公共结构TagINVOICEEXTDATA
        {
            [MarshalAs(UnmanagedType.Struct)]
            公共 tagFpBuyerSellerName szFpBuyerSellerName;
            //物品行数
            [MarshalAs(UnmanagedType.ByValTStr,SizeConst =  4 )]]
             public 字符串szGoodsRows;
            //物品信息枚举
            [MarshalAs(UnmanagedType.ByValArray,SizeConst =  8 )]
            公共 TagGOODSDATA [] szGoodsData;
        } 


 [StructLayout(LayoutKind.Sequential)]
        公共结构TagINVOICEDATA
        {
            //票证扫描图像文件名
            [MarshalAs(UnmanagedType.ByValTStr,SizeConst =  258 )]]
            公共 字符串 szFpImage;
            //票证代码(10字节)
            [MarshalAs(UnmanagedType.ByValTStr,SizeConst =  20 )]]
            公共 字符串 szFpCode;
            //票务印刷序号(8字节)
            [MarshalAs(UnmanagedType.ByValTStr,SizeConst =  20 )]]
            公共 字符串 szFpSeriaoNo;
            //
            [MarshalAs(UnmanagedType.ByValTStr,SizeConst =  20 )]]
            公共 字符串 szFpCode1;
            //
            [MarshalAs(UnmanagedType.ByValTStr,SizeConst =  20 )]]
            公共 字符串 szFpSeriaoNo1;
            //加密版本号(2字节)
            [MarshalAs(UnmanagedType.ByValTStr,SizeConst =  10 )]]
            公共 字符串 szFpEnVersion;
            //开票日期
            [MarshalAs(UnmanagedType.ByValTStr,SizeConst =  20 )]]
            公共 字符串 szFpDate;
            //税率
            [MarshalAs(UnmanagedType.ByValTStr,SizeConst =  10 )]]
            公共 字符串 szFpTaxRate;
            //购方纳税号
            [MarshalAs(UnmanagedType.ByValTStr,SizeConst =  30 )]]
            公共 字符串 szFpBuyerTaxNo;
            //销售方税收号
            [MarshalAs(UnmanagedType.ByValTStr,SizeConst =  30 )]]
            公共 字符串 szFpSellerTaxNo;
            //价税合计
            [MarshalAs(UnmanagedType.ByValTStr,SizeConst =  40 )]]
            公共 字符串 szFpTotal;
            //金额
            [MarshalAs(UnmanagedType.ByValTStr,SizeConst =  40 )]
            公共 字符串 szFpSum;
            //税额
            [MarshalAs(UnmanagedType.ByValTStr,SizeConst =  40 )]]
            公共 字符串 szFpTax;
            //密文数据
            [MarshalAs(UnmanagedType.ByValTStr,SizeConst =  1024 )]]
            公共 字符串 szFpCryptograph;
        } 


 [DllImport(" ,ExactSpelling = 为真)]
        公共 静态 外部  int  FpGetNewPage( ref  TagINVOICEEXTDATA lpFpExtBuffer, ref   int  isBarCode); 


 [DllImport(" ,ExactSpelling = 为真)]
        公共 静态 外部  int  nCompanyCode, [DllImport(" ,ExactSpelling = 为真)]
        公共 静态 外部  [DllImport(" ,ExactSpelling = 为真)]
        公共 静态 外部  int  wAction); 





TagINVOICEDATA标签=新的TagINVOICEDATA();
TagINVOICEEXTDATA tagExt =新的TagINVOICEEXTDATA();
int iIsTwoWM = 0;

int iVersion = FpGetVersion(26494179,51);
int iInitialize = FpScannerInitialize();
int result = FpGetNewPage(ref标签,ref tagExt,ref iIsTwoWM);
int iResult = FpScannerTerminate(0);

这是C#代码,但是结构信息错误,因为汉字,我使用unicode,但是商品信息错误


typedef struct tagGOODSDATA {
       wchar_t        szGoodsName [128];        //1.物品名称
       wchar_t        szGoodsUnit [32];         //2.物品单位
       char           szGoodsNum[24];           //3.物品数量
}GOODSDATA, *PGOODSDATA;


typedef struct tagINVOICEEXTDATA {
       wchar_t         szFpBuyerName[128];          //1.购方企业名称
       wchar_t         szFpSellerName[128];         //2.销方企业名称
       char            szGoodsRows[4];                      //3.物品行数
       GOODSDATA       szGoodsData[8];              //4.物品信息数组
}INVOICEEXTDATA, *PINVOICEEXTDATA;



typedef struct tagINVOICEDATA {
    char    szFpImage[258];             //  发票扫描图像文件名
    char    szFpCode[20];               //1:发票代码(10字节)
    char    szFpSeriaoNo[20];           //3:发票印刷序号(8字节)
    char    szFpCode1[20];
    char    szFpSeriaoNo1[20];          //3:发票印刷序号(8字节)
    char    szFpEnVersion[10];          //  加密版本号(2字节) ???
    char    szFpDate[20];               //2:发票开票日期(6字节)
    char    szFpTaxRate[10];            //8:税率 !!!
    char    szFpBuyerTaxNo[30];         //4:购方纳税号(15/17/18/20字节)
    char    szFpSellerTaxNo[30];        //7:销方纳税号(15/17/20字节)
    char    szFpTotal[40];              //  价税合计
    char    szFpSum[40];                //5:金额
    char    szFpTax[40];                //6:税额 !!!
    char    szFpCryptograph[1024];      //0:密文数据(84/108/780字节)
}INVOICEDATA, *PINVOICEDATA;


int WINAPI FpGetVersion(int nCompanyCode, int nScannerType);
int WINAPI FpScannerInitialize(void);
int WINAPI FpScannerTerminate(int wAction);
int WINAPI FpGetNewPage(PINVOICEDATA lpFpBuffer, PINVOICEEXTDATA lpFpExtBuffer, int *isBarCode);

解决方案

The keyword here is DllImport
Check this out - http://msdn.microsoft.com/en-us/library/aa288468(v=vs.71).aspx[^]


[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode,Pack=1)]
        public struct tagGoodNameUnit
        {
            //物品名称
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
            public string szGoodsName;
            //物品单位
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
            public string szGoodsUnit;
        }


[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode,Pack=1)]
        public struct tagFpBuyerSellerName
        {
            //购方企业名称
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
            public string szFpBuyerName;
            //销方企业名称
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
            public string szFpSellerName;
        }


[StructLayout(LayoutKind.Sequential,Pack=1)]
        public struct TagGOODSDATA
        {
            [MarshalAs(UnmanagedType.Struct)]
            public tagGoodNameUnit szGoodsNameUnit;
            //物品数量
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 24)]
            public string szGoodsNum;
        }


[StructLayout(LayoutKind.Sequential,Pack=1)]
        public struct TagINVOICEEXTDATA
        {
            [MarshalAs(UnmanagedType.Struct)]
            public tagFpBuyerSellerName szFpBuyerSellerName;
            //物品行数
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 4)]
            public string szGoodsRows;
            //物品信息数组
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
            public TagGOODSDATA[] szGoodsData;
        }


[StructLayout(LayoutKind.Sequential)]
        public struct TagINVOICEDATA
        {
            //发票扫描图像文件名
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 258)]
            public string szFpImage;
            //发票代码(10字节)
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 20)]
            public string szFpCode;
            //发票印刷序号(8字节)
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 20)]
            public string szFpSeriaoNo;
            //
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 20)]
            public string szFpCode1;
            //
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 20)]
            public string szFpSeriaoNo1;
            //加密版本号(2字节)
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 10)]
            public string szFpEnVersion;
            //开票日期
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 20)]
            public string szFpDate;
            //税率
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 10)]
            public string szFpTaxRate;
            //购方纳税号
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 30)]
            public string szFpBuyerTaxNo;
            //销方纳税号
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 30)]
            public string szFpSellerTaxNo;
            //价税合计
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 40)]
            public string szFpTotal;
            //金额
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 40)]
            public string szFpSum;
            //税额
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 40)]
            public string szFpTax;
            //密文数据
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 1024)]
            public string szFpCryptograph;
        }


[DllImport("scanreco.dll", ExactSpelling = true)]
        public static extern int FpGetNewPage(ref TagINVOICEDATA lpFpBuffer, ref TagINVOICEEXTDATA lpFpExtBuffer, ref int isBarCode);


[DllImport("scanreco.dll", ExactSpelling = true)]
        public static extern int FpGetVersion(int nCompanyCode, int nScannerType);


[DllImport("scanreco.dll", ExactSpelling = true)]
        public static extern int FpScannerInitialize();


[DllImport("scanreco.dll", ExactSpelling = true)]
        public static extern int FpScannerTerminate(int wAction);





TagINVOICEDATA tag = new TagINVOICEDATA();
TagINVOICEEXTDATA tagExt = new TagINVOICEEXTDATA();
int iIsTwoWM = 0;

int iVersion = FpGetVersion(26494179, 51);
int iInitialize = FpScannerInitialize();
int result = FpGetNewPage(ref tag, ref tagExt, ref iIsTwoWM);
int iResult = FpScannerTerminate(0);

this is the c# code,but the struct info is wrong,because of chinese character,i use unicode,but the goods info is wrong


这篇关于c#调用c ++ dll,这是c ++头文件,如何对c#代码进行编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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