Detect Shortcut Folder?

Started by mcjathan, October 04, 2015, 12:40:07 PM

Previous topic - Next topic

mcjathan

Greetings,

1) I'm running on a Windows 7 Pro x64 workstation.

2) I have this Winbatch script:

   Q:\bin\myscript.wil

3) I've copied and pasted the Winbatch script to another folder.  I pasted the Winbatch script as a shortcut.  I pasted the shortcut in this folder:

    R:\fred\

4) When I double-click the shortcut in R:\fred\ and run my script, I want the script to know the location of the shortcut (R:\fred\).  How can my Winbatch script determine the shortcut location (R:\fred\)?

Regards,

Jeff

....IFICantBYTE

Hi,
try one of these methods and see how you go:

Code (winbatch) Select
;A way to look at the startup info for the current calling process...
; ... the lpTitle will hold the path of the shortcut if one was used to launch the script (or compliled exe) ....IFICantBYTE
; for GetStartupInfo function reference see:  https://msdn.microsoft.com/en-us/library/windows/desktop/ms683230(v=vs.85).aspx
; see notes further down re. the unicode version

sKernel = StrCat(DirWindows(1), "kernel32.dll")

;3 versions of the same idea: comment-out any you can't or don't want to use on your system

;1 = Old way
#DefineFunction StringFromPointer(pointer)
   strFromPointer=""
   ByteValue=IntControl(32, pointer, "BYTE", 0, 0)
   While ByteValue
      strFromPointer=strFromPointer:Num2Char(ByteValue)
      pointer=pointer + 1
      ByteValue=IntControl(32, pointer, "BYTE", 0,0)
   EndWhile
   Return(strFromPointer)
#EndFunction

hSI = BinaryAlloc(68)
;BinaryPoke4(hSI,0,68);        DWORD   cb                  size of structure
;BinaryPoke4(hSI,4,0);         LPTSTR  lpReserved
;BinaryPoke4(hSI,8,0);         LPTSTR  lpDesktop
;BinaryPoke4(hSI,12,0);        LPTSTR  lpTitle
;BinaryPoke4(hSI,16,0);        DWORD   dwX
;BinaryPoke4(hSI,20,0);        DWORD   dwY
;BinaryPoke4(hSI,24,0);        DWORD   dwXSize
;BinaryPoke4(hSI,28,0);        DWORD   dwYSize
;BinaryPoke4(hSI,32,0);        DWORD   dwXCountChars
;BinaryPoke4(hSI,36,0);        DWORD   dwYCountChars
;BinaryPoke4(hSI,40,0);        DWORD   dwFillAttribute
;BinaryPoke4(hSI,44,0);        DWORD   dwFlags
;BinaryPoke2(hSI,48,0);        WORD    wShowWindow (SW_HIDE = 0)
;BinaryPoke2(hSI,50,0);        WORD    cbReserved2
;BinaryPoke4(hSI,52,0);        LPBYTE  lpReserved2
;BinaryPoke4(hSI,56,0);        HANDLE  hStdInput
;BinaryPoke4(hSI,60,0);        HANDLE  hStdOutput
;BinaryPoke4(hSI,64,0);        HANDLE  hStdError

DllCall(sKernel,void:"GetStartupInfoA",lpbinary:hSI)

Title = BinaryAlloc(4)
BinaryPoke4(Title,0,BinaryPeek4(hSI,12))
message("lpTitle ANSI old way",StringFromPointer(BinaryPeek4(hSI,12)))
BinaryFree(hSI)
;Exit


