469,306 Members | 1,911 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,306 developers. It's quick & easy.

C++ struct transparency in C#

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

Nov 15 '05 #1
7 1669

"Urs Wigger" <ns_urs.wigger@leica-geosystems_ns.com> wrote in message
news:Ow****************@TK2MSFTNGP10.phx.gbl...
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

Nov 15 '05 #2
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:
"Urs Wigger" <ns_urs.wigger@leica-geosystems_ns.com> wrote in message
news:Ow****************@TK2MSFTNGP10.phx.gbl...

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



Nov 15 '05 #3
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++?

"Urs Wigger" <ns_urs.wigger@leica-geosystems_ns.com> wrote in message
news:%2***************@tk2msftngp13.phx.gbl...
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:

"Urs Wigger" <ns_urs.wigger@leica-geosystems_ns.com> wrote in message
news:Ow****************@TK2MSFTNGP10.phx.gbl...

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



Nov 15 '05 #4
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.

--
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
Nov 15 '05 #5
Urs,
How do I have to work around this problem?


Try adding the __value keyword

__value struct GridModeDataT

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.
Nov 15 '05 #6
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 ?
Daniel O'Connell wrote:
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++?

"Urs Wigger" <ns_urs.wigger@leica-geosystems_ns.com> wrote in message
news:%2***************@tk2msftngp13.phx.gbl...
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:

"Urs Wigger" <ns_urs.wigger@leica-geosystems_ns.com> wrote in message
news:Ow****************@TK2MSFTNGP10.phx.gbl...

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




Nov 15 '05 #7

"Urs Wigger" <ns_urs.wigger@leica-geosystems_ns.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
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, ;).

Daniel O'Connell wrote:
Hmm, I'm just learning Managed C++ myself, so I don't know for sure what thedefaults 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 notsure, like I said, I'm still learning) and can you access the members from aderived class written in C++?

"Urs Wigger" <ns_urs.wigger@leica-geosystems_ns.com> wrote in message
news:%2***************@tk2msftngp13.phx.gbl...
Thanks for the answer. I don't agree this is a stupid question. But I thinkthe compiler would come up with some kind of
'cannot access private members' error message. I thought struct members inC++ 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:

"Urs Wigger" <ns_urs.wigger@leica-geosystems_ns.com> wrote in message
news:Ow****************@TK2MSFTNGP10.phx.gbl...

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



Nov 15 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Cecile Muller | last post: by
57 posts views Thread by Piotr Wolski | last post: by
4 posts views Thread by Matthias Czapla | last post: by
3 posts views Thread by Mr. x | last post: by
20 posts views Thread by Nathan Sokalski | last post: by
6 posts views Thread by tommaso.gastaldi | last post: by
reply views Thread by ZikO | last post: by
3 posts views Thread by Martijn Mulder | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by harlem98 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.