6

I was given a third party library that wraps unmanaged C++ code into a C# api, one of the functions has a parameter that appears to be a struct from the native global namespace how do we create an instance of that struct in C#?

This is the c++ Struct:

struct GroupInfo
{
    int                 cMembers;                                           // Current # of members in the group.
    char                saMembers[cMaxMembers][cMaxLoginID + 1];            // Members themselves.
};

When we try to declare an instance of it in C# the compiler says global::GroupInfo is not available due to its protection level.

c++ signature

int QueryGroup(char* sGroupName,
    GroupInfo& gi);

C# signature

VMIManaged.QueryGroup(sbyte*, GroupInfo*)

I have a class called group info

class GroupInfo
{
    public int cMembers;
    public sbyte[,] saMembers;
}

and when i try to implement that using this code i get a cannot convert error

GroupInfo gi = new GroupInfo();

unsafe
{
    sbyte* grpName;
    fixed (byte* p = groupNameBytes)
    { 
        grpName = (sbyte*)p;
    }
    return vmi.QueryGroup(grpName, gi); // cannot convert from class GroupInfo to GroupInfo*
}
4
  • From the few snippets you have provided it is hard to understand what the issue is exactly. Can you provide the line of code that is failing? Commented Nov 30, 2017 at 22:26
  • thanks for the comment mageos i have modified the post with the line that is failing. Commented Dec 1, 2017 at 2:37
  • Have you tried vmi.QueryGroup(grpName, &gi);? Commented Dec 1, 2017 at 14:33
  • I did and i get a "cannot take the address of, get the size of, or declare a pointer to a managed type (GroupInfo)" Commented Dec 1, 2017 at 14:56

1 Answer 1

2

You are most likely getting the error because of the default protection level of the default constructor in C# for your GroupData class. If it is defined in a different file from the file in which you are trying to use it, defining it like this should work:

class GroupInfo
{
  public GroupInfo() {}
  public int cMembers;
  public sbyte saMembers[cMaxMembers][cMaxLoginID + 1];
};
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.