Windows API...

M

Michael L. Barker

I'm having a bit of a problem working with API, and I was wondering if
anyone has any resouces on the web that might be able to help me?

But, (hehe) I'll post the problem here, maybe someone can help me with
it... (resources still wanted lol) =)

Public Function GetObjectText(ByVal hwnd As Integer) As String
Dim i As Integer
Dim s As String

i = SendMessage(hwnd, WM_GETTEXTLENGTH, 0%, 0%)
s = Space(i + 1)

SendMessage(hwnd, WM_GETTEXT, i + 1, s)

GetObjectText = s
End Function


I get an Overflow error on "i = SendMessage(hwnd, WM_GETTEXTLENGTH,
0%, 0%)" anyone know why? The window that I'm getting the data from
does have text...

"An unhandled exception of type 'System.OverflowException' occurred in
FindWindow.exe

Additional information: Arithmetic operation resulted in an overflow."

Why must it overflow? That's not cool. =)

P.S. wasn't sure what group to use... forgive the cross-post.

Thank you!
~(mm)8>
 
M

Magnus Persson

SendMessage returns a Long. Since you have declared the return variable "i"
as an Integer you will naturally get an overflow error if the result cannot
be presented by an integer.
 
H

Herfried K. Wagner [MVP]

* (e-mail address removed) (Michael L. Barker) scripsit:
I'm having a bit of a problem working with API, and I was wondering if
anyone has any resouces on the web that might be able to help me?

But, (hehe) I'll post the problem here, maybe someone can help me with
it... (resources still wanted lol) =)

Public Function GetObjectText(ByVal hwnd As Integer) As String
Dim i As Integer
Dim s As String

i = SendMessage(hwnd, WM_GETTEXTLENGTH, 0%, 0%)
s = Space(i + 1)

SendMessage(hwnd, WM_GETTEXT, i + 1, s)

GetObjectText = s
End Function


I get an Overflow error on "i = SendMessage(hwnd, WM_GETTEXTLENGTH,
0%, 0%)" anyone know why? The window that I'm getting the data from
does have text...

Remove the '%' and post the declaration your use for 'SendMessage'. Why
not use 'GetWindowText' via p/invoke directly?
 
H

Herfried K. Wagner [MVP]

* "Magnus Persson said:
SendMessage returns a Long. Since you have declared the return variable "i"
as an Integer you will naturally get an overflow error if the result cannot
be presented by an integer.

'SendMessage' will return an 'Int32' (= 'Integer').
 
M

Micromause

Private 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

What do you mean by "'GetWindowText' via p/invoke directly"? =) (I know
about that API call, but not sure what p/invoke is...)

Thanks for the quick reply! =) =)
~(mm)8>
 
M

Michael L. Barker

ok, fixed the first part... I can get the length of the text in the edit,
but I can't extract... :(

Public Function GetObjectText(ByVal hwnd As Integer) As String
Dim i As Integer
Dim s, t As String

i = SendMessage(hwnd, WM_GETTEXTLENGTH, 0, 0)
s = Space(i + 1)

SendMessage(hwnd, WM_GETTEXT, i + 1, s)

GetObjectText = s
End Function


The error is on line "SendMessage(hwnd, WM_GETTEXT, i + 1, s)", with the
description:
"An unhandled exception of type 'System.InvalidCastException' occurred in
microsoft.visualbasic.dll

Additional information: Cast from string " "
to type 'Integer' is not valid."

I don't know how to get around this, In VB6 I would use "ByVal s" but that
doesn't seem to work now. :(
 
H

Herfried K. Wagner [MVP]

* "Micromause said:
Private 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

What do you mean by "'GetWindowText' via p/invoke directly"? =) (I know
about that API call, but not sure what p/invoke is...)

P/Invoke is what 'Declare'/'DllImportAttribute' do...
 
J

John Smith

Micromause said:
Private 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

do you have Option Strict Off?

MSDN recomends to overload API functions whenever the parameter type was
"Any" in VB 6

try this aproach:

Private Overloads Declare Ansi Function SendMessage Lib "user32"
Alias "SendMessageA" (ByVal hWnd As IntPtr, ByVal wMsg As Integer, ByVal
wParam As Integer, ByVal LParam As Integer) As Integer

