Hey, #1 is it possible to search for a specific exe file on a computer and return the path to it.

If not is it possible to get the directory of an exe from its running process.

If not is it possible to search for a registry entry to return the path to it?

If so to any of those could someone kindly point me in the direction im looking for

Thanks guys :)

Recommended Answers

All 3 Replies

Here is a snippet I found somewhere on the internet quite some time ago, with a little extra code added.
This code manually searches through every file on your C drive (which may take a while) and prints it to the console. With this you should be able to figure out how to search for a file quite easily.

#include <windows.h>
#include <iostream>
using namespace std;

void FindAllFiles(
      const char* searchThisDir,
      bool searchSubDirs,
      void (*UserFunc)(const char*))
{
  // What we need to search for files
  WIN32_FIND_DATA FindFileData = {0};
  HANDLE hFind = INVALID_HANDLE_VALUE;

  // Build the file search string
  char searchDir[2048] = {0};

  // If it already is a path that ends with \, only add the *
  if( searchThisDir[strlen(searchThisDir) - 1] == '\\' ) {
    _snprintf( searchDir, 2047, "%s*", searchThisDir );
  } else {
    _snprintf( searchDir, 2047, "%s\\*", searchThisDir );
  }

  // Find the first file in the directory.
  hFind = FindFirstFile( searchDir, &FindFileData );

  // If there is no file, return
  if ( hFind == INVALID_HANDLE_VALUE ) {
    return;
  }

  do {
    // If it's the "." directory, continue
    if ( strcmp(FindFileData.cFileName, ".") == 0 ) {
      continue;
    }

    // If it's the ".." directory, continue
    if ( strcmp(FindFileData.cFileName, "..") == 0 ) {
      continue;
    }		

    // If we find a directory
    if ( FindFileData.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY ) {
      // If we want to search subdirectories
      if ( searchSubDirs ) {
        // Holds the new directory to search
        char searchDir2[2048] = {0};

        // If it already is a path that ends with \, only add the *
        if ( searchThisDir[strlen(searchThisDir) - 1] == '\\' ) {
          _snprintf(
            searchDir2,
            2047,
            "%s%s",
            searchThisDir,
            FindFileData.cFileName );
        } else {
          _snprintf(
            searchDir2,
            2047,
            "%s\\%s",
            searchThisDir,
            FindFileData.cFileName );
        }
        FindAllFiles(searchDir2, true, UserFunc);
      }
      continue;
    }

    // Create an path to the file
    char filePath[2048] = {0};
    _snprintf(
      filePath,
      2047,
      "%s\\%s",
      searchThisDir,
      FindFileData.cFileName );

    // If there is a file found, pass it to the user function
    if ( UserFunc ) {
      UserFunc( filePath );
    }
  }
  // Loop while we find more files
  while ( FindNextFile(hFind, &FindFileData) != 0 );

  // We are done with the finding
  FindClose( hFind );
}

void OnFile(const char *filename) {
  cout << filename << '\n';
}

int main() {
  FindAllFiles( "C:\\", true, OnFile );
  cin.ignore();
}

Hope this helps :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.