The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: Trope on March 26, 2005, 06:15:26 PM

Title: MOVing one string to another... not working.
Post by: Trope on March 26, 2005, 06:15:26 PM
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


Title: Re: MOVing one string to another... not working.
Post by: hitchhikr on March 26, 2005, 06:27:29 PM
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

Title: Re: MOVing one string to another... not working.
Post by: Trope on March 26, 2005, 06:31:59 PM
ahhhhhh lstrcpy.. i knew i missed something.

thanks so much.

Title: Re: MOVing one string to another... not working.
Post by: raymond on March 26, 2005, 06:39:15 PM
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
Title: Re: MOVing one string to another... not working.
Post by: Trope on March 26, 2005, 07:48:22 PM
Good catch. Would this just truncate the data or cause an error?
Title: Re: MOVing one string to another... not working.
Post by: hitchhikr on March 26, 2005, 10:08:57 PM
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.
Title: Re: MOVing one string to another... not working.
Post by: hutch-- on March 27, 2005, 02:47:18 AM
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