[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.dotnet.framework.interop

Re: EM_CHARFROMPOS always return -1

Alan

6/3/2007 3:53:00 AM

Hi Mike,

when uprade this to vb.net, VarPtr is not support but the Help file
provide no alternative to replace. Is there any safe handler to
replace VarPtr?
'UPGRADE_ISSUE: VarPtr function is not supported. Click for more:
'ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?
keyword="367764E5-F3F8-4E43-AC3E-7FE0B5E074E2"'
SendMessageTimeout HwndOver, WM_GETTEXT, 255,
VarPtr(WindowTextArray(0)), 0, 1000, 0

Alan

On Apr 18, 3:43 am, "MikeD" <nob...@nowhere.edu> wrote:
> <ala...@gmail.com> wrote in message
>
> news:1176826301.501011.121310@q75g2000hsh.googlegroups.com...
>
> > Hi Mike,
>
> > You are really VB WINAPI expert, adding byval does help. However, I
> > get this declaration from API viewer, it seems that the declaration
> > from API viewer may not be correct. Oh, I realize that lParam can be
> > pointer for POINT structure.
>
> No, the declaration in the viewer is correct, it's just "generic". What you
> pass for SendMessage's LParam can vary, even the data type. It might be a
> String, or a Long, or a structure (not just a POINT structure). That's why
> the declaration in the API Viewer has lParam As Any. It's more versatile
> this way, but also more error-prone and even somewhat dangerous (not too
> difficult to cause the app to crash if you don't pass lParam correctly for
> the message you're sending).
>
> What many people do is declare type-safe versions of SendMessage. For
> example
>
> Public Declare Function SendMessageByString Lib "user32" Alias
> "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam
> As Long, ByVal lParam As String) As Long
>
> Public Declare Function SendMessageByLong Lib "user32" Alias
> "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam
> As Long, ByVal lParam As Long) As Long
>
> Public Declare Function SendMessageByPOINTAPI Lib "user32" Alias
> "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam
> As Long, lParam As POINTAPI) As Long
>
> As you can see, this takes care of the ByVal keyword (to include or not
> include it) in the declaration. Since you didn't include the declaration in
> your original post, I couldn't be sure if this was the problem, or if it
> could be something, anything, else. You should always include API
> declarations when posting your code for this reason and others.
>
> Also, your code might have worked without the ByVal keyword IF you had
> assigned the return value of MAKELPARAM to a variable and used that variable
> in your call to SendMessage. Not sure about that, but it's probably a better
> way to go anyway.
>
> > I am author of a pop shareware, to thank you, I can send you a free
> > license. You can send me email atalanwo[at]gmail[dot]com.
>
> Appreciate the gesture, but that's quite alright.
>
> --
> Mike
> Microsoft Visual Basic MVP


1 Answer

Alan

6/3/2007 11:00:00 AM

0

Mike,

OK, I got it, VarPtr can be repaced by
System.Runtime.InteropServices.Marshal.UnsafeAddrOfPinnedArrayElement

SendMessageTimeout(HwndOver, WM_GETTEXT, 255,
System.Runtime.InteropServices.Marshal.UnsafeAddrOfPinnedArrayElement(WindowTextArray,
0), 0, 1000, 0)

However, this won't work:
r = SendMessage(HwndOver, EM_CHARFROMPOS, 0, MAKELPARAM(pt.X,
pt.Y))

You asked me to add ByVal to MAKELPARAM(pt.X, pt.Y) but it won't work
in vb.net 2.0.

BTW, I found that vb.net 2.0 support GetCharIndexFromPosition:
Me.Text2.GetLineFromCharIndex(Me.Text2.GetCharIndexFromPosition(eventArgs.Location))
However, my goal is to get char index from position of other window /
outside my application. It seem that I have to still use Win32 API.

Do other experts have advice?

FYI, my API declaration:

Public Declare Function SendMessage Lib "user32" Alias
"SendMessageA" (ByVal hwnd As Integer, ByVal wMsg As Integer, ByVal
wParam As Integer, ByVal lParam As Integer) As Integer

Public Declare Function GetClassName Lib "user32" Alias
"GetClassNameA"(ByVal hwnd As Integer, ByVal lpClassName As String,
ByVal nMaxCount As Integer) As Integer

