473,398 Members | 2,125 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Marshalling

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.InteropSer vices.MarshalAs
(System.Runtime.InteropServices.UnmanagedType.ByVa lTStr,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?

--
http://www.standards.com/; See Howard Kaikow's web site.
Nov 21 '05 #1
2 5530
On 2005-05-27, Howard Kaikow <ka****@standards.com> wrote:
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.InteropSer vices.MarshalAs
(System.Runtime.InteropServices.UnmanagedType.ByVa lTStr,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?


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.

--
Tom Shelton [MVP]
Nov 21 '05 #2
"Tom Shelton" <ts******@YOUKNOWTHEDRILLcomcast.net> wrote in message
news:eG*************@TK2MSFTNGP12.phx.gbl...
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.
Nov 21 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: PHil Coveney | last post by:
Hello, I am having difficulty marshalling structures when calling DeviceIoControl. I am importing this Win32 function as static extern int DeviceIoControl (int hDevice, int...
4
by: Animesh | last post by:
Hi All, I don't know whethher this is possible or not. This is the result of a bad design problem. Here I go; I have a structure like this: typedef struct _s_index_entry { char *doc_id;...
0
by: swartzbill2000 | last post by:
I am familiar with the VB6/VC6/ATL way of marshalling an incoming interface via New (VB), and then marshalling an outgoing interface with some form of Advise. To me it looks like .Net has...
2
by: bonk | last post by:
I am currently trying to write a longer article about Marshalling when using . Does anyone know books, articles, or websites that cover Marshalling in Platform Invocation Services ()?
2
by: RJ Lohan | last post by:
Howdy, I have a legacy DLL for which I have a problem marshalling a parameter type of char**. The function header (C++) is as so; extern "C" __declspec(dllexport) int __stdcall...
2
by: calenlas | last post by:
Hi all, I'm taking my first steps into C# <--C++ DLL Interop and unfortunately I've run into (what seems to be) a very complicated case as my first task. Perhaps someone here can help me. I...
2
by: d-42 | last post by:
Hi, I'm pretty sure I've just got a Marshalling problem, but I'm completely stumped. If there is a better newsgroup to post this in, please point me towards it. First I'm trying to use...
1
by: d-42 | last post by:
Hi, I'm pretty sure I've just got a Marshalling problem, but I'm completely stumped. If there is a better newsgroup to post this in, please point me towards it. First I'm trying to use...
0
by: santoshdarekar | last post by:
HI, I have some code which I am reverse engineering. Here is one module which is used as calculation madule and marshalling is used in it. But, still there is performance issue as the CPU time the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.