Marshalling

H

Howard Kaikow

I've got the following in a VB 6 project:

Private Type PROCESSENTRY32
dwSize As Long
cntUsage As Long
th32ProcessID As Long
th32DefaultHeapID As Long
th32ModuleID As Long
cntThreads As Long
th32ParentProcessID As Long
pcPriClassBase As Long
dwFlags As Long
szExeFile As String * MAX_PATH
End Type

VB .NET upgrades that to:

Private Structure PROCESSENTRY32
Dim dwSize As Integer
Dim cntUsage As Integer
Dim th32ProcessID As Integer
Dim th32DefaultHeapID As Integer
Dim th32ModuleID As Integer
Dim cntThreads As Integer
Dim th32ParentProcessID As Integer
Dim pcPriClassBase As Integer
Dim dwFlags As Integer
<VBFixedString(MAX_PATH),System.Runtime.InteropServices.MarshalAs
(System.Runtime.InteropServices.UnmanagedType.ByValTStr,SizeConst:=MAX_PATH)
Public szExeFile As String
End Structure

As upgraded, the code runs as expected.

However, there are warning messages:
'UPGRADE_WARNING: Structure PROCESSENTRY32 may require marshalling
attributes to be passed as an argument in this Declare statement. Click for
more:
'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="vbup1050"'

So following the instructions in the warning, I modified the structure to:

<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi)> Private
Structure PROCESSENTRY32
Dim dwSize As Integer
Dim cntUsage As Integer
Dim th32ProcessID As Integer
Dim th32DefaultHeapID As Integer
Dim th32ModuleID As Integer
Dim cntThreads As Integer
Dim th32ParentProcessID As Integer
Dim pcPriClassBase As Integer
Dim dwFlags As Integer
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=MAX_PATH)> Public
szExeFile As String
End Structure

This causes the code to not excute correctl;y because

Dim pe32 As PROCESSENTRY32

pe32.dwSize = Len(pe32)

Fails to set the correct size.

Without the marshalling changes, len(pe32) correctly returns 296.
With the marshalling changes. len(pe32) returns 40.

What needs to be done to correct this problem?
 
T

Tom Shelton

I've got the following in a VB 6 project:

Private Type PROCESSENTRY32
dwSize As Long
cntUsage As Long
th32ProcessID As Long
th32DefaultHeapID As Long
th32ModuleID As Long
cntThreads As Long
th32ParentProcessID As Long
pcPriClassBase As Long
dwFlags As Long
szExeFile As String * MAX_PATH
End Type

VB .NET upgrades that to:

Private Structure PROCESSENTRY32
Dim dwSize As Integer
Dim cntUsage As Integer
Dim th32ProcessID As Integer
Dim th32DefaultHeapID As Integer
Dim th32ModuleID As Integer
Dim cntThreads As Integer
Dim th32ParentProcessID As Integer
Dim pcPriClassBase As Integer
Dim dwFlags As Integer
<VBFixedString(MAX_PATH),System.Runtime.InteropServices.MarshalAs
(System.Runtime.InteropServices.UnmanagedType.ByValTStr,SizeConst:=MAX_PATH)
End Structure

As upgraded, the code runs as expected.

However, there are warning messages:
'UPGRADE_WARNING: Structure PROCESSENTRY32 may require marshalling
attributes to be passed as an argument in this Declare statement. Click for
more:
'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="vbup1050"'

So following the instructions in the warning, I modified the structure to:

<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi)> Private
Structure PROCESSENTRY32
Dim dwSize As Integer
Dim cntUsage As Integer
Dim th32ProcessID As Integer
Dim th32DefaultHeapID As Integer
Dim th32ModuleID As Integer
Dim cntThreads As Integer
Dim th32ParentProcessID As Integer
Dim pcPriClassBase As Integer
Dim dwFlags As Integer
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=MAX_PATH)> Public
szExeFile As String
End Structure

This causes the code to not excute correctl;y because

Dim pe32 As PROCESSENTRY32

pe32.dwSize = Len(pe32)

Fails to set the correct size.

Without the marshalling changes, len(pe32) correctly returns 296.
With the marshalling changes. len(pe32) returns 40.

What needs to be done to correct this problem?

I would declare this like this...

<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Auto)> _
Private Structure PROCESSENTRY32
Dim dwSize As Integer
Dim cntUsage As Integer
Dim th32ProcessID As Integer
Dim th32DefaultHeapID As IntPtr
Dim th32ModuleID As Integer
Dim cntThreads As Integer
Dim th32ParentProcessID As Integer
Dim pcPriClassBase As Integer
Dim dwFlags As Integer
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=MAX_PATH)> _
Public szExeFile As String
End Structure

The change that made it change the length was the remove of the
VB.FixedLength string attribute. Check it using the Marshal.SizeOf
method instead, since it understands the marshaling attributes.
 
H

Howard Kaikow

The change that made it change the length was the remove of the
VB.FixedLength string attribute. Check it using the Marshal.SizeOf
method instead, since it understands the marshaling attributes.

Thanx, I missed that.
Deteriorating eyesight or less brain matter, take yer pick.
 

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