News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

Help for "Create a shortcut" example

Started by ognil, September 18, 2024, 05:43:33 AM

Previous topic - Next topic

ognil

Hi,

Need a help for "Create a shortcut" example from https://masm32.com/board/index.php?topic=12059.0

Quoteinclude \masm32\MasmBasic\MasmBasic.inc        ; ## COM demo: create shortcut ##
include IShellLink.inc
; credits to Gerardo Friedrich (BiteRider), Jaymeson Trudgen (NaN) and Ernest Murphy (source)
.code
CreateSC proc pPathObj, pPathLink
LOCAL WebInterface, pIPersistFile
  lea edi, WebInterface
  pIShellLink equ dword ptr [edi]
  .if rv(CoCreateInstance, offset CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, offset IID_IShellLink, edi)==S_OK
    CoInvoke pIShellLink, IShellLinkWVtbl.QueryInterface, addr IID_IPersistFile, addr pIPersistFile
    .if eax==S_OK
        CoInvoke pIShellLink, IShellLinkWVtbl.SetPath, pPathObj
        CoInvoke pIShellLink, IShellLinkWVtbl.SetIconLocation, pPathObj, 0
        .if eax==S_OK
            CoInvoke pIPersistFile, IPersistFileVtbl.Save, pPathLink, TRUE
            PrintLine Str$("Done, result=%i", eax)
        .endif
    .endif
  .endif
  ret
CreateSC endp
  SetGlobals src$, lnk$
  Init
  .if rv(OleInitialize, NULL)==S_OK
    wLet src$=wRec$(MbExeFolder$)+"ShortCut.asc"
    wLet lnk$=wRec$(MbExeFolder$)+"ScSource.lnk"
    invoke CreateSC, src$, lnk$
    invoke OleUninitialize
  .endif
EndOfCode

I could not understand and compile this example because there are no comments and it is written in a language unknown to me.
Is it possible for someone to translate it to MASM64, so I can compile it?

Thanks in advance :thumbsup:
"Not keeping emotions under control is another type of mental distortion."

stoo23

Hi 'ognil', that is written in "MasmBasic", written and maintained by a member here (jj2007) and has it's own forum section, so I have moved your post to that section  :smiley:

ognil

#2
Sorry Admin, for posting in the wrong forum. I was just following the advice of

zedd151 from here: https://masm32.com/board/index.php?topic=12262.0

Quoteby zedd151
One thing that you can do for yourself ognil, is make a posting in The Campus with any questions that you have about 64 bit assembly programming for Windows.  :thumbsup:  You should receive better responses there, than simply inquiring in an introductory post. 

Thank you
"Not keeping emotions under control is another type of mental distortion."

zedd151

Sorry ognil, I should have said 'with any general questions'. Your request is somewhat specific, as not all members here use MasmBasic. It will be better served here.  :smiley:  No one knows better how MasmBasic works than jj2007.   :biggrin:
:sad:

NoCforMe

Yes, JJ's the one to handle this.
Just to clue you in, MasmBasic is a dialect of BASIC that's actually assembly language (MASM-based) with a lot of stuff added on top of it, to allow you to use BASIC statements or assembly operations or a mix of both. I don't use it myself, but it's a great tool for those who like this kind of programming. One nice thing is that it's an IDE (integrated development environment), so you can edit, assemble and link all within the same application.
Assembly language programming should be fun. That's why I do it.

ognil

Admin and NoCforMe opened my eyes to what MasmBasic is.

Thank you guys. :thumbsup:
"Not keeping emotions under control is another type of mental distortion."

stoo23

QuoteSorry Admin, for posting in the wrong forum. I was just following the advice of
No need for apologies  :smiley:

As 'zedd' suggested correctly, the Campus IS a good place to ask questions, especially for people learning assembler.

jj2007

Quote from: ognil on September 18, 2024, 05:43:33 AMI could not understand and compile this example because there are no comments and it is written in a language unknown to me.

Most commands, e.g. CoInvoke and wLet, are explained in MasmBasic's help file. You need to install the package.

QuoteIs it possible for someone to translate it to MASM64, so I can compile it?

From what I see, you would be able to do it. Just take the example and port it...

Something like wLet lnk$=wRec$(MbExeFolder$)+"ScSource.lnk" is obviously not available in the Masm64 SDK, so you need to use buffers, MultiByteToWideChar etc

ognil

#8
Thank you JJ,

About MasmBasic, according to NoCforMe:
QuoteI don't use it myself, but it's a great tool ...

My university experience is with C++, MASM64, PHP, js, HTML5, CSS3 and Visual Studio as IDE.
At this stage, I prefer to invest my time in mastering these languages rather than learning new ones.

My expectations of getting a working MASM64 code were not met, but I solved the problem in my own way:

- I found the C++ source code from MS
https://learn.microsoft.com/en-us/windows/win32/shell/links#creating-a-shortcut-and-a-folder-shortcut-to-a-file
- I translated it to MASM64 and VoilĂ ...

Attachment is here:
https://masm32.com/board/index.php?topic=12272.0


; MSDN example
; https://learn.microsoft.com/en-us/windows/win32/shell/links#creating-a-shortcut-and-a-folder-shortcut-to-a-file

include  \masm32\include64\masm64rt.inc

.data

