Re: Structure in CSharp



On Wed, 09 May 2007 18:49:01 -0700, Yatin Patel <YatinPatel@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote:

[...]
This for some reason is not working for LocalPhoneNumber field in the above structure. It gives runtime error which is as below:

Cannot marshal field 'MyStruct' of type 'R_OMNI_LINK_MESSAGE': The type
definition of this field has layout information but has an invalid
managed/unmanaged type combination or is unmarshalable.

Well, you're beyond me at this point. :) I haven't done enough of what you're trying to do to consider myself very helpful on the matter.

Being ignorant's never stopped me from commenting in the past though, so I'll point out a few of things I noticed:

One is that your _myUnion struct doesn't look to me to be the same as the original struct you've specified. You've got as its first field a single byte "_messageType", but the original struct has a 255-byte array named "Data". In addition, you've instructed C# to marshal that single byte as a 255 element array. If I were the C# compiler, I might complain about something like that too. :)

Another thing is that the _olmSYSTEM_INFORMATION struct doesn't look like anything in your original struct definition. This may or may not be a problem, but I am left wondering how you got from the original struct definition you posted to the C# declaration you've also posted.

Finally, in the same way that your declaration in "_myUnion" for the "_messageType" field looks odd to me, so too does the declaration for "LocalPhoneNumber" in the "_olmSYSTEM_INFORMATION" struct. It's a byte, but you're telling C# it's an array of length 25.

I figure there's one of two possibilities: either this is actually how you're supposed to do it, and C# has some really weird syntax that I don't understand; or, it's not how you're supposed to do it, and you're supposed to declare types in C# that are at least roughly the same as the unmanaged type you're trying to get. Honestly, either one of those is possible for all I know. But you might want to think about the possibility that it's the latter that's causing you trouble. :)

Pete
.



Relevant Pages

  • Re: How to pass a struct to a function
    ... write an expression evaluating to your struct -- the name of a variable ... you haven't declared a type "struct entry". ... about how this declaration is handled, ... on which compiler you're using, but any decent compiler will print ...
    (comp.lang.c)
  • Re: Passing Structure to a function
    ... > int Menu; ... 'struct' keyword above is because otherwise you got compiler errors. ... The reason for the errors is that phonerec hasn't been declared or ... A structure declaration merely says that a structure ...
    (comp.lang.cpp)
  • Re: syntax errror
    ... >>> int maxGrey' ... >> declaration of maxGrey. ... typedef double; ... struct some_tag_name { ...
    (comp.lang.c)
  • Re: mutually referential (Pg 140 K&R2)
    ... >In this example the vacuous declaration doesn't make any difference; ... >needed when you want a name in an inner scope to match a tag defined ... >in the inner scope instead of a tag from the outer scope. ... the vacuous declarations of struct ...
    (comp.lang.c)
  • Re: struct type completion
    ... # content is an incomplete type. ... We need to re-declare the identifiers to complete their type. ... With struct types this is a bit different: ... declaration, that's why sizeof object is okay after the second ...
    (comp.lang.c)