PInvoke - Structures

  • Thread starter Thread starter Mythran
  • Start date Start date
M

Mythran

[StructLayout(LayoutKind.Sequential)]
struct MyStruct
{
public int Number;
}

[StructLayout(LayoutKind.Sequential)]
struct MyStruct
{
private int mNumber;
public int Number
{
get {
return mNumber;
}
set {
mNumber = value;
}
}
}

For PInvoke calls, are the two structures above interchangeable? If a
structure is defined as the first MyStruct, can I use an instance of the 2nd
MyStruct?

Thanks,
Mythran
 
Mythran said:
For PInvoke calls, are the two structures above interchangeable? If a
structure is defined as the first MyStruct, can I use an instance of the
2nd MyStruct?

I dunno, give it a try and see. You'll have your answer in a minute or so.

Michael
 
Michael C said:
I dunno, give it a try and see. You'll have your answer in a minute or so.

Michael

I did try and the results are...confusing :)

I created a struct for RECT and used properties instead of members. It
worked fine. When I put Console.WriteLine inside the getter for Left, the
returned value for the function I used returned an empty
structure...repeatedly.

<shrug>

Mythran
 
Mythran said:
I did try and the results are...confusing :)

I created a struct for RECT and used properties instead of members. It
worked fine. When I put Console.WriteLine inside the getter for Left, the
returned value for the function I used returned an empty
structure...repeatedly.

I don't quite get what you mean here?
 
Michael C said:
I don't quite get what you mean here?

I don't remember which api call I made, but it returns a code that you can
use to identify what the contents of RECT is. I set it up using the normal
RECT layout, and it worked fine (the return code was the code that said it
had data in it). When I changed to properties, it also worked fine. Once I
put in a Console.WriteLine inside the get {} part of the Left property, the
return code was the code the meant there was no data in the RECT structure
(the RECT was empty).

Mythran
 
Ok, I tried this (no code included, just letting ya know what I tried).

In my Left property for my struct that contains public properties instead of
public members, I put a Console.WriteLine("HERE"); after I set the private
member, mLeft. When I pass this structure to the API function, the
structure is returned (out parameter) with data in it. The
Console.WriteLine() call is never called though (no "HERE" on console
window). The Left value is set though. It could be that it just places the
values in memory at the locations of the first 4 members, I'd have to give
it a try and test it out. But it's not that important :)

Mythran
 
Mythran said:
Ok, I tried this (no code included, just letting ya know what I tried).

In my Left property for my struct that contains public properties instead
of public members, I put a Console.WriteLine("HERE"); after I set the
private member, mLeft. When I pass this structure to the API function,
the structure is returned (out parameter) with data in it. The
Console.WriteLine() call is never called though (no "HERE" on console
window). The Left value is set though. It could be that it just places
the values in memory at the locations of the first 4 members, I'd have to
give it a try and test it out. But it's not that important :)

That makes sense, it must be picking up the private variables still and
marshalling those.

Michael
 
Back
Top