C# 调用VC++的DLL,VC++封装DLL

VS中新建一个动态库项目

文件生成一个工程名对应的.cpp文件,该文件定义 DLL应用程序的导出函数。

工程内新建一个类OutputInt,我用类向导生成,工程中会添加OutputInt.cpp和OutputInt.h两个文件,

在.h文件中声明函数

#pragma once
class OutputInt
{
public:
    OutputInt();
    ~OutputInt();
    
    static _declspec(dllexport)  int TestFunction();
};

在.cpp文件中实现 构造函数、析构函数和 自定义函数

#include "stdafx.h"
#include "OutputInt.h"


OutputInt::OutputInt()
{
}


OutputInt::~OutputInt()
{
}


int OutputInt::TestFunction()
{
    return 111;
}

类和头文件都实现了。

 

 

 

然后在Win32Project1.cpp中定义对外暴露的函数

#include "stdafx.h"
#include "OutputInt.h"

extern "C" __declspec(dllexport) int TestFunction()
{
    OutputInt outPut;
    return outPut.TestFunction();
}

编译,生成对应工程项目名的DLL文件,动态链接库生成完成。

 

在C# WPF应用程序中调用以上生成的DLL

把生成的DLL文件拷贝到 WPF工程的DEBUG文件夹下,调用方式:

[DllImport("Win32Project1.dll", ExactSpelling = false)]
 public static extern int TestFunction();

 

调用完成。

 

转载于:https://www.cnblogs.com/pangkang/p/5856144.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值