如何声明常量字符串以在非托管 C++ 中使用? dll 和 C# 应用程序中?

发布于 2024-08-25 09:22:01 字数 448 浏览 5 评论 0原文

目前,我在启动时通过回调将 const 字符串值从 C++ 传递到 C# 中,但我想知道是否有一种方法可以在 C++ 头文件中定义它们,然后我也可以在 C# 中引用它们。

我已经用枚举做到了这一点,因为它们很简单。 我在我的 C++ 库项目(通过顶部带有编译指示的 .h 文件)和我的 C# 应用程序(作为链接)中都包含一个文件:

#if _NET
public
#endif
enum ETestData
{
    First,
    Second
};

我知道这听起来很混乱,但它有效:)

但是...我怎样才能对字符串常量做同样的事情 - 我最初认为平台之间的语法差异太大,但也许有办法?

使用涉及 #if _NET、#defines 等的巧妙语法?

使用资源文件?

使用 C++/CLI 库?

有什么想法吗?

Curently I'm passing my const string values up from my C++ into my C# at startup via a callback, but I'm wondering if there's a way of defining them in a C++ header file that I can then also refer to in C#.

I already do this with enums as they are easy.
I include a file in both my C++ library project (via a .h file with a pragma once at the top), and my C# application (as a link):

#if _NET
public
#endif
enum ETestData
{
    First,
    Second
};

I know it sounds messy, but it works :)

But...how can I do the same with string constants - I'm initially thinking the syntax is too different between the platforms, but maybe there's a way?

Using clever syntax involving #if _NET, #defines etc?

Using resource files?

Using a C++/CLI library?

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

攒眉千度 2024-09-01 09:22:02

C# 字符串常量采用以下形式:

public const string MyString = "Hello, world";

我认为 C++ 中的首选方式是:

const std::string MyString ="Hello, world";

C# 中的 string 只是 .NET 类型 String 的别名。一种方法是创建一个 C++ #define:

#define String const std::string

你的通用代码将如下所示:

   // at the beginning of the file
   #if !_NET
   #define String const std::string
   #endif

   // For each string definition
   #if _NET
   public const
   #endif
   String MyString = "Hello, world";

我必须承认我还没有尝试过,但看起来它会起作用。

A C# string constant would take the form:

public const string MyString = "Hello, world";

I think the preferred way in C++ is:

const std::string MyString ="Hello, world";

string in C# is just an alias for the .NET type, String. One way to do this would be make a C++ #define:

#define String const std::string

And your common code would look like this:

   // at the beginning of the file
   #if !_NET
   #define String const std::string
   #endif

   // For each string definition
   #if _NET
   public const
   #endif
   String MyString = "Hello, world";

I have to admit that I haven't tried it, but it looks like it'll work.

南冥有猫 2024-09-01 09:22:02

你可以说我很有趣,但我认为最好的方法是使用 C++/CLI 和 C++。

这使您可以将相同的字符串 #include 到两个不同的上下文中,并让编译器发挥作用。
这将为您提供字符串数组,

// some header file
L"string1",
L"string2",
L"string3",

// some C++ file
static wchar_t*[] string = {
#include "someheaderfile.h"
};

// in some C++/CLI file
array<String^>^ myArray = gcnew array<String^> {
#include "someheaderfile.h"
};

否则您可以直接使用 C 预处理器:

// in somedefineset
#define SOME_STRING_LITERAL L"whatever"

// in some C++ file
#include "somedefineset.h"
const wchar_t *kSomeStringLiteral = SOME_STRING_LITERAL

// in some C++/CLI file
literal String ^kSomeStringLiteral = SOME_STRING_LITERAL;

Call me funny, but I think the best way to do this is using C++/CLI and C++.

This lets you #include the same strings into two different contexts and let the compiler do the magic.
This will give you arrays of strings

// some header file
L"string1",
L"string2",
L"string3",

// some C++ file
static wchar_t*[] string = {
#include "someheaderfile.h"
};

// in some C++/CLI file
array<String^>^ myArray = gcnew array<String^> {
#include "someheaderfile.h"
};

otherwise you can use the C preprocessor straight out:

// in somedefineset
#define SOME_STRING_LITERAL L"whatever"

// in some C++ file
#include "somedefineset.h"
const wchar_t *kSomeStringLiteral = SOME_STRING_LITERAL

// in some C++/CLI file
literal String ^kSomeStringLiteral = SOME_STRING_LITERAL;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文