C++ struct transparency in C#

U

Urs Wigger

In a C++ project, I have the following struct definition:

struct GridModeDataT
{
double dVal1;
double dVal2;
double dVal3;
long lNumberOfPoints;
int bUseRegion;
enum ES_RegionType regionType;
};

I did a Managed C++ wrapper class around my C++ project. This MC++
wrapper defines a virtual function 'OnGetGridModeParamsAnswer()', which
uses a parameter of the above struct type.

Finally, in a C# project, I inherit from the class in the MC++ project
and override the virtual function, as shown here:

public override void OnGetGridModeParamsAnswer(GridModeDataT gridModeData)
{
Console.WriteLine("Derived OnGetGridModeParamsAnswer() was called");
}

Everything compiles and runs correctly. If I look to 'gridModeData'
within the debuggers 'Watch' window, the struct members are correctly
resolved and assigned. For example it looks as follows in the watch window:

gridModeData {GridModeDataT} GridModeDataT
bUseRegion 0 int
dVal1 1.0 double
dVal2 2.0 double
dVal3 3.0 double
lNumberOfPoints 4 int
regionType 1 ES_RegionType

Now the problem: If I try to reference the struct members in C# code,
the compiler says there are no such members:

public override void OnGetGridModeParamsAnswer(GridModeDataT
gridModeData)
{
double dVal1 = gridModeData.dVal1; // compiler says:
'GridModeDataT' does not contain a definition for 'dVal1'

Console.WriteLine("Derived OnGetGridModeParamsAnswer() was called");
}

The 'intellisense' only offers the the following methods on
gridModeData: 'Equals', 'GetHashCode', 'GetType', 'ToString'.
I don't understand why I can't access the 'original' struct members,
although the debugger can 'see' them.

How do I have to work around this problem?

Any help is appreciated.
Regards Urs
 
D

Daniel O'Connell

Urs Wigger said:
In a C++ project, I have the following struct definition:

struct GridModeDataT
{
double dVal1;
double dVal2;
double dVal3;
long lNumberOfPoints;
int bUseRegion;
enum ES_RegionType regionType;
};

I did a Managed C++ wrapper class around my C++ project. This MC++
wrapper defines a virtual function 'OnGetGridModeParamsAnswer()', which
uses a parameter of the above struct type.

Finally, in a C# project, I inherit from the class in the MC++ project
and override the virtual function, as shown here:

public override void OnGetGridModeParamsAnswer(GridModeDataT gridModeData)
{
Console.WriteLine("Derived OnGetGridModeParamsAnswer() was called");
}

Everything compiles and runs correctly. If I look to 'gridModeData'
within the debuggers 'Watch' window, the struct members are correctly
resolved and assigned. For example it looks as follows in the watch window:

gridModeData {GridModeDataT} GridModeDataT
bUseRegion 0 int
dVal1 1.0 double
dVal2 2.0 double
dVal3 3.0 double
lNumberOfPoints 4 int
regionType 1 ES_RegionType

Now the problem: If I try to reference the struct members in C# code,
the compiler says there are no such members:

public override void OnGetGridModeParamsAnswer(GridModeDataT
gridModeData)
{
double dVal1 = gridModeData.dVal1; // compiler says:
'GridModeDataT' does not contain a definition for 'dVal1'

Console.WriteLine("Derived OnGetGridModeParamsAnswer() was called");
}

The 'intellisense' only offers the the following methods on
gridModeData: 'Equals', 'GetHashCode', 'GetType', 'ToString'.
I don't understand why I can't access the 'original' struct members,
although the debugger can 'see' them.

How do I have to work around this problem?
I know this is a stupid question...but are the fields public?
 
U

Urs Wigger

Thanks for the answer. I don't agree this is a stupid question. But I
think the compiler would come up with some kind of
'cannot access private members' error message. I thought struct members
in C++ are all public by default. Anyway, I tried with

struct GridModeDataT
{
public:
double dVal1;
double dVal2;
double dVal3;
long lNumberOfPoints;
int bUseRegion;
}

but it didn't solve my problem.
 
D

Daniel O'Connell

