pInvoke Struct
-
I seen a struct on a pInvoke.Net
[StructLayout(LayoutKind.Sequential)]
public struct PROCESSENTRY32
{
public uint dwSize;
public uint cntUsage;
public uint th32ProcessID;
public IntPtr th32DefaultHeapID;
public uint th32ModuleID;
public uint cntThreads;
public uint th32ParentProcessID;
public int pcPriClassBase;
public uint dwFlags;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=260)] public string szExeFile;
};PROCESSENTRY32 pe = new PROCESSENTRY32();
pe.dwSize = (uint)Marshal.SizeOf(pe);I know i have read somewhere here that strut should be intalized inside constructior or it will couse problems. Does that applay to WinAPI too? Edit: Forgot code tags
-
I seen a struct on a pInvoke.Net
[StructLayout(LayoutKind.Sequential)]
public struct PROCESSENTRY32
{
public uint dwSize;
public uint cntUsage;
public uint th32ProcessID;
public IntPtr th32DefaultHeapID;
public uint th32ModuleID;
public uint cntThreads;
public uint th32ParentProcessID;
public int pcPriClassBase;
public uint dwFlags;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=260)] public string szExeFile;
};PROCESSENTRY32 pe = new PROCESSENTRY32();
pe.dwSize = (uint)Marshal.SizeOf(pe);I know i have read somewhere here that strut should be intalized inside constructior or it will couse problems. Does that applay to WinAPI too? Edit: Forgot code tags
Inside what constructor? The only thing you have to worry about is establishing the size of the struct so that the WinAPI knows how big the object is. The only concern about initializing it is what scope the resulting object has.
.45 ACP - because shooting twice is just silly
-----
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001 -
Inside what constructor? The only thing you have to worry about is establishing the size of the struct so that the WinAPI knows how big the object is. The only concern about initializing it is what scope the resulting object has.
.45 ACP - because shooting twice is just silly
-----
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001i see, so it doesn't have struct problems like c# i know for size iz has to be:
public struct Size
{
public Size(int x, int y)
{
X=x;
Y=y;
}
public int X;
public int Y;
}And it shoudln't be used as:
Size size = new Size(1,1);
size.X = 5;
size.Y = 10; -
I seen a struct on a pInvoke.Net
[StructLayout(LayoutKind.Sequential)]
public struct PROCESSENTRY32
{
public uint dwSize;
public uint cntUsage;
public uint th32ProcessID;
public IntPtr th32DefaultHeapID;
public uint th32ModuleID;
public uint cntThreads;
public uint th32ParentProcessID;
public int pcPriClassBase;
public uint dwFlags;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=260)] public string szExeFile;
};PROCESSENTRY32 pe = new PROCESSENTRY32();
pe.dwSize = (uint)Marshal.SizeOf(pe);I know i have read somewhere here that strut should be intalized inside constructior or it will couse problems. Does that applay to WinAPI too? Edit: Forgot code tags
Saksida Bojan wrote:
strut should be intalized inside constructior
Correct, however every
struct
has a default parameterless constructor that cannot be overridden. If you are creating your own constructor (with parameters) then you would need to need to initialize every field inside that constructor. Nothing to do with your question, but *sometimes* when PInvoking you can interchangeint
/uint
. The size of the struct you have declared will be 296 bytes on a 32 bit system and 300 bytes on a 64 bit system. This is well within the range of anint
so you can safely declaredwSize
as anint
(still 4 bytes) and get rid of the cast touint
when calculating the size.Dave
Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn) -
I seen a struct on a pInvoke.Net
[StructLayout(LayoutKind.Sequential)]
public struct PROCESSENTRY32
{
public uint dwSize;
public uint cntUsage;
public uint th32ProcessID;
public IntPtr th32DefaultHeapID;
public uint th32ModuleID;
public uint cntThreads;
public uint th32ParentProcessID;
public int pcPriClassBase;
public uint dwFlags;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=260)] public string szExeFile;
};PROCESSENTRY32 pe = new PROCESSENTRY32();
pe.dwSize = (uint)Marshal.SizeOf(pe);I know i have read somewhere here that strut should be intalized inside constructior or it will couse problems. Does that applay to WinAPI too? Edit: Forgot code tags
If you don't explicitly initialize the fields in a
struct
(such as when using the default ctor) all fields will be initialized to theirdefault
value (0
,0.0
,null
, etc) That's not really a "problem", and it's well-defined behaviour. Even if you have a huge struct like this. The semantics of the default ctor are described in §11.1.2 of ECMA-334:11.1.2 Default constructors
All value types implicitly declare a public parameterless instance constructor called the default constructor.
The default constructor returns a zero-initialized instance known as the default value for the value type:Followed by a table of types and their default values. What kind of problems do you expect? edit: pre with lang="none" appears to be broken..?
modified on Saturday, September 18, 2010 12:46 PM
-
If you don't explicitly initialize the fields in a
struct
(such as when using the default ctor) all fields will be initialized to theirdefault
value (0
,0.0
,null
, etc) That's not really a "problem", and it's well-defined behaviour. Even if you have a huge struct like this. The semantics of the default ctor are described in §11.1.2 of ECMA-334:11.1.2 Default constructors
All value types implicitly declare a public parameterless instance constructor called the default constructor.
The default constructor returns a zero-initialized instance known as the default value for the value type:Followed by a table of types and their default values. What kind of problems do you expect? edit: pre with lang="none" appears to be broken..?
modified on Saturday, September 18, 2010 12:46 PM
harold aptroot wrote:
lang="none"
Try
lang="text"
Dave
Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn) -
harold aptroot wrote:
lang="none"
Try
lang="text"
Dave
Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn) -
If you don't explicitly initialize the fields in a
struct
(such as when using the default ctor) all fields will be initialized to theirdefault
value (0
,0.0
,null
, etc) That's not really a "problem", and it's well-defined behaviour. Even if you have a huge struct like this. The semantics of the default ctor are described in §11.1.2 of ECMA-334:11.1.2 Default constructors
All value types implicitly declare a public parameterless instance constructor called the default constructor.
The default constructor returns a zero-initialized instance known as the default value for the value type:Followed by a table of types and their default values. What kind of problems do you expect? edit: pre with lang="none" appears to be broken..?
modified on Saturday, September 18, 2010 12:46 PM
harold aptroot wrote:
What kind of problems do you expect?
I do not know if i will have a problem. I don't remeber where i read, maybe a dsicusion Class versus Struct. that if you change struct member value, you actualy intilizing new instane of a struct. Does that happen in WinAPI or shoudn't i be worried about that? Maybe it is my paranoia.
-
harold aptroot wrote:
What kind of problems do you expect?
I do not know if i will have a problem. I don't remeber where i read, maybe a dsicusion Class versus Struct. that if you change struct member value, you actualy intilizing new instane of a struct. Does that happen in WinAPI or shoudn't i be worried about that? Maybe it is my paranoia.
Saksida Bojan wrote:
that if you change struct member value, you actualy intilizing new instane of a struct
That is not what happens. Something related does happen though - structs are often copied because their semantics require it, and then changing a field in the struct only causes the copy to change. That is also why it sometimes isn't even allowed.
-
Saksida Bojan wrote:
that if you change struct member value, you actualy intilizing new instane of a struct
That is not what happens. Something related does happen though - structs are often copied because their semantics require it, and then changing a field in the struct only causes the copy to change. That is also why it sometimes isn't even allowed.
thank you for answer