当前位置: 代码迷 >> C# >> C# 调用C/C++ DLL解决办法
  详细解决方案

C# 调用C/C++ DLL解决办法

热度:19   发布时间:2016-05-05 03:48:05.0
C# 调用C/C++ DLL
typedef struct IdcInfo {
char name[30+1]; //
char sex[1+1]; //'1'男 '2'女 
char nation[2+1]; //民族 '01'-'57'
char birth[8+1]; //YYYYMMDD
char addr[70+1];
char pid[18+1];
char issue[30+1]; //发卡机关
char valid_start[8+1]; //
char valid_end[8+1];   //
} TIdcInfo;

//读身份证,读取成功后,会在当前目录生成xp.bmp照片文件  // iccdll.dll
STDAPI ReadIdcInfo(int ReaderHandle, TIdcInfo *pIdcInfo);




求C#中该如何调用,谢谢!
------解决思路----------------------
我直接上代码吧:
C#端:

    //public struct Test
    //{
    //char name[30+1]; //
    //char sex[1+1];    //'1'男 '2'女 
    //char nation[2+1];    //民族 '01'-'57'
    //char birth[8+1];    //YYYYMMDD
    //char addr[70+1];
    //char pid[18+1];
    //char issue[30+1];    //发卡机关
    //char valid_start[8+1]; //
    //char valid_end[8+1];   //
    //int test;
    //}

    [StructLayout(LayoutKind.Sequential)]
    public struct Test
    {
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 31)]
        public string name;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 2)]
        public string sex;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 3)]
        public string nation;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 9)]
        public string birth;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 71)]
        public string addr;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 19)]
        public string pid;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 31)]
        public string issue;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 9)]
        public string valid_start;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 9)]
        public string valid_end;
        [MarshalAs(UnmanagedType.I4)]
        public int test;
    } 

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        [DllImport("Testing.dll", EntryPoint = "fnTesting")]
        public static extern int fnTesting(ref Test ts);

        private void button1_Click(object sender, EventArgs e)
        {
            Test ts = new Test();
            int s = fnTesting(ref ts);
        }
    }


C++端:

TESTING_API int fnTesting(IdcInfo* info)
{
if(NULL != info)
{
strcpy(info->name, "abc");
::MessageBoxA(NULL, info->name, "", MB_OK);
info->test = 8888;
}
return 42;
}


在XP+ VS 2005下测试通过,没有任何问题。
  相关解决方案