Hmm, I'm just learning Managed C++ myself, so I don't know for sure what the
defaults are(I'm in the explicitly declare access camp when it comes to C++
because its so easy), but a few things that may be the issue. Is
GridModeDataT a managed struct? (either using __gc or __valuetype, I'm not
sure, like I said, I'm still learning) and can you access the members from a
derived class written in C++?



Thanks for the answer. I don't agree this is a stupid question. But I think
the compiler would come up with some kind of
'cannot access private members' error message. I thought struct members in
C++ are all public by default. Anyway, I tried with

struct GridModeDataT
{
public:
double dVal1;
double dVal2;
double dVal3;
long lNumberOfPoints;
int bUseRegion;
}

but it didn't solve my problem.


Daniel O'Connell wrote:


In a C++ project, I have the following struct definition:

struct GridModeDataT
{
double dVal1;
double dVal2;
double dVal3;
long lNumberOfPoints;
int bUseRegion;
enum ES_RegionType regionType;
};

I did a Managed C++ wrapper class around my C++ project. This MC++
wrapper defines a virtual function 'OnGetGridModeParamsAnswer()', which
uses a parameter of the above struct type.

Finally, in a C# project, I inherit from the class in the MC++ project
and override the virtual function, as shown here:

public override void OnGetGridModeParamsAnswer(GridModeDataT

gridModeData)

{
Console.WriteLine("Derived OnGetGridModeParamsAnswer() was called");
}

Everything compiles and runs correctly. If I look to 'gridModeData'
within the debuggers 'Watch' window, the struct members are correctly
resolved and assigned. For example it looks as follows in the watch

window:

gridModeData {GridModeDataT} GridModeDataT
bUseRegion 0 int
dVal1 1.0 double
dVal2 2.0 double
dVal3 3.0 double
lNumberOfPoints 4 int
regionType 1 ES_RegionType

Now the problem: If I try to reference the struct members in C# code,
the compiler says there are no such members:

public override void OnGetGridModeParamsAnswer(GridModeDataT
gridModeData)
{
double dVal1 = gridModeData.dVal1; // compiler says:
'GridModeDataT' does not contain a definition for 'dVal1'

Console.WriteLine("Derived OnGetGridModeParamsAnswer() was called");
}

The 'intellisense' only offers the the following methods on
gridModeData: 'Equals', 'GetHashCode', 'GetType', 'ToString'.
I don't understand why I can't access the 'original' struct members,
although the debugger can 'see' them.

How do I have to work around this problem?


I know this is a stupid question...but are the fields public?


Any help is appreciated.
Regards Urs
 
M

Morten Wennevik

Not sure if C++ works the same but in C# you need to specify every single
field as public

struct GridModeDataT
{
public double dVal1;
public double dVal2;
public double dVal3;
public long lNumberOfPoints;
public int bUseRegion;
}

Anything not made public is treated as private.
 
U

Urs Wigger

Hi Daniel,

No, GridModeDataT is not a managed struct. My intention was to use C/C++
legay code unchanged in C# as far as possible. Although I had a bad
feeling about this, I just tried. And I wondered why neither the
compiler complained about the signature of
OnGetGridModeParamsAnswer(GridModeDataT gridModeData), nor there was a
problem at runtime. Memory layout of gridModeData is as expected (when
looked at it thru debugger) when OnGetGridModeParamsAnswer gets called.
The only problem is that I cannot access the struct members in C# code
syntactically. I'm afraid I'll have to wrap all my legacy C++ structs to
managed C++ structs ?
 
D

Daniel O'Connell

Urs Wigger said:
Hi Daniel,

No, GridModeDataT is not a managed struct. My intention was to use C/C++
legay code unchanged in C# as far as possible. Although I had a bad
feeling about this, I just tried. And I wondered why neither the
compiler complained about the signature of
OnGetGridModeParamsAnswer(GridModeDataT gridModeData), nor there was a
problem at runtime. Memory layout of gridModeData is as expected (when
looked at it thru debugger) when OnGetGridModeParamsAnswer gets called.
The only problem is that I cannot access the struct members in C# code
syntactically. I'm afraid I'll have to wrap all my legacy C++ structs to
managed C++ structs ?
You probably will, I would guess the C++ compiler handles that through IJW,
even though it doesn't work...I dunno, I'm still learning like I said, ;).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top