VB and VBA Users Source Code: Convert a pointer to a string into a string
[
Home
|
Contents
|
Search
|
Reply
| Previous | Next ]
VB/VBA Source Code
Convert a pointer to a string into a string
By:
Andrew Baker
Email (spam proof):
Email the originator of this post
Date:
Friday, January 26, 2001
Hits:
814
Category:
Windows API
Article:
There are various instances (generally when using APIs) where it is necassary to convert a pointer to a string, back into a string. The following function returns a string from a pointer (a demonstration routine is at the bottom of this post): Option Explicit Private Declare Sub CopyMem Lib "kernel32" Alias "RtlMoveMemory" (pTo As Any, uFrom As Any, ByVal lSize As Long) Private Declare Function lstrlenW Lib "kernel32" (ByVal lpString As Long) As Long 'Purpose : Converts a pointer to a string into a string. 'Inputs : lPtr A long pointer to a string held in memory 'Outputs : The string held at the specified memory address 'Author : Andrew Baker 'Date : 03/08/2000 19:14 'Notes : 'Revisions : Private Function StrFromPtr(ByVal lPtr As Long) As String Dim lLen As Long Dim abytBuf() As Byte 'Get the length of the string at the memory location lLen = lstrlenW(lPtr) * 2 - 1 'Unicode string (must double the buffer size) If lLen > 0 Then ReDim abytBuf(lLen) 'Copy the memory contents 'into a they byte buffer Call CopyMem(abytBuf(0), ByVal lPtr, lLen) 'convert and return the buffer StrFromPtr = abytBuf End If End Function 'Demonstration routine Sub Test() Dim sTest As String sTest = "Andrew" Debug.Print StrFromPtr(StrPtr(sTest)) End Sub
Terms and Conditions
Support this site
Download a trial version of the Excel Workbook Rebuilder