Results 1 to 4 of 4

Thread: CopyMemory with StrPtr

  1. #1

    Thread Starter
    Fanatic Member Geespot's Avatar
    Join Date
    Oct 2001
    Location
    Birmingham, UK
    Posts
    577

    CopyMemory with StrPtr

    Hi,

    Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
    Private Declare Sub ZeroMemory Lib "kernel32.dll" Alias "RtlZeroMemory" (Destination As Any, ByVal Length As Long)

    Private Sub Command1_Click()
    Dim strString As String * 10
    Dim myStr As String * 10

    ZeroMemory strString, 10
    ZeroMemory myStr, 10


    strString = "A_Word"
    CopyMemory ByVal myStr, ByVal StrPtr(strString), 10

    MsgBox myStr

    End Sub


    mystr only contains the letter 'A', how can i copy the rest of it?

    And i HAVE to copy the string from its memory location, note the ByVal StrPtr(strString)

    it works fine without the StrPtr but then it wont be know good to me then.

    What it is, i have a DLL that returns the address of a string that is 10 bytes long. I would like to know what is at that address (string wise)

    Thanks

  2. #2
    jim mcnamara
    Guest
    I think you may be going at this the hard way.

    Not knowing what decare you are using for your dll try this:

    Code:
    Declare Function MyDll Lib "mylib.dll" ( _ 
               ByRef szDesiredString As String
    ) As Long
    This declares a pointer to a string, which is what you are trying to get back.

    I'm sure there are other aguments, but that's okay.
    Most api's return & use null-terminated strings. If you are sure that 10 characters is the maximum you'll ever get:

    do this:
    Code:
    Dim mystr as string
    mystr = space$(10) & chr(0) ' make an empty string
    x = MyDll(mystr)
    If x = okay then
       mystr = left( mystr, instr(mystr,chr(0) )-1  )
       ' do stuff with the string
    end if

  3. #3

    Thread Starter
    Fanatic Member Geespot's Avatar
    Join Date
    Oct 2001
    Location
    Birmingham, UK
    Posts
    577
    I have managed to get it working now. Im making an app that will have plugin support, so users can add there own dlls at will.

    I solved the problem by using copymemory to a byte array ( arrMyStr( 1 to 10) as Byte ) and that has worked except that it is in the standard windows style 'A _ W o r d' *sigh*. easily fixed tho

  4. #4
    Fanatic Member Kaverin's Avatar
    Join Date
    Oct 2000
    Posts
    930
    So you want a string given only a pointer to it, correct? Modify your CopyMemory like this:
    VB Code:
    1. Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (ByVal lpDest As Long, ByVal lpSource As Long, ByVal nBytes As Long)
    Notice how the params are now given "as long" rather than "as any". Since it ultimately uses a long value, you may as well cut out the fudgy "any" type. I have a function I made a long time ago to give me a VB string if I only knew the pointer, and it uses that particular style to avoid potential problems.
    VB Code:
    1. Private Declare Function lstrlen Lib "kernel32" Alias "lstrlenW" (ByVal lpString As Long) As Long
    2. Private Function GetStringFromPointer(ByVal lpString As Long) As String
    3.    Dim lngLength As Long, strBuffer As String
    4.    lngLength = lstrlen(lpString) 'get length of string (in chars)
    5.    strBuffer = Space$(lngLength) 'make a buffer to copy to
    6.    CopyMemory StrPtr(strBuffer), lpString, lngLength * 2 'copy the string (note that this is a unicode string, so really there are 2 bytes per char
    7.    GetStringFromPointer = strBuffer 'return the copied string
    8. End Function
    9.  
    10. 'assume 1051 was your pointer, and you wanted the string
    11. Dim strX As String
    12. strX = GetStringFromPointer(1051)
    I'm baaaack...
    VB5 Professional Edition, VC++ 6
    Using a 1 gHz Thunderbird, 256 mb RAM, 40 gb HD system with Win98se

    I feel special because I finally figured out how to loop midis: Post link
    I'm a fanatic too

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