Get the executable name from extension

Share your advanced PureBasic knowledge/code with the community.
User avatar
flaith
Enthusiast
Enthusiast
Posts: 704
Joined: Mon Apr 25, 2005 9:28 pm
Location: $300:20 58 FC 60 - Rennes
Contact:

Get the executable name from extension

Post 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
Last edited by flaith on Thu Dec 29, 2011 11:07 am, edited 1 time in total.
“Fear is a reaction. Courage is a decision.” - WC
MachineCode
Addict
Addict
Posts: 1482
Joined: Tue Feb 22, 2011 1:16 pm

Re: Get the executable name from extension

Post 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$
Microsoft Visual Basic only lasted 7 short years: 1991 to 1998.
PureBasic: Born in 1998 and still going strong to this very day!
User avatar
flaith
Enthusiast
Enthusiast
Posts: 704
Joined: Mon Apr 25, 2005 9:28 pm
Location: $300:20 58 FC 60 - Rennes
Contact:

Re: Get the executable name from extension

Post 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
“Fear is a reaction. Courage is a decision.” - WC
MachineCode
Addict
Addict
Posts: 1482
Joined: Tue Feb 22, 2011 1:16 pm

Re: Get the executable name from extension

Post 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?
Microsoft Visual Basic only lasted 7 short years: 1991 to 1998.
PureBasic: Born in 1998 and still going strong to this very day!
User avatar
flaith
Enthusiast
Enthusiast
Posts: 704
Joined: Mon Apr 25, 2005 9:28 pm
Location: $300:20 58 FC 60 - Rennes
Contact:

Re: Get the executable name from extension

Post by flaith »

Yes i had to add those constants, Purebasic don't know them, for now :wink:
“Fear is a reaction. Courage is a decision.” - WC
User avatar
luis
Addict
Addict
Posts: 3895
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: Get the executable name from extension

Post 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.
"Have you tried turning it off and on again ?"
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5494
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: Get the executable name from extension

Post by Kwai chang caine »

Thanks for sharing FLAITH, very useful 8)
ImageThe happiness is a road...
Not a destination
User avatar
flaith
Enthusiast
Enthusiast
Posts: 704
Joined: Mon Apr 25, 2005 9:28 pm
Location: $300:20 58 FC 60 - Rennes
Contact:

Re: Get the executable name from extension

Post by flaith »

@All : thanks guys :D
@Luis : thanks for pointing that out, code modified :wink:
“Fear is a reaction. Courage is a decision.” - WC
Post Reply