Page 1 of 1

Get the executable name from extension

Posted: Wed Dec 28, 2011 12:28 pm
by flaith
Hello all,

as i needed to get the name of the executable from the association name, i did that little snippet
If someone could find it useful

Code: Select all

Enumeration
  #ASSOCSTR_COMMAND              = 1
  #ASSOCSTR_EXECUTABLE
  #ASSOCSTR_FRIENDLYDOCNAME
  #ASSOCSTR_FRIENDLYAPPNAME
  #ASSOCSTR_NOOPEN
  #ASSOCSTR_SHELLNEWVALUE
  #ASSOCSTR_DDECOMMAND
  #ASSOCSTR_DDEIFEXEC
  #ASSOCSTR_DDEAPPLICATION
  #ASSOCSTR_DDETOPIC
  #ASSOCSTR_INFOTIP
  #ASSOCSTR_QUICKTIP
  #ASSOCSTR_TILEINFO
  #ASSOCSTR_CONTENTTYPE
  #ASSOCSTR_DEFAULTICON
  #ASSOCSTR_SHELLEXTENSION
  #ASSOCSTR_DROPTARGET
  #ASSOCSTR_DELEGATEEXECUTE
  #ASSOCSTR_MAX
EndEnumeration

Enumeration 
  #ASSOCF_INIT_NOREMAPCLSID      = $00000001
  #ASSOCF_INIT_BYEXENAME         = $00000002
  #ASSOCF_OPEN_BYEXENAME         = $00000002
  #ASSOCF_INIT_DEFAULTTOSTAR     = $00000004
  #ASSOCF_INIT_DEFAULTTOFOLDER   = $00000008
  #ASSOCF_NOUSERSETTINGS         = $00000010
  #ASSOCF_NOTRUNCATE             = $00000020
  #ASSOCF_VERIFY                 = $00000040
  #ASSOCF_REMAPRUNDLL            = $00000080
  #ASSOCF_NOFIXUPS               = $00000100
  #ASSOCF_IGNOREBASECLASS        = $00000200
  #ASSOCF_INIT_IGNOREUNKNOWN     = $00000400   
EndEnumeration

pcchOut.l   = #MAX_PATH
pszOut.s    = Space(pcchOut)
flags       = #ASSOCF_NOTRUNCATE | #ASSOCF_REMAPRUNDLL
pszAssoc.s  = ".htm"
pszExtra.s  = "open"

If OpenLibrary(0, "Shlwapi.dll")
  *AssocQueryString = GetFunction(0, "AssocQueryStringA")
  If *AssocQueryString
    hresult = CallFunctionFast(*AssocQueryString, flags, #ASSOCSTR_EXECUTABLE, @pszAssoc, @pszExtra, @pszOut, @pcchOut)
    Select hresult
      Case #S_OK
        Debug "Executable name for '"+pszAssoc+"' : "+pszOut
      Case #S_FALSE
        Debug "pszOut is NULL. pcchOut contains the required buffer size"
      Case #E_POINTER
        Debug "The pszOut buffer is too small to hold the entire string"
      Default
        Debug "No association defined for extension '"+pszAssoc+"'"
    EndSelect
  EndIf
EndIf
** Edit ** : Modified pszOut and call to a pointer, thank to Luis :D

Re: Get the executable name from extension

Posted: Wed Dec 28, 2011 1:27 pm
by MachineCode
You call that a "little snippet"? Try this for size instead. :)

Code: Select all

f$="c:\test.txt" ; Full path to a valid file must be specified.
e$=Space(999) : FindExecutable_(GetFilePart(f$),GetPathPart(f$),@e$)
Debug e$

Re: Get the executable name from extension

Posted: Wed Dec 28, 2011 2:23 pm
by flaith
MachineCode wrote:You call that a "little snippet"? Try this for size instead. :)

Code: Select all

f$="c:\test.txt" ; Full path to a valid file must be specified.
e$=Space(999) : FindExecutable_(GetFilePart(f$),GetPathPart(f$),@e$)
Debug e$
Agreed, but here you need to declare (to create) a filename, in my code you only need to give the extension part and then you will have the executable name which load that kind of file :wink:
About the little snippet, yes it's a big snippet :D

Re: Get the executable name from extension

Posted: Wed Dec 28, 2011 2:29 pm
by MachineCode
True about needing an existing file, but it's not normally a problem. As for the snippet size, I didn't test it, but do you really have to put in all those enumerations? Wouldn't PureBasic already know them?

Re: Get the executable name from extension

Posted: Wed Dec 28, 2011 2:52 pm
by flaith
Yes i had to add those constants, Purebasic don't know them, for now :wink:

Re: Get the executable name from extension

Posted: Wed Dec 28, 2011 8:20 pm
by luis
@flaith

Thanks, it can be useful !

Please note pcchOut should be @pcchOut, it's a pointer, if you didn't get a IMA was by chance :)

And I would say space(255) should be Space(pcchOut) or Space(#MAX_PATH) or whatever.

Re: Get the executable name from extension

Posted: Wed Dec 28, 2011 8:59 pm
by Kwai chang caine
Thanks for sharing FLAITH, very useful 8)

Re: Get the executable name from extension

Posted: Thu Dec 29, 2011 11:09 am
by flaith
@All : thanks guys :D
@Luis : thanks for pointing that out, code modified :wink: