c# - 在 C# 中使用 C CLR::Class Library

标签 c# c++ dll c++-cli clr

我正在尝试让一些 C++ 代码在 C# 下工作。我一直在尝试在所有地方按照在线教程进行操作,但没有任何运气来编译或执行代码。

我编写了一个可编译为 .dll 的基本 C++ 类和一个 C# 控制台应用程序。 - 我已将 C++ dll 上的平台工具集更改为 v100 - 我在我的 C# 应用程序中添加了对 dll 的引用

请注意,我正在尝试使用 C++ 类,而不是 C++ 静态函数...在代码中!

// CTrans.h
#pragma once

#include "windows.h"

using namespace System;

namespace CTrans {

    public ref class CTransClass
    {
    public:
    System::String^ helloWorld();
    };
}

// CTrans.cpp
// This is the main DLL file.
#include "stdafx.h"
#include "CTrans.h"

String^ CTrans::CTransClass::helloWorld()
{
    return gcnew System::String("Hello World!");
}

// Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CTWrapper
{
    class Program
    {
        static void Main(string[] args)
        {
            unsafe
            {
                CTrans.CTransClass trans = new CTrans.CTransClass();
                String tmp = trans.helloWorld();
            }
        }
    }
}

错误:http://sumarlidason.com/Capture.PNG

编辑:删除细节,添加错误截图。

最佳答案

您是否尝试添加“using CTrans;”使用 block 到 Program.cs 的顶部?

检查以确保程序集是使用相同的体系结构 (x86/x64) 编译的,并且错误列表中没有其他相关警告。

关于c# - 在 C# 中使用 C CLR::Class Library,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13610366/

相关文章:

c# - Blazor 中有没有办法提示用户浏览到本地文件夹并选择它来下载文件?

c++ - 相同大小的二维和一维数组之间的内存或执行问题?

visual-studio-2010 - 使用 Visual Studio C++ 2010 中的命令行创建 DLL 和 LIB 文件

c++ - 将单例实例共享到共享库而不导出它

c# - 无法将 Entity Framework 连接到本地 SQL Server Express

c# - 任何可以从 aiff 文件解析 ID3 block 的库?

时间:2019-03-17 标签:c#Screna: How do I define screen area?

c++ - 程序以代码 132 退出

c++ - 我应该将项目的哪些文件放入 repo

c++ - 低版本visual studio构建.dll