'UPGRADE_WARNING: Structure POINTAPI may require marshalling
attributes to be passed as an argument in this Declare statement.
Click for more: 'ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?
keyword="C429C3A5-5D47-4CD9-8F51-74A1616405DC"'
Public Declare Function ScreenToClient Lib "user32" (ByVal hwnd As
Integer, ByRef lpPoint As POINTAPI) As Integer

'UPGRADE_ISSUE: Declaring a parameter 'As Any' is not supported.
Click for more: 'ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?
keyword="FAE78A8D-8978-4FD4-8208-5B7324A8F795"'
'UPGRADE_ISSUE: Declaring a parameter 'As Any' is not supported.
Click for more: 'ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?
keyword="FAE78A8D-8978-4FD4-8208-5B7324A8F795"'
Public Declare Sub CopyMemory Lib "kernel32" Alias
"RtlMoveMemory" (ByRef Destination As Object, ByRef Source As Object,
ByVal Length As Integer)

Public Function MAKELPARAM(ByRef wLow As Integer, ByRef wHigh As
Integer) As IntPtr

'Combines two integers into a long
MAKELPARAM = MAKELONG(wLow, wHigh)

End Function


Public Function MAKELONG(ByRef wLow As Integer, ByRef wHigh As
Integer) As Integer

MAKELONG = LoWord(wLow) Or (&H10000 * LoWord(wHigh))

End Function


Public Function LoWord(ByRef dwValue As Integer) As Short

CopyMemory(LoWord, dwValue, 2)

End Function

On Jun 3, 11:52 am, Alan <ala...@gmail.com> wrote:
> Hi Mike,
>
> when uprade this to vb.net, VarPtr is not support but the Help file
> provide no alternative to replace. Is there any safe handler to
> replace VarPtr?
> 'UPGRADE_ISSUE: VarPtr function is not supported. Click for more:
> 'ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?
> keyword="367764E5-F3F8-4E43-AC3E-7FE0B5E074E2"'
> SendMessageTimeout HwndOver, WM_GETTEXT, 255,
> VarPtr(WindowTextArray(0)), 0, 1000, 0
>
> Alan
>
> On Apr 18, 3:43 am, "MikeD" <nob...@nowhere.edu> wrote:
>
>
>
> > <ala...@gmail.com> wrote in message
>
> >news:1176826301.501011.121310@q75g2000hsh.googlegroups.com...
>
> > > Hi Mike,
>
> > > You are really VB WINAPI expert, adding byval does help. However, I
> > > get this declaration from API viewer, it seems that the declaration
> > > from API viewer may not be correct. Oh, I realize that lParam can be
> > > pointer for POINT structure.
>
> > No, the declaration in the viewer is correct, it's just "generic". What you
> > pass for SendMessage's LParam can vary, even the data type. It might be a
> > String, or a Long, or a structure (not just a POINT structure). That's why
> > the declaration in the API Viewer has lParam As Any. It's more versatile
> > this way, but also more error-prone and even somewhat dangerous (not too
> > difficult to cause the app to crash if you don't pass lParam correctly for
> > the message you're sending).
>
> > What many people do is declare type-safe versions of SendMessage. For
> > example
>
> > Public Declare Function SendMessageByString Lib "user32" Alias
> > "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam
> > As Long, ByVal lParam As String) As Long
>
> > Public Declare Function SendMessageByLong Lib "user32" Alias
> > "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam
> > As Long, ByVal lParam As Long) As Long
>
> > Public Declare Function SendMessageByPOINTAPI Lib "user32" Alias
> > "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam
> > As Long, lParam As POINTAPI) As Long
>
> > As you can see, this takes care of the ByVal keyword (to include or not
> > include it) in the declaration. Since you didn't include the declaration in
> > your original post, I couldn't be sure if this was the problem, or if it
> > could be something, anything, else. You should always include API
> > declarations when posting your code for this reason and others.
>
> > Also, your code might have worked without the ByVal keyword IF you had
> > assigned the return value of MAKELPARAM to a variable and used that variable
> > in your call to SendMessage. Not sure about that, but it's probably a better
> > way to go anyway.
>
> > > I am author of a pop shareware, to thank you, I can send you a free
> > > license. You can send me email atalanwo[at]gmail[dot]com.
>
> > Appreciate the gesture, but that's quite alright.
>
> > --
> > Mike
> > Microsoft Visual Basic MVP- Hide quoted text -
>
> - Show quoted text -