Results 1 to 11 of 11

Thread: String value as lParam?

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Mar 2001
    Location
    San Jose
    Posts
    26

    Red face

    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

  2. #2
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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

  3. #3
    Megatron
    Guest
    If you change the declaration to SendMessage(...lParam As Any), then it will also work.

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Mar 2001
    Location
    San Jose
    Posts
    26
    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...

  5. #5
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Mar 2001
    Location
    San Jose
    Posts
    26
    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

  7. #7
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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

  8. #8
    Megatron
    Guest
    You could also use an Atom.

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Mar 2001
    Location
    San Jose
    Posts
    26
    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

  10. #10
    New Member
    Join Date
    Apr 2001
    Posts
    14
    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

  11. #11
    Frenzied Member MerrionComputin's Avatar
    Join Date
    Apr 2001
    Location
    Dublin, Ireland
    Posts
    1,616

    Lightbulb

    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
  •  



Click Here to Expand Forum to Full Width