栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

使用C++创建DLL实现简单的功能,并在C#环境下调用该DLL

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用C++创建DLL实现简单的功能,并在C#环境下调用该DLL

计算输入数据的阶乘,判断输入的合法性
计算2个输入数据a和b的差(a-b),若a 一:创建C#窗体程序:用于调用C++编写的DLL

(1)启动VS》新建项目》选择c# windows窗体应用(.net framework)

 (2)项目创建后,整体效果如下图:

二:用C++创建DLL

(1)在解决方案上右击选择【添加】>【新建项目】

 

 (2)在添加新项目对话框中,按下图进行选择填写

 (3)项目创建后,整体效果如下图:

(4)新建头文件(这里是为了独立开,也可以在已有的头文件上写)

 

 (5)在头文件中写入以下代码

#pragma once

extern "C" _declspec(dllexport) int _stdcall test01(int a);
extern "C" _declspec(dllexport) int _stdcall test02(int a, int b);

 (6)在源文件中新建一个cpp项目

 (7)添加刚刚写的头文件“test.h”和“pch.h"

并且写下如是测试代码

#include"test.h"
#include"pch.h"
int _stdcall test01(int a)
{
	if (a < 0)  return -1;
	else if (a > 31)return 0;
	else if (a == 0)return 1;
	else return(a * test01(a - 1));
}
int _stdcall test02(int a, int b)
{
	if (a >= b)return (a - b);
	else return (b - a);
}

 (8)添加定义文件,在源文件夹新建test.def

 

(9)写下如是代码

LIBRARY"Dlltest"
EXPORTS
test01 @1
test02 @2

(10)

先将解决方案切换到Release模式,再在CreateDLL项目名称上右击选择【生成】或【重新生成】

注:Release模式下生成的DLL才是最终的,Debug模式下生成的DLL有时会出问题

(11) 在解决方案所在的目录中打开Release文件夹即可看到生成的DLL

 

三:用C#项目调用C++创建DLL

(1)将C#项目设置为启动项目,并且将解决方案设置为Debug模式

 

(2)C#项目中定义Dllimport

class DllTest
        {
            [Dllimport(@"../../Release/Dlltest.dll", EntryPoint = "test01", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = false, CallingConvention = CallingConvention.StdCall)]
            public static extern int test01(int a);

            [Dllimport(@"../../Release/Dlltest.dll", EntryPoint = "test02", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = false, CallingConvention = CallingConvention.StdCall)]
            public static extern int test02(int a, int b);
        }

 (3)这样就可以在后面的程序中调用

 

四,c#代码全部
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace 新调用dll
{
    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();
        }
        class DllTest
        {
            [Dllimport(@"../../Release/Dlltest.dll", EntryPoint = "test01", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = false, CallingConvention = CallingConvention.StdCall)]
            public static extern int test01(int a);

            [Dllimport(@"../../Release/Dlltest.dll", EntryPoint = "test02", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = false, CallingConvention = CallingConvention.StdCall)]
            public static extern int test02(int a, int b);
        }


        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox2.Text == "")
            {
                MessageBox.Show("请输入求阶乘的数字!");
            }
            else
            {
                int r1 = DllTest.test01(int.Parse(textBox2.Text));
                textBox1.Text = r1.ToString();
            }
        }

        private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 0x20) e.KeyChar = (char)0;  //禁止空格键
            if ((e.KeyChar == 0x2D) && (((TextBox)sender).Text.Length == 0)) return;   //处理负数
            if (e.KeyChar > 0x20)
            {
                try
                {
                    double.Parse(((TextBox)sender).Text + e.KeyChar.ToString());
                }
                catch
                {
                    e.KeyChar = (char)0;   //处理非法字符
                }
            }
        }

        private void textBox3_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 0x20) e.KeyChar = (char)0;  //禁止空格键
            if ((e.KeyChar == 0x2D) && (((TextBox)sender).Text.Length == 0)) return;   //处理负数
            if (e.KeyChar > 0x20)
            {
                try
                {
                    double.Parse(((TextBox)sender).Text + e.KeyChar.ToString());
                }
                catch
                {
                    e.KeyChar = (char)0;   //处理非法字符
                }
            }
        }

        private void textBox5_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 0x20) e.KeyChar = (char)0;  //禁止空格键
            if ((e.KeyChar == 0x2D) && (((TextBox)sender).Text.Length == 0)) return;   //处理负数
            if (e.KeyChar > 0x20)
            {
                try
                {
                    double.Parse(((TextBox)sender).Text + e.KeyChar.ToString());
                }
                catch
                {
                    e.KeyChar = (char)0;   //处理非法字符
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if(textBox3.Text==""&&textBox5.Text=="")
            {
                MessageBox.Show("请输入求差的数据!");
            }
            else
            {
                textBox4.Text = DllTest.test02(int.Parse(textBox3.Text), int.Parse(textBox5.Text)).ToString();
            }
        }
    }
}

 

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/298091.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号