public struct in managed c++ dll cannot be seen from c# - c#

I have a c# application. It references a c++-cli dll. In the namespace of the c++ dll a public struct is declared:
namespace Wrapper {
public struct maxs
{
public:
char Name[255];
int num;
};
etc
There is a function in the c++ dll which returns a pointer to a pointer to the struct.
Wrapper::Maxs** Wrapper::Class1::CallMax();
The C# application references the c++ dll and declares an instance of the struct (unsafe code)
Wrapper.Class1 oMax = new Wrapper.Class1()
Wrapper.Maxs** maxs;
maxs= oMax.CallMax();
int num = oMax.m_num;
Then I try to access the fields of maxs like so:
for(int i = 0; i < num; ++i)
{
name = maxs[i]->name;
}
However, that 'name' field is not visible in the c# intellisense and doesn't compile.
Interestingly I went to see the definition of the struct in the c++ metadata and the struct is blank..
using System;
using System.Runtime.CompilerServices;
namespaceWrapper
{
[CLSCompliant(false)]
[NativeCppClass]
[UnsafeValueType]
public struct Maxs
{
}
}
Is that normal?
So the question is: Why can c# not see the fields of the public struct Maxs? And why is the metadata info show a blank struct?
thanks.

This is the correct answer AFAIK:
Because it is not a managed struct. You'll need to declare it as public value struct instead. – Hans Passant Mar 24 at 2:33

Related

Calling C++ Code with a struct from C#

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*
}
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];
};

How to initialize sbyte* in c#

I am building a c# application, in my application, I load a c++/cli dll, and calling its function.
I have declare a value class in my c++/cli class.
public value class S_OpenParam {
public :
int iPort;
char* szIpAddress;
int iBaudRate;
};
Then , I am trying to initialize my S_OpenParam in my c# application.
I am facing problem on initialize the char* szIpAddress
myObj.S_OpenParam sParam;
sParam.iBaudRate = 0;
sParam.iPort = 0;
When I try to assign a value to it:
sParam.szIpAddress = "127.0.0.1";
It shows the type is sbyte*
Do you know how to initialize it ?
Since you are in a C++/CLI dll, why don't you use String^ instead of char*, so that you will be able to update it from c# without problems ?
public value class S_OpenParam {
public :
int iPort;
String^ szIpAddress; <-- String^ instead of char*
int iBaudRate;
};

How to keep C struct references to a C# struct with Marshal or GCHandle?

I found several questions on this topic but nothing, which fits my exact problem. I'm having structs in a C DLL, for example like this one here:
extern "C"
{
struct CStruct
{
int alpha;
double beta;
};
}
CSharpStruct has the same structure as its C friend, like this:
public struct CSharpStruct
{
public int alpha;
public double beta;
}
and want to deliver the data by reference to a c# program.
I created a method in the C DLL which is able to return
a pointer to the struct like this one here: (test data)
const EXPORT_API CStruct* GetStruct()
{
CStruct1.alpha = 21;
CStruct1.beta = 23.141;
return &CStruct1;
}
and I am able to access the data in c# in this way:
[DllImport("ASimplePlugin")]
private static extern IntPtr GetStruct();
//...
IntPtr cStruct = GetStruct();
CSharpStruct test = (CSharpStruct) Marshal.PtrToStructure(cStruct, typeof(CSharpStruct));
After that I can access the data from C, but Marshal.PtrToStructure
copies them from the IntPtr reference into the C# struct by Value.
What I want to achieve is to have a direct reference to the c values
in the c# struct so that changing it in c# changes the values in c.
How is this possible?

How to convert these two unmanaged structures to managed types?

The first one in my c++ namespace is
public struct channel_vars {
int fetch_data; /* Boolean flag */
void * data; /* (malloc'd) address of data */
unsigned int lines; /* Number of lines returned */
}
I don't know what the void* is supposed to turn into when managed
The second one is
public struct hdf_call_vars_t {
struct channel_vars p_vars;
struct channel_vars s_vars;
enum FILE_VERSION file_vers; /* Set in top level sub. used in lower */
int fetch_n; /* Boolean flag */
s_line_header_t * n_addr; /* malloc'd address of ndata */
unsigned int n_lines;
csdt_file_header_t hdr;
};
In the one above, s_line_header_t is itself a struct which is made up of doubles and ints and another structure that is also made up of basic types like ints etc.
Since managed code doesn't allow pointers, how do I convert these structures to managed types? All this code is in my c++ -cli project.
thanks,
sb
You can use IntPtr class in managed code to map your field that uses pointer on unmaged code
If you are moving to c# All classes are functionally identical to pointers. They operate almost in the same way in that they can either point to a class or be null.
void* is a bit more tricky. This can point to anything. In c# all objects inherit from the base object class. So replace all void* with object and you should be fine, since you will need a cast to get back out of a both a void* and an object.
public struct ChannelVars {
public bool FetchData;
public object Data;
public uint Lines;
}
public struct HDFCallVarsT {
public ChannelVars PVars;
public ChannelVars SVars;
//enum FILE_VERSION file_vers; //you will need to get the enum set here correctly
public bool FetchN;
public SLineHeaderT NAddr; //SLineHeaderT must be a class somewhere
public uint NLines;
public CSDTFileHeaderT HDR; //CSDTFileHeaderT must be a class somewhere
};

Marshall an array of structs

I am calling a C++ function from C#. As arguments it receives a pointer to an array of structs.
struct A
{
int data;
}
int CFunction (A* pointerToFirstElementOfArray, int NumberOfArrayElements)
In C# I have created the same struct (as a class) and I marshall it correctly (the first element in the array is received correctly). Here is my C# definition of the C++ struct:
[StructLayout(LayoutKind.Sequential), Serializable]
class A
{
int data;
}
The first element is read correctly so I know all the fields in the struct are marshalled correctly. The problem occurs when I try to send over an array of elements. How do I create an array of classes that will be in a single memory block (chunk), so the C++ function can increment the pointer to the array?
I guess I would need something similar to stackalloc, however I belive that only works for primitive types?
Thank you for your help.
This is not intended to be a definitive answer, but merely an example of you could accomplish this using stackalloc and unsafe code.
public unsafe class Example
{
[DllImport("library.dll")]
private static extern int CFunction(A* pointerToFirstElementOfArray, int NumberOfArrayElements);
public void DoSomething()
{
A* a = stackalloc A[LENGTH];
CFunction(a, LENGTH);
}
}
Also, pay attention to the packing of the struct that the API accepts. You may have to play around with the Pack property of the StructLayout attribute. I believe the default is 4, but some APIs expect 1.
Edit:
For this to work you will have to change the declaration of A from a class to a struct.
public struct A
{
public int data;
}

Categories

Resources