gpt4 book ai didi

c# - 如何使用 C++ DLL 中的 C# 代码

转载 作者:行者123 更新时间:2023-11-30 03:38:41 26 4
gpt4 key购买 nike

<分区>

我有一个要求,要求使用从 C++ dll 导出的函数。

导出的函数中需要发生很多事情,但我不想重写我为此编写的所有 C# 代码。

我只想将 C# 代码粘贴到 DLL 中并完成。

注意:我不想调用 C# DLL,我想将 C# 代码放入 C++ dll。

这是 Exports.def 文件:

LIBRARY InstallCheckWin32
EXPORTS
IsConnectionPointValid @1
fnTest @2

这是我的 DLL .h 文件:

// The following ifdef block is the standard way of creating macros which make exporting 
// from a DLL simpler. All files within this DLL are compiled with the INSTALLCHECKWIN32_EXPORTS
// symbol defined on the command line. This symbol should not be defined on any project
// that uses this DLL. This way any other project whose source files include this file see
// INSTALLCHECKWIN32_API functions as being imported from a DLL, whereas this DLL sees symbols
// defined with this macro as being exported.
#ifdef INSTALLCHECKWIN32_EXPORTS
#define INSTALLCHECKWIN32_API __declspec(dllexport)
#else
#define INSTALLCHECKWIN32_API __declspec(dllimport)
#endif


INSTALLCHECKWIN32_API void CallCSharp();

这是 .cpp 文件:

// InstallCheckWin32.cpp : Defines the exported functions for the DLL application.
//

#include "stdafx.h"
#include "InstallCheckWin32.h"
#include <tchar.h>


INSTALLCHECKWIN32_API void CallCSharp()
{
// this is where I want to use C# objects
// eg:
DateTime now = DateTime.Now;
}

我已将通用配置属性“公共(public)语言运行时支持”设置为公共(public)语言运行时支持(/clr)

我还需要设置什么才能在 C++ dll 中使用 C# 代码?

谢谢

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com