Author Topic: String From Pointer  (Read 465 times)

0 Members and 1 Guest are viewing this topic.

Aurel

  • Sr. Member
  • ****
  • Posts: 402
String From Pointer
« on: October 02, 2018, 10:10:29 AM »
Charles
when i looking into Jose post i want try one VB example about thematicand here is code:
Code: [Select]
'Get String from  Pointer
! lstrlenA Lib "kernel32.dll" (ByVal lpString As Long) As Long
! PutMem4  Lib "msvbvm60.dll" (ByVal Addr As Long, ByVal NewVal As Long) As Long
! SysAllocString  Lib "oleaut32.dll" (Optional ByVal pszStrPtr As Long) As Long
! SysAllocStringByteLen Lib "oleaut32.dll"(Optional ByVal pszStrPtr As Long, Optional ByVal Length As Long) As String
! SysReAllocString Lib "oleaut32.dll" (ByVal pBSTR As Long, Optional ByVal pszStrPtr As Long) As Long

declare function GetStrFromPtrA(ByVal Ptr As Long) As String
 
'test
'Returns a copy of a null-terminated ANSI string (LPSTR/LPCSTR) from the given pointer
Function GetStrFromPtrA(ByVal Ptr As Long) As String
     Function = SysAllocStringByteLen( Ptr, lstrlenA(Ptr))
End Function



string s = GetStrFromPtrA(100)

and compiler complain that variable ptr is not defined because is in local scope
or maybe something else ?
just asking...
my site:BLOG and FORUM
https://aurelsoft.ucoz.com/

Charles Pegge

  • Admin Support Member
  • *****
  • Posts: 4205
    • Oxygen Basic
Re: String From Pointer
« Reply #1 on: October 02, 2018, 12:50:18 PM »
Hi Aurel,

Ptr is a reserved word, so changing to aPtr solves the problem

Code: [Select]
'Get String from  Pointer
! lstrlenA Lib "kernel32.dll" (ByVal lpString As Long) As Long
! PutMem4  Lib "msvbvm60.dll" (ByVal Addr As Long, ByVal NewVal As Long) As Long
! SysAllocString  Lib "oleaut32.dll" (Optional ByVal pszStrPtr As Long) As Long
! SysAllocStringByteLen Lib "oleaut32.dll"(Optional ByVal pszStrPtr As Long, Optional ByVal Length As Long) As String
! SysReAllocString Lib "oleaut32.dll" (ByVal pBSTR As Long, Optional ByVal pszStrPtr As Long) As Long

declare function GetStrFromPtrA(ByVal aPtr As Long) As String
 
'test
'Returns a copy of a null-terminated ANSI string (LPSTR/LPCSTR) from the given pointer
Function GetStrFromPtrA(ByVal aPtr As Long) As String
     Function = SysAllocStringByteLen( aPtr, lstrlenA(aPtr))
End Function

Josť Roca

  • Sr. Member
  • ****
  • Posts: 263
Re: String From Pointer
« Reply #2 on: October 02, 2018, 03:46:17 PM »
And using a true pointer, instead of 100, will also help :)