Am writing a program to find the binary directory of any executable, so I use FindExecutable() but sometimes it does not find the executable and i want the program to know that the Executable has not been found but i don't know how, please help.

#include <windows.h>
#include <iostream.h>
#include <stdio.h>
#include <string.h>

int main(int argc, char **argv)
{
 if (argc == 2) //Try to see if the user Entered a command
 {   
	 char rgvalue[100];
     
 FindExecutable(argv[1], NULL, rgvalue);
 printf("%s", rgvalue);
 
 

 }
 return 0;
}

<< moderator edit: added [code][/code] tags >>

First of all, the function has 3 return values:

SE_ERR_OOM - System out of memory (WinXP only IIRC)
SE_ERR_FNF - File not Found
SE_ERR_NOASSOC - The file type has no associated exe

So, you could do something like this

int rValue=FindExecutable(argv[1], NULL, rgvalue);

if(rValue==SE_ERR_OOM)
   printf("System out of memory/resources");
else if(rValue==SE_ERR_FNF)
   printf("File not found");
else if(rValue==SE_ERR_NOASSOC)
   printf("There's no associated executable with the given file type");
else
   printf("%s", rgvalue);

Now, theres an error in the buffer:

char rgvalue[100];

Should actually be

char rgvalue[MAX_PATH];

You need o have that in account when working with paths in windows, since there might be a path longer than 100 (whjat you specified), but its never longer than MAX_PATH (a compiler global define)

Not that you probably dont know, but this function is meant to search for the executable associated with a filename, for example *.doc->word,*.txt->notepad and so on, but you knew that already dontcha?

Anyway, I hope that helps, good luck

cheers

It won't work:

error C2440: 'initializing' : cannot convert from 'struct HINSTANCE__ *' to 'int'
This conversion requires a reinterpret_cast, a C-style cast or function-style cast

Thanks

Uppppssss, my bad, I forgot HINSTANCE is a long, not an int...

int rValue=(int)FindExecutable(argv[1], NULL, rgvalue);

...

if(rValue==(int)SE_ERR_OOM)
   printf("System out of memory/resources");
else if(rValue==(int)SE_ERR_FNF)
   printf("File not found");
else if(rValue==(int)SE_ERR_NOASSOC)
   printf("There's no associated executable with the given file type");
else
   printf("%s", rgvalue);

That should do it

cheers

This article has been dead for over six months. Start a new discussion instead.