如何共享C#和C ++代码之间保持恒定? [英] How do I share a constant between C# and C++ code?

查看:178
本文介绍了如何共享C#和C ++代码之间保持恒定?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写使用C#和WCF一个和C ++ WWSAPI第二两个过程。我希望能够以定义的地址在一个地方被用于通信的两者之间,并有C#和Visual C ++使用它。这可能吗?

I'm writing two processes using C# and WCF for one and C++ and WWSAPI for the second. I want to be able to define the address being used for communication between the two in a single place and have both C# and C++ use it. Is this possible?

在一个IDL我来是定义常量最接近的,然后使用MIDL和TLBIMP得到它为可以由C#消耗一个DLL 。然而,这似乎并没有暴露不变,或者至少我无法弄清楚如何使它这样做。也许它仅限于只键入定义。

The closest I've come is defining the constant in an IDL, then using MIDL and TLBIMP to get it into a DLL that can be consumed by C#. However this doesn't seem to expose the constant, or at least I can't figure out how to make it do so. Maybe it is limited to type definitions only.

任何其他建议?

推荐答案

C#和C ++有不同的模型常数。典型地,该常数甚至不会在所得的C ++二进制发射 - 在需要的大部分时间它的自动替换

C# and C++ have differing models for constants. Typically, the constant won't even be emitted in the resulting C++ binary -- it's automatically replaced where it is needed most of the time.

而不是使用恒定的,做一个函数返回常量,你可以p / Invoke从C#。

Rather than using the constant, make a function which returns the constant, which you can P/Invoke from C#.

因此,

#include <iostream>
const double ACCELERATION_DUE_TO_GRAVITY = 9.8;
int main()
{
     std::cout << "Acceleration due to gravity is: " << 
         ACCELERATION_DUE_TO_GRAVITY;
}



变为

becomes

#include <iostream>
extern "C" double AccelerationDueToGravity()
{
    return 9.8;
}
int main()
{
     std::cout << "Acceleration due to gravity is: " << 
         AccelerationDueToGravity();
}



你应该可以的P / Invoke从C#。

which you should be able to P/Invoke from C#.

这篇关于如何共享C#和C ++代码之间保持恒定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