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

计算输入数据的阶乘,判断输入的合法性
计算2个输入数据a和b的差(a-b),若a<b则需先交换a,b的值再计算

一:创建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();
            }
        }
    }
}

 

 

  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值