FindExecutable

Share your advanced PureBasic knowledge/code with the community.
dige
Addict
Addict
Posts: 1410
Joined: Wed Apr 30, 2003 8:15 am
Location: Germany
Contact:

FindExecutable

Post by dige »

Code: Select all

Procedure.s FindExecutable (File.s); Retrieves the name to the executable (.exe) file associated with a specific document file
  Protected libid.i, FuncId.i, *mem, Result.s
  ; http://msdn.microsoft.com/en-us/library/bb776419%28VS.85%29.aspx  
  
  libid = OpenLibrary(#PB_Any, "shell32.dll" )
  If libid
    FuncId = GetFunction(libid, "FindExecutableA" )
    If FuncId
      *mem = AllocateMemory(#MAX_PATH)
      If *mem
        If CallFunctionFast(FuncId, @File, #Null, *mem ) > 32
          Result = PeekS(*mem, #PB_Any, #PB_Ascii)
        EndIf
        FreeMemory(*mem)
      EndIf  
    EndIf
    CloseLibrary(libid)
  EndIf
  ProcedureReturn Result
EndProcedure 
Debug "C:\Temp\Document.pdf"
Last edited by dige on Wed Dec 08, 2010 9:43 am, edited 1 time in total.
"Daddy, I'll run faster, then it is not so far..."
User avatar
Joakim Christiansen
Addict
Addict
Posts: 2452
Joined: Wed Dec 22, 2004 4:12 pm
Location: Norway
Contact:

Re: FindExecutable

Post by Joakim Christiansen »

That's a handy one!
I like logic, hence I dislike humans but love computers.
User avatar
Rings
Moderator
Moderator
Posts: 1435
Joined: Sat Apr 26, 2003 1:11 am

Re: FindExecutable

Post by Rings »

whats wrong with that one:

Code: Select all

Result.s=Space(#MAX_PATH)
r=FindExecutable_(@File.s,@Path.s,@Result.s)
SPAMINATOR NR.1
dige
Addict
Addict
Posts: 1410
Joined: Wed Apr 30, 2003 8:15 am
Location: Germany
Contact:

Re: FindExecutable

Post by dige »

I prefer to have more control over api calls. Its also more safe for
use with different windows versions... or for futur usage against
deprecated functions...
"Daddy, I'll run faster, then it is not so far..."
cas
Enthusiast
Enthusiast
Posts: 597
Joined: Mon Nov 03, 2008 9:56 pm

Re: FindExecutable

Post by cas »

I agree with Rings. Except if you are coding your app for older windows version than XP (which doesn't have this function available), there is no point of doing it manually.
MSDN wrote:Returns a value greater than 32 if successful, or a value less than or equal to 32 representing an error.
Shouldn't then instead of

Code: Select all

If CallFunctionFast(FuncId, @File, #Null, *mem ) >= 32
be this:

Code: Select all

If CallFunctionFast(FuncId, @File, #Null, *mem ) > 32
?
dige
Addict
Addict
Posts: 1410
Joined: Wed Apr 30, 2003 8:15 am
Location: Germany
Contact:

Re: FindExecutable

Post by dige »

@CAS: you're right! I've fixed the code above.
cas wrote:I agree with Rings. Except if you are coding your app for older windows version than XP (which doesn't have this function available), there is no point of doing it manually.
If you're goin to distribut programs, you'll wonder what Windows versions still in use...
"Daddy, I'll run faster, then it is not so far..."
Post Reply