|
-
Apr 4th, 2001, 08:31 PM
#1
Thread Starter
Junior Member
Hi again,
how to send a string value as a lParam paramiter of PostMessage
I m trying this, but i m getting error during it running...
Dim mStr as String
mStr = "Hello"
PostMessage hwnd, PRIVATE_MSG, 0, AddressOf mStr
and i also try without AddressOf operator but still i m getting error.. please tell me how to convert String to Long for passing it and how to retrive it on WndProc function? I think i have to use MoveMemory for converting back Long value to String value.. but please help me regarding this as soon as possible...
Thank you,
Prashant
-
Apr 5th, 2001, 01:59 PM
#2
Monday Morning Lunatic
You need VarPtr:
Code:
Dim mStr as String
mStr = "Hello"
PostMessage hwnd, PRIVATE_MSG, 0, VarPtr(mStr)
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Apr 5th, 2001, 02:25 PM
#3
If you change the declaration to SendMessage(...lParam As Any), then it will also work.
-
Apr 5th, 2001, 04:03 PM
#4
Thread Starter
Junior Member
Thanks for replying me, its really works but how to retrive lParam(Long) value to String... i tryied CopyMemory API to convert it to string, but its crashed my system...
-
Apr 5th, 2001, 04:55 PM
#5
Monday Morning Lunatic
What API function are you using that you need to return a string from?
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Apr 5th, 2001, 05:27 PM
#6
Thread Starter
Junior Member
Private Declare Sub CopyMem Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Public Function WndProc(ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Select Case wMsg
Case PRIVATE_MSG
Dim mStr As String * 100
CopyMem ByVal mStr, ByVal lParam, Len(mStr)
MsgBox "PRIVATE_MSG " + mStr
WndProc = 1
Case Else
WndProc = CallWindowProc(OldProc, hwnd, wMsg, wParam, lParam)
End Select
End Function
-
Apr 5th, 2001, 05:29 PM
#7
Monday Morning Lunatic
I would suggest making use of wParam to send the length of the string -- I think you're copying past the end of the string.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Apr 5th, 2001, 06:40 PM
#8
You could also use an Atom.
-
Apr 5th, 2001, 07:59 PM
#9
Thread Starter
Junior Member
Thanks, at last i got the solution but still i m not very satisfy for this... now i m using following code.. but please inform me if there is any better solution for it...
Private Sub Command1_Click()
Message FindWindow(vbNullString, App.Title), PRIVATE_MSG, Len(mStr), StrPtr(mStr)
End Sub
Public Function WndProc(ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Select Case wMsg
Case PRIVATE_MSG
Dim mStrTmp As String, mStr As String, tmp As Integer
mStrTmp = Space$(wParam * 2 - 1)
CopyMem ByVal mStrTmp, ByVal lParam, Len(mStrTmp)
mStr = ""
For tmp = 0 To wParam - 1
mStr = mStr + Mid$(mStrTmp, tmp * 2 + 1, 1)
Next tmp
MsgBox "PRIVATE_MSG " + mStr
WndProc = 1
Case Else
WndProc = CallWindowProc(OldProc, hwnd, wMsg, wParam, lParam)
End Select
End Function
Thanks again
-
Apr 5th, 2001, 08:29 PM
#10
New Member
I can't be sure this works without testing, but I think you can change your function to do this using StrConv to format the Unicode to Ascii rather than picking it apart yourself.
Public Function WndProc(ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Select Case wMsg
Case PRIVATE_MSG
Dim mStr As String
mStrTmp = Space(wParam * 2)
CopyMem mStr, ByVal lParam, Len(mStrTmp)
MsgBox "PRIVATE_MSG " + StrConv(mStr, vbFromUnicode)
WndProc = 1
Case Else
WndProc = CallWindowProc(OldProc, hwnd, wMsg, wParam, lParam)
End Select
End Function
-
Apr 6th, 2001, 08:36 AM
#11
Frenzied Member
Try:
'\\ --[StringFromPointer]-------------------------------------------------------------------
'\\ Returns a VB string from an API returned string pointer
'\\ Parameters:
'\\ lpString - The long pointer to the string
'\\ lMaxlength - the size of empty buffer to allow
'\\ HISTORY:
'\\ DEJ 28/02/2001 Check pointer is a valid string pointer...
'\\ ----------------------------------------------------------------------------------------
'\\ You have a royalty free right to use, reproduce, modify, publish and mess with this code
'\\ I'd like you to visit http://www.merrioncomputing.com for updates, but won't force you
'\\ ----------------------------------------------------------------------------------------
Public Function StringFromPointer(lpString As Long, lMaxLength As Long) As String
Dim sRet As String
Dim lRet As Long
If IsBadStringPtrByLong(lpString, lMaxLength) Then
'\\ An error has occured - do not attempt to use this pointer
Call ReportError(Err.LastDllError, "StringFromPointer", "Attempt to read bad string pointer: " & lpString)
StringFromPointer = ""
Exit Function
End If
'\\ Pre-initialise the return string...
sRet = Space$(lMaxLength)
CopyMemory ByVal sRet, ByVal lpString, ByVal Len(sRet)
If Err.LastDllError = 0 Then
If InStr(sRet, Chr$(0)) > 0 Then
sRet = Left$(sRet, InStr(sRet, Chr$(0)) - 1)
End If
End If
StringFromPointer = sRet
End Function
Note:
For a windows api return, set max length to at least 1024 to be on the safe side...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|