在C#中调用C语言写的动态链接库 DLL

1.首先将相关的C语言接口的DLL文件复制到bin\Debug                       
2.添加 using System.Windows.Shapes; 
3. 在类中用一下方式添加接口函数 
        [DllImport("TFFT.dll", EntryPoint = "TtfTest_InitTestEvn")]
        public static extern int TtfTest_InitTestEvn();

        [DllImport("TFFT.dll", EntryPoint = "TtfTest_Test")]
        public static extern int TtfTest_Test(); 

4.在相应的地方引用相关的函数TtfTest_InitTestEvn(),TtfTest_Test()。


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;                    
using System.Windows.Shapes; 
using System.Runtime.InteropServices;

namespace One
{
    /// 
    /// MainWindow.xaml 的交互逻辑
    /// 
    ///   
    public partial class MainWindow : Window
    {
        [DllImport("TFFT.dll", EntryPoint = "TtfTest_InitTestEvn")]
        public static extern int TtfTest_InitTestEvn();

        [DllImport("TFFT.dll", EntryPoint = "TtfTest_Test")]
        public static extern int TtfTest_Test();
   
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            string strDiffTime;
            DateTime dtStart, dtEnd;

            dtStart = DateTime.Now;
            for (int i = 0; i < 10; ++i )
            {
                MainWindow.TtfTest_InitTestEvn();
                MainWindow.TtfTest_Test();
            }
          
            dtEnd = DateTime.Now;//获得当前时间

            double dStartTime = dtStart.TimeOfDay.TotalSeconds;
            double dEndTime = dtEnd.TimeOfDay.TotalSeconds;

            strDiffTime = (dEndTime - dStartTime).ToString() + ":s";
            MessageBox.Show(strDiffTime);
          
        }
    }
}


你可能感兴趣的:(C#)