;2 = Unicode (Wide) call using new Struct methods - not sure why the returned string is sometimes truncated... is the Wide Win32 function doing this or is it the Struct stuff with unicode?.. the ANSI version seems to work
StructDescription = "DWORD:cb LPWSTR:lpreserved LPWSTR:lpDesktop LPWSTR:lpTitle DWORD:dwX DWORD:dwY DWORD:dwXSize DWORD:dwYSize DWORD:dwXCountChars DWORD:dwYCountChars DWORD:dwFillAttribute DWORD:dwFlags WORD:wShowWindow WORD:cbReserved2 BYTE:lpReserved2 HANDLE:hStdInput HANDLE:hStdOutput HANDLE:hStdError"
STARTUPINFO = DllStructAlloc(StructDescription)
DllCall(sKernel,void:"GetStartupInfoW",lpstruct:STARTUPINFO)
Message("lpTitle Unicode using Struct",DllStructPeek(STARTUPINFO,"lpTitle"))
DllStructFree(STARTUPINFO)
;Exit

;3 = ANSI version using new Struct method
StructDescription = "DWORD:cb LPSTR:lpreserved LPSTR:lpDesktop LPSTR:lpTitle DWORD:dwX DWORD:dwY DWORD:dwXSize DWORD:dwYSize DWORD:dwXCountChars DWORD:dwYCountChars DWORD:dwFillAttribute DWORD:dwFlags WORD:wShowWindow WORD:cbReserved2 BYTE:lpReserved2 HANDLE:hStdInput HANDLE:hStdOutput HANDLE:hStdError"
STARTUPINFO = DllStructAlloc(StructDescription)
DllCall(sKernel,void:"GetStartupInfoA",lpstruct:STARTUPINFO)
Message("lpTitle ANSI using Struct",DllStructPeek(STARTUPINFO,"lpTitle"))
DllStructFree(STARTUPINFO)
Exit


Regards,
....IFICantBYTE

Nothing sucks more than that moment during an argument when you realize you're wrong. :)

kdmoyers

Thanks!
Interesting to compare this with intcontrol(1004) and intcontrol(1006)
all close, but not the same.
-Kirby
The mind is everything; What you think, you become.

td

If you change the Shortcut's 'Start-in' folder setting to the be the shortcut's location then the DirScript function will report the shortcut's locations at script startup.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

td

Quote from: ....IFICantBYTE on October 04, 2015, 10:46:45 PM

2 = Unicode (Wide) call using new Struct methods - not sure why the returned string is sometimes truncated... is the Wide Win32 function doing this or is it the Struct stuff with unicode?.. the ANSI version seems to work


Didn't notice this comment originally but the truncation is the result of a problem in how the DllStructPeek function handles Unicode string.  This problem has already been addressed for the next release.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

....IFICantBYTE

Quote
Didn't notice this comment originally but the truncation is the result of a problem in how the DllStructPeek function handles Unicode string.  This problem has already been addressed for the next release.
ok that's good to know - thanks Tony.
Also... no big one, but I noticed when coding the above, that there is no LPBYTE data type for the struct definition .. I just used a BYTE instead and I didn't need to look at the contents the pointer refers to in this case anyway.
Regards,
....IFICantBYTE

Nothing sucks more than that moment during an argument when you realize you're wrong. :)

td

You can use any 4 byte integer data type for LPBYTE when using 32-bit Windows.  You can use DWORD_PTR, INT_PTR or LONG_PTR on either 32-bit or 64-bit Windows.   It is better to use BYTE * because it is more descriptive and portable between both 32-bit and 64-bit WinBatch. 

However you should not use BYTE because it is a single byte data type and pointers are either four or eight bytes.  In some cases you can get by with it because of compiler structure member alignment but you risk memory corruption in cases when alignment doesn't give you the extra space for the 3 additional bytes.   
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

....IFICantBYTE

Quote from: td on October 06, 2015, 06:43:50 AM
However you should not use BYTE because it is a single byte data type and pointers are either four or eight bytes.

Good point(er) Tony! ;)
Regards,
....IFICantBYTE

Nothing sucks more than that moment during an argument when you realize you're wrong. :)

td

Quote from: td on October 05, 2015, 06:47:28 AM
If you change the Shortcut's 'Start-in' folder setting to the be the shortcut's location then the DirScript function will report the shortcut's locations at script startup.

DirScript should be DirGet.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade