473,386 Members | 1,973 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,386 software developers and data experts.

Interop: Using WriteProcessMemory on LVITEM Structure

I have defined LVITEM as follows:

<StructLayout(LayoutKind.Sequential)> Private Structure LVITEM
Dim mask As Int16
Dim iItem As Int16
Dim iSubItem As Int16
Dim state As Int16
Dim stateMask As Int16
<MarshalAs(UnmanagedType.LPWStr)> Dim pszText As IntPtr
Dim cchTextMax As Int16
Dim iImage As Int16
Dim lParam As Integer
Dim iIndent As Int16
End Structure

When I execute the following code I get an exception on the last line:

<code>
Dim lvi As LVITEM

''dwSize = Marshal.SizeOf(lvi) ' This does not work either
dwSize = 24

Dim SysShared As IntPtr
Dim StringMemory As IntPtr

lvi.mask = LVIF_TEXT
lvi.cchTextMax = 100
lvi.iSubItem = 0

SysShared = GetMemSharedNT(lPID, dwSize, hProcess)
StringMemory = GetMemSharedNT(lPID, 100, hProcess)

lvi.pszText = StringMemory

WriteProcessMemory(hProcess, SysShared, lvi, dwSize,
BytesWritten)
</code>

The error I get is

"Can not marshal field pszText of type LVITEM: Invalid managed/unmanaged
type combination (Int/UInt must be paired with I or U)."

I guess that pszText should be defined 'As String', but then I have a
problem making it point to the memory I have allocated.

Does anyone have any idea how to get round this? I have a sample in C from
this site

http://www.codeproject.com/threads/int64_memsteal.asp

but I would really prefer to do the whole thing in VB.NET.

TIA

Charles
Sep 1 '05 #1
3 2269
Hi Charles,

If you want to make pszText an IntPtr, you don't need MarshalAsAttribute
at all. If it was a String then yes, you need a marshalling attribute to
specify whether it is a LPStr, LPWStr, LPTStr or a ByValTStr. IntPtr can
be passed only as 4-byte integer, thus it don't need any marshalling
attribute.

BTW, your LVITEM declaration is wrong. All fields there are Int32's, not
Int16's.

HTH
Roman

"Charles Law" <bl***@nowhere.com> сообщил/сообщила в новостях следующее:
news:Oy**************@TK2MSFTNGP09.phx.gbl...
I have defined LVITEM as follows:

<StructLayout(LayoutKind.Sequential)> Private Structure LVITEM
Dim mask As Int16
Dim iItem As Int16
Dim iSubItem As Int16
Dim state As Int16
Dim stateMask As Int16
<MarshalAs(UnmanagedType.LPWStr)> Dim pszText As IntPtr
Dim cchTextMax As Int16
Dim iImage As Int16
Dim lParam As Integer
Dim iIndent As Int16
End Structure

When I execute the following code I get an exception on the last line:

<code>
Dim lvi As LVITEM

''dwSize = Marshal.SizeOf(lvi) ' This does not work either
dwSize = 24

Dim SysShared As IntPtr
Dim StringMemory As IntPtr

lvi.mask = LVIF_TEXT
lvi.cchTextMax = 100
lvi.iSubItem = 0

SysShared = GetMemSharedNT(lPID, dwSize, hProcess)
StringMemory = GetMemSharedNT(lPID, 100, hProcess)

lvi.pszText = StringMemory

WriteProcessMemory(hProcess, SysShared, lvi, dwSize,
BytesWritten)
</code>

The error I get is

"Can not marshal field pszText of type LVITEM: Invalid managed/unmanaged type combination (Int/UInt must be paired with I or U)."

I guess that pszText should be defined 'As String', but then I have a
problem making it point to the memory I have allocated.

Does anyone have any idea how to get round this? I have a sample in C from this site

http://www.codeproject.com/threads/int64_memsteal.asp

but I would really prefer to do the whole thing in VB.NET.

TIA

Charles

Sep 1 '05 #2
Hi Roman

Thanks. I removed the MarshalAs attribute from the IntPtr and I no longer
get the exception.

I took the following LVITEM definition from the MSDN

<extract>
typedef struct _LVITEM {
UINT mask;
int iItem;
int iSubItem;
UINT state;
UINT stateMask;
LPTSTR pszText;
int cchTextMax;
int iImage;
LPARAM lParam;
#if (_WIN32_IE >= 0x0300)
int iIndent;
#endif
#if (_WIN32_IE >= 0x560)
int iGroupId;
UINT cColumns; // tile view columns
PUINT puColumns;
#endif
} LVITEM, *LPLVITEM;
</extract>

