1

I am writing a wrapper for a C# .dll to be used by a c++ program. I have the functions working but I am not sure how to access/set-up the properties in the wrapper so they are accessible.

The properties are of all different data types, some are read only, some are protected writes, and some are read write.

I trimmed down my code some to show how I have things set up. But I can't figure out how to do the properties and have had no luck googling it. Maybe looking for the wrong term?

My code: c#:

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

    public Byte[] TagData
    {
        get;
        set;
    }

    public RFIDInterface()
    {
         ...
    }

    public bool Connect()
    {
         ...
    }

}

.H file:

class RFIDWrapperIFacePrivate;

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

public:
    // Constructors
    RFIDWrapperIFace();

    // destructors
    ~RFIDWrapperIFace();

    // Connection functions
    bool Connect();

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

.C file:

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
?????
  • Have you considered `[ComVisible]`? You'll get property support through the COM interface, and you won't have to deal with data marshalling...as much, anyway. I'm not the biggest fan of COM, but it would certainly be my goto approach for exposing C# code to C++. – zzxyz Feb 26 '18 at 19:11

1 Answers1

0

In the .NET Framework version 4+, runtime support has been removed for enforcing the Deny, RequestMinimum, RequestOptional, and RequestRefuse permission requests.

Security attributes to class would be somewhat.

[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;
}

As-per document "The security information declared by a security attribute is stored in the metadata of the attribute target and is accessed by the system at run time. Security attributes are used only for declarative security."

MSDN page have full list of available security attributes

Mandar
  • 1,006
  • 11
  • 28
  • Unfortunately I am unable to modify the C# code. It was given to me as a look, don't touch. –  Feb 28 '18 at 13:40