C#调用C、C++dll

本文介绍了如何使用C#通过DllImport特性调用C、C++编写的dll动态链接库,以实现跨语言的函数调用。示例中展示了调用名为'Add'的函数,该函数接受两个整数参数并返回它们的和。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 
 
  1. [DllImport("MotorControlDll.dll",EntryPoint="Add",
  2. ExactSpelling=false,CallingConvention=CallingConvention.Cdecl)]  
  3. public static extern int Add(int a,int b); 

    至于DllImport属性的用法可以察看MSDN,对于各项参数有详细的说明。

    最后还要记得将DllDemo生成的位于Debug文件中DllDemo.dll文件加入到C#的WinForm程序的bin目录下。

    这样就可以直接使用Add(int a,int b)函数。

    这样,就完成了C#中调用dll库。


2、

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Security;

namespace CardControls
{
    /// <summary>
    /// 明华动态库
    /// </summary>
    [SecurityCritical]
    public class DllImportMHClass
    {
        //Encoding.Default.GetBytes()
        //Encoding.Default.GetString()
        [DllImport("Mwic_32.dll", EntryPoint = "ic_init", SetLastError = true,
           CharSet = CharSet.Auto, ExactSpelling = false,
           CallingConvention = CallingConvention.StdCall)]
        public static extern int ic_init(Int16 poud, Int32 baud);

        [DllImport("Mwic_32.dll", EntryPoint = "ic_usbinit", SetLastError = true,
            CharSet = CharSet.Auto, ExactSpelling = false,
            CallingConvention = CallingConvention.StdCall)]
        public static extern int ic_usbinit();

        [DllImport("Mwic_32.dll", EntryPoint = "ic_exit", SetLastError = true,
            CharSet = CharSet.Auto, ExactSpelling = false,
            CallingConvention = CallingConvention.StdCall)]
        public static extern Int16 ic_exit(int icdev);

        [DllImport("Mwic_32.dll", EntryPoint = "srd_ver", SetLastError = true,
            CharSet = CharSet.Auto, ExactSpelling = false,
            CallingConvention = CallingConvention.StdCall)]
        public static extern Int16 srd_ver(int icdev, int len, byte[] ver);
        //public static extern int srd_ver(int icdev, int len, byte[] ver);

        [DllImport("Mwic_32.dll", EntryPoint = "get_status", SetLastError = true,
            CharSet = CharSet.Auto, ExactSpelling = false,
            CallingConvention = CallingConvention.StdCall)]
        public static extern Int16 get_status(int icdev, ref Int16 number);

        [DllImport("Mwic_32.dll", EntryPoint = "dv_beep", SetLastError = true,
            CharSet = CharSet.Auto, ExactSpelling = false,
            CallingConvention = CallingConvention.StdCall)]
        public static extern Int16 dv_beep(int icdev, Int16 time);

        [DllImport("Mwic_32.dll", EntryPoint = "chk_4442", SetLastError = true,
            CharSet = CharSet.Auto, ExactSpelling = false,
            CallingConvention = CallingConvention.StdCall)]
        public static extern Int16 chk_4442(int icdev);

        [DllImport("Mwic_32.dll", EntryPoint = "rsct_4442", SetLastError = true,
            CharSet = CharSet.Auto, ExactSpelling = false,
            CallingConvention = CallingConvention.StdCall)]
        public static extern Int16 rsct_4442(int icdev, ref Int16 counter);

        [DllImport("Mwic_32.dll", EntryPoint = "swr_4442", SetLastError = true,
             CharSet = CharSet.Auto, ExactSpelling = false,
             CallingConvention = CallingConvention.StdCall)]
        public static extern Int16 swr_4442(int icdev, Int16 offset, Int16 len, byte[] w_string);

        [DllImport("Mwic_32.dll", EntryPoint = "srd_4442", SetLastError = true,
             CharSet = CharSet.Auto, ExactSpelling = false,
             CallingConvention = CallingConvention.StdCall)]
        public static extern Int16 srd_4442(int icdev, Int16 offset, Int16 len, byte[] r_string);


        [DllImport("Mwic_32.dll", EntryPoint = "csc_4442", SetLastError = true,
             CharSet = CharSet.Auto, ExactSpelling = false,
             CallingConvention = CallingConvention.StdCall)]
        public static extern Int16 csc_4442(int icdev, Int16 len, byte[] p_string);


        [DllImport("Mwic_32.dll", EntryPoint = "wsc_4442", SetLastError = true,
             CharSet = CharSet.Auto, ExactSpelling = false,
             CallingConvention = CallingConvention.StdCall)]
        public static extern Int16 wsc_4442(int icdev, Int16 len, byte[] p_string);

        [DllImport("Mwic_32.dll", EntryPoint = "rsc_4442", SetLastError = true,
             CharSet = CharSet.Auto, ExactSpelling = false,
             CallingConvention = CallingConvention.StdCall)]
        public static extern Int16 rsc_4442(int icdev, Int16 len, byte[] p_string);

        [DllImport("Mwic_32.dll", EntryPoint = "asc_hex", SetLastError = true,
             CharSet = CharSet.Auto, ExactSpelling = false,
             CallingConvention = CallingConvention.StdCall)]
        public static extern Int16 asc_hex(byte[] asc, byte[] hex, int length);

        [DllImport("Mwic_32.dll", EntryPoint = "hex_asc", SetLastError = true,
             CharSet = CharSet.Auto, ExactSpelling = true,
             CallingConvention = CallingConvention.StdCall)]
        public static extern Int16 hex_asc(byte[] hex, byte[] asc, int length);

        [DllImport("Mwic_32.dll", EntryPoint = "sam_slt_reset", SetLastError = true,
             CharSet = CharSet.Auto, ExactSpelling = true,
             CallingConvention = CallingConvention.StdCall)]
        public static extern Int16 sam_slt_reset(int icdev, byte cardSet, ref Int16 len, byte[] receive_data);

        [DllImport("Mwic_32.dll", EntryPoint = "sam_slt_protocol", SetLastError = true,
             CharSet = CharSet.Auto, ExactSpelling = true,
             CallingConvention = CallingConvention.StdCall)]
        public static extern Int16 sam_slt_protocol(int icdev, byte CardType, Int16 sLen, byte[] send_cmd, ref Int16 rLen, byte[] receive_data);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值