and duly converted the elements to .NET types, so doesn't that mean that an
int should be translated to int16 in .NET? With the exception that I
mentioned in the OP, this definition works for me, although I admit that it
also works using int32s.

Charles
"Dragon" <no@spam.please> wrote in message
news:uu****************@tk2msftngp13.phx.gbl...
Hi Charles,

If you want to make pszText an IntPtr, you don't need MarshalAsAttribute
at all. If it was a String then yes, you need a marshalling attribute to
specify whether it is a LPStr, LPWStr, LPTStr or a ByValTStr. IntPtr can
be passed only as 4-byte integer, thus it don't need any marshalling
attribute.

BTW, your LVITEM declaration is wrong. All fields there are Int32's, not
Int16's.

HTH
Roman

"Charles Law" <bl***@nowhere.com> сообщил/сообщила в новостях следующее:
news:Oy**************@TK2MSFTNGP09.phx.gbl...
I have defined LVITEM as follows:

<StructLayout(LayoutKind.Sequential)> Private Structure LVITEM
Dim mask As Int16
Dim iItem As Int16
Dim iSubItem As Int16
Dim state As Int16
Dim stateMask As Int16
<MarshalAs(UnmanagedType.LPWStr)> Dim pszText As IntPtr
Dim cchTextMax As Int16
Dim iImage As Int16
Dim lParam As Integer
Dim iIndent As Int16
End Structure

When I execute the following code I get an exception on the last line:

<code>
Dim lvi As LVITEM

''dwSize = Marshal.SizeOf(lvi) ' This does not work either
dwSize = 24

Dim SysShared As IntPtr
Dim StringMemory As IntPtr

lvi.mask = LVIF_TEXT
lvi.cchTextMax = 100
lvi.iSubItem = 0

SysShared = GetMemSharedNT(lPID, dwSize, hProcess)
StringMemory = GetMemSharedNT(lPID, 100, hProcess)

lvi.pszText = StringMemory

WriteProcessMemory(hProcess, SysShared, lvi, dwSize,
BytesWritten)
</code>

The error I get is

"Can not marshal field pszText of type LVITEM: Invalid

managed/unmanaged
type combination (Int/UInt must be paired with I or U)."

I guess that pszText should be defined 'As String', but then I have a
problem making it point to the memory I have allocated.

Does anyone have any idea how to get round this? I have a sample in C

from
this site

http://www.codeproject.com/threads/int64_memsteal.asp

but I would really prefer to do the whole thing in VB.NET.

TIA

Charles


Sep 1 '05 #3
> and duly converted the elements to .NET types, so doesn't that mean
that an
int should be translated to int16 in .NET?


int (as well as UINT) is 4-byte integer, so it's .NET equivalent is
Int32 (Well it's actually UInt32 for UINT, but using UInt's in VB 2002/3
is a real pain).
Int16 is for short datatype.
Sep 1 '05 #4

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

Similar topics

2
by: Ross Micheals | last post by:
All I have some general .NET questions that I'm looking for some help with. Some of these questions (like the first) are ones that I've seen various conflicting information on, or questions that...
2
by: STiAT | last post by:
Hello. I tried to find a similar Function to WriteMemory, and couldnt find any memory manipulating functions in dotNET at all. I now wanted to know if there are any similar functions to the...
1
by: ivang | last post by:
Hello, All! Please help me to marshal following win32 structure: typedef struct _STARTUPINFOW { DWORD cb; LPWSTR lpReserved;
17
by: Aaron | last post by:
I've got a doozie of a problem! I and others have been trying to figure this out for too long and I've come to the conclusion that I should probably look for some support.. Ok, I have a COM...
3
by: Charles Law | last post by:
I have defined LVITEM as follows: <StructLayout(LayoutKind.Sequential)> Private Structure LVITEM Dim mask As Int16 Dim iItem As Int16 Dim iSubItem As Int16 Dim state As Int16 Dim stateMask As...
6
by: Nataraj1978 | last post by:
I have a requirement to use a dll written in Visual C++ 6.0 in my C# application. In order to establish the link between my application and the dll, I have written a ATL COM Component in Visual...
2
by: Evolution445 | last post by:
Hi all, In my previous topic about a week ago on this forum I attempted to use ReadProcessMemory. After several hours of trying different things, It worked. Next up I tried figuring out...
3
by: michelqa | last post by:
Hi, I already post a similar question last week without success. Ok I want to get the current text selection in a RICHEDIT control.. This can be easily done in C++ with EM_EXGETSEL message. I...
2
by: Fredo | last post by:
First of all, I apologize for cross-posting. I posted this on the framework.interop group as well, but haven't received a response yet, so I thought I'd try here... I'm trying to write an app...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.