473,382 Members | 1,421 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,382 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
Nov 21 '05 #1
3 4645
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

Nov 21 '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


Nov 21 '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.
Nov 21 '05 #4

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

Similar topics

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...
0
by: ree32 | last post by:
Is there one using a tree structure to collapse nodes, while editing. What I had in mind was something similar to Visual Studio.net where you can collapse functions to make it easier to read. So...
14
by: Just Me | last post by:
Can anyone fix the code below? I need to set pTo to NULL and pFrom to a fullfilepath followed by two NULLs Below Filename is a string With FileOperation ..wFunc = FO_DELETE ..pFrom =...
16
by: Asaf | last post by:
I am trying to create and use a COM object with C#.NET 2005. The assembly is set to "Register for COM interop" but when I am trying to call it from VB on Word 2003 I am getting this error: ...
3
by: DaveoB | last post by:
Hey Im wonderin if anyone can help me with this question,Sum the odd numbers between 1 and 97, using a for structure. Might seem a bit simple but i cant get it to work. Anyone help?
3
by: =?Utf-8?B?TWluZ3lp?= | last post by:
Hi, I have the following question regarding communicating with a OCX control using C#. Idon't have access to the ocx code, so my c# code is actually mimic the behavior of C++ counterparts....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...

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.