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

What's wrong with this code

[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.


Nov 20 '05 #1
5 5213
* " SamSpade" <st**************@REMOVEaol.com> scripsit:
[DllImport("user32.dll", CharSet=CharSet.Auto, SetLastError=true)]public
static extern int SendMessage(


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

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #2
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 [MVP]" <hi***************@gmx.at> wrote in message
news:c6************@ID-208219.news.uni-berlin.de...
* " SamSpade" <st**************@REMOVEaol.com> scripsit:
[DllImport("user32.dll", CharSet=CharSet.Auto, SetLastError=true)]public
static extern int SendMessage(


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

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>

Nov 20 '05 #3
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;

}
Nov 20 '05 #4
" SamSpade" <st**************@REMOVEaol.com> schrieb
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

Nov 20 '05 #5
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
Nov 20 '05 #6

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

Similar topics

125
by: Sarah Tanembaum | last post by:
Beside its an opensource and supported by community, what's the fundamental differences between PostgreSQL and those high-price commercial database (and some are bloated such as Oracle) from...
72
by: E. Robert Tisdale | last post by:
What makes a good C/C++ programmer? Would you be surprised if I told you that it has almost nothing to do with your knowledge of C or C++? There isn't much difference in productivity, for...
121
by: typingcat | last post by:
First of all, I'm an Asian and I need to input Japanese, Korean and so on. I've tried many PHP IDEs today, but almost non of them supported Unicode (UTF-8) file. I've found that the only Unicode...
51
by: WindAndWaves | last post by:
Can anyone tell me what is wrong with the goto command. I noticed it is one of those NEVER USE. I can understand that it may lead to confusing code, but I often use it like this: is this...
46
by: Keith K | last post by:
Having developed with VB since 1992, I am now VERY interested in C#. I've written several applications with C# and I do enjoy the language. What C# Needs: There are a few things that I do...
13
by: Jason Huang | last post by:
Hi, Would someone explain the following coding more detail for me? What's the ( ) for? CurrentText = (TextBox)e.Item.Cells.Controls; Thanks. Jason
1
by: GS | last post by:
I got a combobox box that I load at load time. the Item and vales ended up in reverse order of each other, what went wrong? the database table has the following row code value ebay ...
98
by: tjb | last post by:
I often see code like this: /// <summary> /// Removes a node. /// </summary> /// <param name="node">The node to remove.</param> public void RemoveNode(Node node) { <...> }
9
by: Pyenos | last post by:
import cPickle, shelve could someone tell me what things are wrong with my code? class progress: PROGRESS_TABLE_ACTIONS= DEFAULT_PROGRESS_DATA_FILE="progress_data" PROGRESS_OUTCOMES=
20
by: Daniel.C | last post by:
Hello. I just copied this code from my book with no modification : #include <stdio.h> /* count characters in input; 1st version */ main() { long nc; nc = 0;
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: 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...

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.