Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 232
  • Last Modified:

How do I open any file in C or C++` in its native application?

I want the user to type in the path of the file they want to open and have it run as if you double clicked it.  This is in windowsXP.

I know how to prompt the user and get the string they enter, but am having trouble with the opening part.  I have tried WinExec and CreateProcess which seems to work for opening a specific application but not for taking a variable to point to a specific file.

I would prefer the answer in C if it is possible. C++ if not.

Thank you

1 Solution
The key API you need is FindExecutable, which takes a filename and folder and returns to you the fully qualified path of the executable that handles that extension.

#include <windows.h>
#include <stdlib.h>

void main() {
   char associatedExecutable[_MAX_PATH];
   char myFile[] = "temp1.txt";
   char myFolder[] = "C:\\Scratch";
   char systemString[_MAX_PATH * 2]; // Very Crude!

   HINSTANCE h = FindExecutable(myFile,myFolder,associatedExecutable);
   // This is crude, but illustrates the point
   strcat(systemString," ");
The easiest way I've found is to use ShellExecute: http://msdn2.microsoft.com/en-us/library/bb762153.aspx

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now