;********************************************
IID_IShellLinkA  dd 214EEh
dw    0,0,0c0h
db    0,0,0,0,0,46h
;********************************************
IID_IShellLinkW  dd 214F9h
dw    0,0,0c0h
db    0,0,0,0,0,46h
;********************************************
CLSID_ShellLink  dd 21401h
dw    0,0,0c0h
db    0,0,0,0,0,46h
;********************************************
IID_IPersistFile dd 10bh
dw    0,0,0c0h
db    0,0,0,0,0,46h
;********************************************
pIShellLink  dq 0
ppf          dq 0
lpszPathLink dw "t","o","N","o","t","e","p","a","d",".","l","n","k",0,0,0,0
lpszPathObj  db "C:\Windows\System32\notepad.exe",0
lpszDesc    db "Shortcut to Notepad",0
;*******************************************************************; 

.code
main                        proc                           
                            xor    ecx, ecx
                            mov    edx, 2
                            call    CoInitializeEx
                            call    Shortcut
                            call    CoUninitialize                   
                            xor    ecx, ecx
                            call    ExitProcess
main                        endp
;*******************************************************************; 
Shortcut                    proc
                           
                            lea        rax, pIShellLink                ; Result -> IID_IShellLinkW interface
                            lea        r9,  IID_IShellLinkA
                            mov        [rsp+4*8], rax
                            mov        r8d, 1                          ; 1 = CLSCTX_INPROC_SERVER
                            xor        edx, edx
                            lea        rcx, CLSID_ShellLink
                            call    CoCreateInstance
                            test    eax, eax                        ; eax = 0 -> Success
                            jne        Ret_0                ; return FALSE
                           
                            mov    rcx, pIShellLink                ; IID_IShellLinkW interface
                            lea    r8,  ppf                        ; Result -> IID_IPersistFile interface
                            mov    rax, [rcx]                      ; rax -> Vtbl with methods           
                            lea    rdx, IID_IPersistFile
                            call    qword ptr [rax+0*8]            ; IID_IShellLinkW::QueryInterface method = 0*8
                            test    eax, eax                        ; eax = 0 -> Success
                            jne        Ret_B                        ; return FALSE
                           
                            mov    rcx, pIShellLink                ; IID_IShellLinkW interface
                            mov    rax, [rcx]                      ; IID_IShellLink::IShellLinkAVtbl
                            lea    rdx, lpszPathObj                ; Pointer to ANSI string
                            call    qword ptr [rax+20*8]            ; IID_IShellLinkW::SetPath method in IShellLinkA = 20*8
                            test    eax, eax                        ; eax = 0 -> Success
                            jne        Ret_A   
                           
                            mov    rcx, pIShellLink                ; IID_IShellLinkW interface
                            lea    rdx, lpszDesc                  ; Pointer to ANSI string
                            mov    rax, [rcx]                      ; IID_IShellLinkW::IShellLinkWVtbl ; Vtbl with methods 
                            call    qword ptr [rax+7*8]            ; IID_IShellLinkW::SetDescription method in IShellLinkAVtbl = 7*8
                            test    eax, eax                        ; eax = 0 -> Success
                            jne        Ret_A             
                           
                            mov    rcx, ppf                        ; IID_IPersistFile interface
                            lea    rdx, lpszPathLink              ; Pointer to Unicode string
                            xor    r8d, r8d                        ; fRemember parameter = 0
                            mov    rax, [rcx]                      ; IID_IPersistFile::Vtbl with methods
                            call    qword ptr [rax+6*8]            ; IID_IPersistFile::Save method = 6*8
                            test    eax, eax                        ; eax = 0 -> Success
                            jne        Ret_A   
; ppf->Release()           
                            mov    rcx, ppf                        ; IID_IPersistFile interface
                            mov    rax, [rcx]                      ; IID_IPersistFile::Vtbl with methods
                            call    qword ptr [rax+2*8]            ; IID_IPersistFile::Release method
; pIShellLink->Release()   
                            mov    rcx, pIShellLink                ; IID_IShellLinkW interface
                            mov    rax, [rcx]                      ; IID_IShellLinkW::IShellLinkWVtbl ; Vtbl with methods
                            call    qword ptr [rax+2*8]            ; IID_IShellLinkW::Release method                   
                           
                            mov    eax, 1                          ; Return 1 -> success
                            ret
Ret_A:                     
; ppf->Release()           
                            mov    rcx, ppf                        ; IID_IPersistFile interface
                            mov    rax, [rcx]                      ; Vtbl with methods
                            call    qword ptr [rax+2*8]            ; IID_IPersistFile::Release method
Ret_B:                     
; pIShellLink->Release()   
                            mov    rcx, pIShellLink                ; IID_IShellLinkW interface
                            mov    rax, [rcx]                      ; Vtbl with methods
                            call    qword ptr [rax+2*8]            ; IID_IShellLinkW::Release method
Ret_0:                     
                            xor    eax, eax                        ; Error -> return FALSE
                            ret
Shortcut                    endp
;*******************************************************************;
end
"Not keeping emotions under control is another type of mental distortion."

jj2007

Quote from: ognil on September 19, 2024, 10:53:05 PMAttachment is here:
https://masm32.com/board/index.php?topic=12272.0

My compliments :thumbsup: