News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

MOVing one string to another... not working.

Started by Trope, March 26, 2005, 06:15:26 PM

Previous topic - Next topic

Trope

I have a string that I declared like this:



yname              db 20h dup (0)



and I populate it like this:



invoke GetDlgItemTextA, hWnd, IDC_EDIT1, offset yname, 49




Now what I would like to do is SAVE this to another variable. I declared a buffer like this:



namebuffer         dd ?




How do I get yname into namebuffer? I have tried M2M, I have tried using the EAX register - nothing. I am missing something simple here I believe.

Thanks!

Giovanni



hitchhikr

yeah, this is not visual basic.

To copy a string into another one you can do this:


yname db 20h dup (0)
namebuffer db 20h dup (0)

invoke GetDlgItemTextA, hWnd, IDC_EDIT1, offset yname, 49
invoke lstrcpy, addr namebuffer, addr yname


Or if it is just to create a pointer to the first buffer:


yname db 20h dup (0)
namebuffer dd 0

mov eax,offset yname
mov namebuffer,eax


Trope

ahhhhhh lstrcpy.. i knew i missed something.

thanks so much.


raymond

QuoteI have a string that I declared like this:

yname              db 20h dup (0)

and I populate it like this:

invoke GetDlgItemTextA, hWnd, IDC_EDIT1, offset yname, 49

You are reserving only 32 bytes for the "yname" variable, and then you allow the "GetDlgItemTextA" function to write as many as 49 characters at that location. That could trash some of your other data if IDC_EDIT1 contains more than 31 characters.

Raymond
When you assume something, you risk being wrong half the time
http://www.ray.masmcode.com

Trope

Good catch. Would this just truncate the data or cause an error?

hitchhikr

If the string is too large, GetDlgItemTextA may try to write where it can't (producing an access violation) or where it shouldn't (overwrite the content of the following variables or code in memory) or both.

hutch--

Giovanni,

There is a procedure in the MASM32 library called szCopy that will do the copy task for you and you have the source available to see how its done.

A couple of tricks,

Allocate the buffer to receive the data from the API call in the UNinitialised data section. You keep the EXE file smaller this way.


.data?
  buffer db 256 dup (?)


You can use code like this to do the copy to a buffer,


  invoke szCopy,ADDR source,ADDR buffer
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php