C#调用C++动态库dll

  • cpp文件,生成dll主文件
// waveletDenoise.cpp : Defines the exported functions for the DLL application.
//

#include "stdafx.h"
#include "Wavelet.h"
#include "algorithm" 
#include "WaveletFilter.h"

using namespace wavelet;

bool WaveletFilter(double* Input, double* Output, int signal_size)
{
        ***********
        ***********
return true;
}
  • 头文件
#ifndef WAVELETFILTER_H
#define WAVELETFILTER_H

extern "C"
{
_declspec(dllexport) bool WaveletFilter(double* Input, double* Output, int signal_size);
}

#endif
  • C# 调用接口
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;

namespace wavelet_DLL_CSharp
{
    class waveletDenoiseHeader
    {
        [DllImport("waveletDenoise.dll", CallingConvention = CallingConvention.Cdecl)]   // C declaration calling convention
        public static extern bool WaveletFilter(double[] Input, double[] Output, int signal_size);
    }
}
  • 测试
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;

namespace wavelet_DLL_CSharp
{
    class Program
    {
    // 也可以不新建类,给出以下声明即可,dll文件要放在bin目录里哟。
    //    [DllImport("waveletDenoise.dll", CallingConvention = CallingConvention.Cdecl)]   // C declaration calling convention
    //    public static extern bool WaveletFilter(double[] Input, double[] Output, int signal_size);
        static void Main(string[] args)
        {
            double[] signal = new double[8] { 420.2, 423.53, 423.52, 423.35, 424.52, 428, 430.79, 428.92 };
            double[] dsignal = new double[8];
            waveletDenoiseHeader.WaveletFilter(signal, dsignal, 8);
            Console.WriteLine("signal: {0}", signal[0]);
            Console.WriteLine("dsignal: {0}", dsignal[0]);
            Console.ReadKey();
        }
    }
}

补充说明:二维数据没法传到C#里的哦,需要转换成一维数组。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值