C
CSharp
C++
Java
Visual Basic
HTML - CSS
Android
Asp
Asp.Net
Php
Python
JavaScript
SQL
XML
Assembly
ActiveX
API
Code Snippets
Database
Files Directories Drives
Forms
Graphics Games
Multimedia
Strings
Windows and Controls
More...
Visual Basic > API and Miscellaneous sample source codes
Visual Basic Code > Convert a pointer to a string into a string
Convert a pointer to a string into a string 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 '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
Privacy Policy
|
Contact
|
Advertising
|
Link to Us
|
Directory