Private Overloads Declare Unicode Function SendMessage Lib "user32"
Alias "SendMessageW" (ByVal hWnd As IntPtr, ByVal wMsg As Integer, ByVal
wParam As Integer, ByVal LParam As String) As Integer

if you don't like IntPtr replace it with Integer or preferable Int32
 
M

Michael L. Barker

Why is everything so backwards? Where can I find a more modern set of API
calls?...
 
C

Cor

Michael,

Because we try to avoid them if it is not necassery, this is a dotnet
Newsgroup

Why should we get sticked on ancient things because you want to use that.

It gives us nothing extra to investigate that.

Just my thought

Cor
 
M

Michael L. Barker

Then let me ask you for your "thoughts" on this one Mr. Dot NET guy.



How else, other then API, can you extract content from a text box from
another app?



"Ancient", yeah... right. What do think the windows is, one big framework?
No point and click/drag and drop access here Jr. API is still key in many
things.



I understand that this is a dot net group, and I'm using that framework... I
just need to know how to get this working so I'll have a better
understanding on how to call the API from "the" framework. Thank you.



~(mm)8>
 
C

Cor

Hi Michael,

Why you visit this newsgroup, you know everything much better and ask
questions.

Seriously a lot of succes

Cor
 
A

Ayaz Ahmed

Hello,

Option Explicit

Private Declare Function NetMessageBufferSend Lib "netapi32.dll" _
(ByVal servername As String, _
ByVal msgname As String, _
ByVal fromname As String, _
ByVal Buffer As String, _
ByVal BufSize As Long) As Long

Private Const NERR_SUCCESS As Long = 0
Private Const NERR_BASE As Long = 2100
Private Const NERR_NetworkError As Long = (NERR_BASE + 36)
Private Const NERR_NameNotFound As Long = (NERR_BASE + 173)
Private Const NERR_UseNotFound As Long = (NERR_BASE + 150)
Private Const ERROR_ACCESS_DENIED As Long = 5
Private Const ERROR_BAD_NETPATH As Long = 53
Private Const ERROR_NOT_SUPPORTED As Long = 50
Private Const ERROR_INVALID_PARAMETER As Long = 87
Private Const ERROR_INVALID_NAME As Long = 123


Public Function NetSendMessage(ByVal sSendTo As String, ByVal sMessage
As String) As Long
Dim ret As Long
sSendTo = StrConv(sSendTo, vbUnicode)
sMessage = StrConv(sMessage, vbUnicode)
NetSendMessage = NetMessageBufferSend(vbNullString, sSendTo,
vbNullString, _
sMessage, Len(sMessage))
End Function

Public Function NetSendErrorMessage(ErrNum As Long) As String
Select Case ErrNum
Case NERR_SUCCESS
'NetSendErrorMessage = "The message was successfully sent"
Case NERR_NameNotFound
NetSendErrorMessage = "Send To not found"
Case NERR_NetworkError
NetSendErrorMessage = "General network error occurred"
Case NERR_UseNotFound
NetSendErrorMessage = "Network connection not found"
Case ERROR_ACCESS_DENIED
NetSendErrorMessage = "Access to computer denied"
Case ERROR_BAD_NETPATH
NetSendErrorMessage = "Sent From server name not found."
Case ERROR_INVALID_PARAMETER
NetSendErrorMessage = "Invalid parameter(s) specified."
Case ERROR_NOT_SUPPORTED
NetSendErrorMessage = "Network request not supported."
Case ERROR_INVALID_NAME
NetSendErrorMessage = "Illegal character or malformed name."
Case Else
NetSendErrorMessage = "Unknown error executing command."
End Select
End Function


Warm Regards,

Ayaz Ahmed
Software Engineer & Web Developer
Creative Chaos (Pvt.) Ltd.
"Managing Your Digital Risk"
http://www.csquareonline.com
Karachi, Pakistan
Mobile +92 300 2280950
Office +92 21 455 2414
 
M

Michael L. Barker

What is this?... I don't think we're on the same page... but still... what
does this do? lol
 
J

Jared

I have used this API in one of my old project and it just stopped working in
XP. I too posted it here and I couldn't find a solution. Seems to work fine
on 2000 machines though.
 
J

Jared

Look at my previous post dated 12/18/2003 titled NetMessageBufferSend - XP -
Help!!! If you get the answer you're looking for let me know, I haven't been
able to get it working.
Jared
 

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