【问题标题】:How to access C# .dll Properties when using a C++ wrapper使用 C++ 包装器时如何访问 C# .dll 属性
【发布时间】:2018-02-26 18:58:24
【问题描述】:

我正在为 C# .dll 编写一个包装器,供 c++ 程序使用。我有这些功能,但我不确定如何访问/设置包装器中的属性,以便它们可以访问。

属性是所有不同的数据类型,有些是只读的,有些是受保护的写入,有些是读写。

我精简了一些代码以显示我是如何设置的。但我不知道如何做这些属性,也没有运气用谷歌搜索它。也许是在寻找错误的术语?

我的代码: 时间:

public class RFIDInterface
{
    public bool TagDetected
    {
        get;
        private set;
    }

    public Byte[] TagData
    {
        get;
        set;
    }

    public RFIDInterface()
    {
         ...
    }

    public bool Connect()
    {
         ...
    }

}

.H 文件:

class RFIDWrapperIFacePrivate;

class __declspec(dllexport) RFIDWrapperIFace
{
private:
    RFIDWrapperIFacePrivate* _private;

public:
    // Constructors
    RFIDWrapperIFace();

    // destructors
    ~RFIDWrapperIFace();

    // Connection functions
    bool Connect();

    // properties
    ???????
};

.C 文件:

class RFIDWrapperIFacePrivate
{
public:
    msclr::auto_gcroot<RF182CInterface::RFIDInterface^> rf182CInterface;
};

// default constructor
RFIDWrapperIFace::RFIDWrapperIFace()
{
    _private = new RFIDWrapperIFacePrivate();
    _private->rf182CInterface = gcnew RF182CInterface::RFIDInterface();
}

// deconstructor
RFIDWrapperIFace::~RFIDWrapperIFace()
{
    delete _private;
}

// Connects to the RF182C device
// returns: True if successful
bool RFIDWrapperIFace::Connect()
{
    return _private->rf182CInterface->Connect();
}

// Transmits a string to the RF182C device
// Message: the message to transmit
// returns: True if successful
bool RFIDWrapperIFace::TransmitMessage(const char* Message)
{
    return _private->rf182CInterface->TransmitMessage(gcnew System::String(Message));
}

// properties
?????

【问题讨论】:

  • 你考虑过[ComVisible]吗?您将通过 COM 接口获得属性支持,并且您不必处理数据编组……无论如何。我不是 COM 的最大粉丝,但它肯定是我将 C# 代码暴露给 C++ 的首选方法。

标签: c# c++ command-line-interface wrapper


【解决方案1】:

在 .NET Framework 版本 4+ 中,已删除运行时支持以强制执行 Deny、RequestMinimum、RequestOptional 和 RequestRefuse 权限请求。

类的安全属性会有所不同。

[ComVisible(true)]
//[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public class RFIDInterface
{
    //[MarshalAs(UnmanagedType.Bool)]
    public bool TagDetected;
    {
        // No attribute, default access
        get;

        [SecurityPermissionAttribute(SecurityAction.Deny, UnmanagedCode = true)] 
        private set;
    }

    [MarshalAs(UnmanagedType.BStr)]
    public string TagData;
}

按文档“安全属性声明的安全信息存储在属性目标的元数据中,并在运行时由系统访问。安全属性仅用于声明性安全。”

MSDN page 拥有可用安全属性的完整列表

【讨论】:

  • 不幸的是,我无法修改 C# 代码。它是给我看的,不要碰。
猜你喜欢
  • 1970-01-01
  • 2011-01-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多