What's wrong with this code

S

SamSpade

[DllImport("user32.dll", CharSet=CharSet.Auto, SetLastError=true)]public
static extern int SendMessage(

IntPtr hWnd, [MarshalAs(UnmanagedType.U4)] int Msg, int wParam, IntPtr
lParam);

--snip


SelectionFont.cbSize = Marshal.SizeOf(SelectionFont)

SelectionFont.dwMask = Rtf.CFM_FACE

Dim TmpStruc As IntPtr = Marshal.AllocHGlobal(SelectionFont.cbSize)

Marshal.StructureToPtr(SelectionFont, TmpStruc, True)

NumOfCharReturned = WinApi.SendMessage(gctlText.TxtHandle,
Rtf.EM_GETCHARFORMAT, Rtf.SCF_SELECTION, TmpStruc)

Marshal.PtrToStructure(TmpStruc, SelectionFont)

Marshal.FreeHGlobal(TmpStruc)

==========

It returns a large negative NumOfCharReturned and bombs at
Marshal.PtrToStructure.
 
H

Herfried K. Wagner [MVP]

* " SamSpade said:
[DllImport("user32.dll", CharSet=CharSet.Auto, SetLastError=true)]public
static extern int SendMessage(

It's C# code and this is a VB.NET group.
 
S

SamSpade

I think the C# code is OK. I gave it only for reference.

You don't see anything wrong with the VB code??


Herfried K. Wagner said:
* " SamSpade said:
[DllImport("user32.dll", CharSet=CharSet.Auto, SetLastError=true)]public
static extern int SendMessage(

It's C# code and this is a VB.NET group.
 
S

SamSpade

REVISED
It bombs at Marshal.PtrToStructure as follows:
Marshal.PtrToStructure(TmpStruc, lSelectionFont) Run-time exception thrown
: System.ArgumentException -
The structure must not be a value class.
Parameter name: structure

The structure is declared as follows(see below for the definition)
Dim SelectionFont As CHARFORMAT2

If I call a sendmessage that is defined as
[DllImport("user32.dll", EntryPoint="SendMessage", CharSet=CharSet.Auto )]

public static extern int SendMessage( IntPtr hWnd, int Msg, int wParam, ref
CHARFORMAT2 cf2 );

It works OK so I think the problem is in the VB code below:

WHY does it think the structure is a value class??




SelectionFont.cbSize = Marshal.SizeOf(SelectionFont)

SelectionFont.dwMask = Rtf.CFM_FACE

Dim TmpStruc As IntPtr = Marshal.AllocHGlobal(SelectionFont.cbSize)

Marshal.StructureToPtr(SelectionFont, TmpStruc, True)

WinApi.SendMessage(gctlText.TxtHandle,
Rtf.EM_GETCHARFORMAT, Rtf.SCF_SELECTION, TmpStruc)

Marshal.PtrToStructure(TmpStruc, SelectionFont)

Marshal.FreeHGlobal(TmpStruc)


SendMessage is defined in a library as
[DllImport("user32.dll", CharSet=CharSet.Auto, SetLastError=true)]public
static extern int SendMessage(
IntPtr hWnd, [MarshalAs(UnmanagedType.U4)] int Msg, int wParam, IntPtr
lParam);


[StructLayout(LayoutKind.Sequential, Pack=4, CharSet=CharSet.Auto)]

public struct CHARFORMAT2

{

public int cbSize;

public int dwMask;

public int dwEffects;

public int yHeight;

public int yOffset;

public int crTextColor;

public byte bCharSet;

public byte bPitchAndFamily;

[MarshalAs(UnmanagedType.ByValArray, SizeConst=32)] public char[]
szFaceName; //OR [MarshalAs( UnmanagedType.ByValTStr, SizeConst=32 )] public
string szFaceName;

public short wWeight;

public short sSpacing;

public int crBackColor;

public int lcid;

public int dwReserved;

public short sStyle;

public short wKerning;

public byte bUnderlineType;

public byte bAnimation;

public byte bRevAuthor;

public byte bReserved1;

}
 
A

Armin Zingler

SamSpade said:
REVISED
It bombs at Marshal.PtrToStructure as follows:
Marshal.PtrToStructure(TmpStruc, lSelectionFont) Run-time exception
thrown
: System.ArgumentException -
The structure must not be a value class.
Parameter name: structure

The structure is declared as follows(see below for the definition)
Dim SelectionFont As CHARFORMAT2

If I call a sendmessage that is defined as
[DllImport("user32.dll", EntryPoint="SendMessage",
CharSet=CharSet.Auto )]

public static extern int SendMessage( IntPtr hWnd, int Msg, int
wParam, ref CHARFORMAT2 cf2 );

It works OK so I think the problem is in the VB code below:

WHY does it think the structure is a value class??

Each structure is a value type. See the documentation of the PtrToStructure
function: You mustn't use this overloaded version with value types. Use the
other overloaded version:

Overloads Public Shared Function PtrToStructure(IntPtr, Type) As Object


--
Armin

How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html
 
S

SamSpade

Each structure is a value type. See the documentation of the
PtrToStructure

I did and saw: "This must be an instance of a formatted class." But don't
know what a formatted class is nor how to make the structure one (I know now
that is not the way to go)

Thanks, works now now that I'm using the correct version.


I've see examples where they use AllocCoTaskMem
instead of the AllocHGlobal I used.

I've read the doc but do not know which I should use when.

Thanks again
 

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