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;
}