Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Pointers and strings..

Status
Not open for further replies.

Masali

Programmer
Jun 19, 2002
143
SE
In C/C++ the function I want to use in VB is used like this:

char *name = SendMessage(hwnd, 1024, 3, 211);

As you can see it returns a pointer. Is it possible to use some API functions to read out the pointer to a standard string?

Thanks in advance!
 
Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long Hope it helps

SC
 
Sendmessage is a peculiar one. You need to vary the declaration depending on what lParam represents. For instance I use,

Public Declare Function SendMessageLong 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 SendMessageString 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 SendMessageRect Lib "user32" Alias "SendMessageA" (ByVal HWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As RECT32) As Long

Public Declare Function SendMessageLineRect Lib "user32" Alias "SendMessageA" (ByVal HWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As tagLINERECT) As Long


Example of use:-

Type RECT32
Left As Long
TOp As Long
Right As Long
Bottom As Long
End Type

Dim txRect32 As RECT32
L = SendMessageRect(TX.HWnd, TX_GETRECT, 0, txRect32)
Peter Meachem
peter @ accuflight.com

 
If I understand your question correctly, then I think you would like to take the Long value returned from the SendMessage API, and treat that as if it were a pointer to char, and get the contents of that string (pointed to by the long) and copy into an actual string variable.

If that is correct, then you might want to try the following:

' Declare the following two API's
Code:
Public Declare Function StrLenA Lib "kernel32" Alias "lstrlenA" (ByVal Ptr As Long) As Long
Public Declare Function StrCopyA Lib "kernel32" Alias "lstrcpyA" (ByVal RetVal As String, ByVal Ptr As Long) As Long

' Add the following function which takes a Long as a parameter, and returns the corresponding string
Code:
Public Function PointerToAsciiStr(ByVal LongPtr As Long) As String

   Dim StrLeng As Long
   Dim StrValue As String
   Dim NullPos As Long
   Dim RetVal As Long
   
   PointerToAsciiStr = vbNullString
   If (LongPtr > 0) Then
      StrLeng = StrLenA(LongPtr)
      If (StrLeng > 0) Then
         StrValue = Space$(StrLeng + 1)
         RetVal = StrCopyA(StrValue, LongPtr)
         NullPos = InStr(StrValue, Chr$(0))
         If (NullPos > 0) Then
         PointerToAsciiStr = Left(StrValue, (NullPos - 1))
      Else
         PointerToAsciiStr = StrValue
      End If
   End If

End Function

Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top