C++非托管调用托管dll

本文介绍如何通过定义托管C++头文件来实现托管与非托管C++之间的交互,包括混合编译模式设置、头文件及库文件引入方式、数据类型对应表等内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

如要保证调用托管dll,需定义好托管C++的头文件。
1.托管C++设置为混合编译模式。
2.头文件中要保证引入C++非托管库的头文件,如:
#include
#pragma comment( lib, "comsupp.lib" )
#pragma comment(lib, "comsuppw.lib")
#pragma comment(lib, "kernel32.lib")

#pragma pack(1)

#include
#include
using namespace std;
等,不可引入托管的命名空间,如
using system;
等。
3.在cpp文件中可引入托管库,如system等。
4.在头文件中需要定义类的成员变量时,可采用void *定义,
在cpp文件中,用到此指针时,reinterpret_cast*>(pYourPoint)
去转换指针。
5.在头文件中不要出现托管的数据类型。
按着以上原则,将此头文件以及托管dll,lib文件交给非托管C++工程,就可以使用了。

以下为网上所查得对应表:
Win32 API 非托管 C 语言类型 托管类名 说明
handlevoid*System.IntPtr32位
byteunsigned charSystem.Byte8位
shortshortSystem.Int1616位
wordunsigned charSystem.UInt1616位
intintSystem.UInt3232位
uintusigned intSystem.UInt3232位
longlongSystem.UInt3232位
boollongSystem.UInt3232位
dwordunsigned longSystem.UInt3232位
ulongunsigned longSystem.UInt3232位
charunsigned longSystem.CharANSI
lpstrchar*System.String或System.StringBuilderANSI
lpcstrconst char*System.String或System.StringBuilderANSI
lpwstrwchar_t*System.String或System.StringBuilderUnicode
lpcwstrconst wchar_t*System.String或System.StringBuilderUnicode
floatfloatSystem.Single32位
doubledoubleSystem.Double64位
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值