The MASM Forum

General => The Campus => Topic started by: Shintaro on August 17, 2014, 09:43:56 PM

Title: Calling invoke 2 times
Post by: Shintaro on August 17, 2014, 09:43:56 PM
Hi,

When I call the following invoke's, one straight after the other, the different strings (szDeskTop and szAddDataDir)  end up with the same contents.

szDeskTop LPWSTR ?
szAppDataDir LPWSTR ?

      invoke SHGetFolderPath,0,CSIDL_DESKTOP,0,0,ADDR szDeskTop

      invoke SHGetFolderPath,0,CSIDL_APPDATA,0,0,ADDR szAppDataDir

Am I meant to reset or clear something between invoke calls?

Regards.
Title: Re: Calling invoke 2 times
Post by: qWord on August 17, 2014, 09:57:28 PM
You are passing the address of a pointer-variable, but the function expect the address of a char. buffer. (length=MAX_PATH)
Title: Re: Calling invoke 2 times
Post by: Shintaro on August 21, 2014, 07:05:08 PM
Thank you for your help.

Is the following:
szDeskTop db dup 260 (0)

the same as
sz[MAX_PATH]:CHAR

Or is there some bounds checking in MAX_PATH?

So how do I determine if the string of chars being returned from the API call is greater than 260?

Regards.
Title: Re: Calling invoke 2 times
Post by: dedndave on August 21, 2014, 10:01:42 PM
szDeskTop db dup 260 (0)
incorrect syntax - and you can put it in the .DATA? section, if you use...
szDeskTop db 260 dup(?)
or
szDeskTop db MAX_PATH dup(?)
this allocates space in the uninitialized data section
it is "valid" for the lifetime of the process

sz[MAX_PATH]:CHAR
again, incorrect syntax
    LOCAL szDeskTop[MAX_PATH]:CHAR
this allocates space inside a PROC, on the stack
it is only valid for the life of the call

when working with pathnames, i find it's better to use the UNICODE version of the function
it's a little bit of a headache, as many functions require it to be prepended with "\\?\"
but, the length limit is extended to 32768 TCHAR's (64 KB)
and, it will handle non-ascii characters   :t
i would then use either a heap allocated or a stack allocated buffer (after probing the stack)
Title: Re: Calling invoke 2 times
Post by: Shintaro on August 22, 2014, 01:17:30 AM
Thanks for that.

I thought that

.data
szDeskTop db dup 260 (0)


meant that the 260 bytes would be allocated at compile time (making the binary bigger) and that the whole 260 bytes would contain 0 (zero's). I remember a class in C telling me to initialize variables/types etc with known values and not leave them in an unknown state.


So in C I would use something like SizeOf() before data was moved to a variable, hence not allowing a buffer overflow. Not sure how I accomplish that in Asm.

I would appreciate it if you could point me to any relevant doc.
I don't think Iczelions docs are enough. Is there example code in the masm directory that I have overlooked?

I appreciate your help.

Kind regards.
Title: Re: Calling invoke 2 times
Post by: qWord on August 22, 2014, 01:37:36 AM
Quote from: aparsons on August 22, 2014, 01:17:30 AMI remember a class in C telling me to initialize variables/types etc with known values and not leave them in an unknown state.
This applies to local variables (on stack). Variables in the .data?-segment (BSS) are zero initialized and are not allowed to have any other initial value. If none-zero values are required, use .data or .const.

Quote from: aparsons on August 22, 2014, 01:17:30 AMSo in C I would use something like SizeOf() before data was moved to a variable, hence not allowing a buffer overflow.
It is a good sign that worry about that, but in this case it is that the function SHGetFolderPath (http://msdn.microsoft.com/en-us/library/windows/desktop/bb762181%28v=vs.85%29.aspx) does not -- by definition -- write more than MAX_PATH characters to the buffer (otherwise it will fail --> eax != S_OK).
Title: Re: Calling invoke 2 times
Post by: Grincheux on December 24, 2015, 06:21:26 AM
An other version of

Quote
szDeskTop LPWSTR ?
szAppDataDir LPWSTR ?
      invoke SHGetFolderPath,0,CSIDL_DESKTOP,0,0,ADDR szDeskTop
      invoke SHGetFolderPath,0,CSIDL_APPDATA,0,0,ADDR szAppDataDir

could be :

Quote
             push OFSFET szAppDataDir
             push 0
             push 0
             push CSIDL_APPDATA
             push 0
             push OFFSET @NextAddr
             push OFFSET szDesktop
             push 0
             push 0
             push CSIDL_DESKTOP
             push 0
             push OFFSET ShGetFolderPath
             jmp ShGetFolderPath


@NextAddr :
No time spend for coming back to your code and then make an other call!
Shorter than 2 calls.

If szDesktop was local, replace :

Quote
             push OFFSET szDesktop
by :
Quote
             lea eax,szDesktop
             push eax

This is for the 32 bit version.
In 64 bits after the first call is made, don't correct the stack.
just modify "ADDR szAppDataDir" by "szDesktop"
then issue the call without passing the other parameters.
This is good if RCX, RDX, R8 and R9 are not modified.
After you correct the stack

I used this technique for creating children windows. Only in 5 lines for the second child.
Title: Re: Calling invoke 2 times
Post by: guga on December 24, 2015, 06:30:06 AM
Hi Grincheux, did you got my msg ?
Title: Re: Calling invoke 2 times
Post by: Grincheux on December 24, 2015, 06:56:28 AM
Yes I made the answers.
And in my answers I was speaking about this post.
Title: Re: Calling invoke 2 times
Post by: qWord on December 24, 2015, 07:17:46 AM
This thread is 1 year old!

Quote from: Grincheux on December 24, 2015, 06:21:26 AM
[...]
This is for the 32 bit version.
In 64 bits after the first call is made, don't correct the stack.
just modify "ADDR szAppDataDir" by "szDesktop"
then issue the call without passing the other parameters.
This is good if RCX, RDX, R8 and R9 are not modified.
After you correct the stack

I used this technique for creating children windows. Only in 5 lines for the second child.
Buggy technique, because called functions can modify parameters on stack.
Title: Re: Calling invoke 2 times
Post by: Grincheux on December 24, 2015, 07:49:58 AM
If it was 5 five years old I made my post, the time is not a problem.
Does it mean that after one year it is forbidden to read/answer to a post?