【发布时间】:2012-11-28 16:56:45
【问题描述】:
我正在尝试让一些 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
编辑:删除细节,添加错误截图。
【问题讨论】:
-
请提供您收到的错误消息并考虑在问题中添加问题。旁注:在您的帖子中添加任何问候/感谢信/签名是不“可以的” - 它们很少为问题增加重要价值。
-
编译代码时会发生什么?如果你能编译它,当你执行它时会发生什么?
-
哦,对不起。它无法从 dll 中找到引用的命名空间。
-
呃,看了@警告,发现问题;将框架更改为 4.0 感谢阅读,有人可以标记为已回答,否则我将在 7 小时内标记为已回答 :(