Q How do I open a file using the application associated with its extension in Windows?

A If you're writing a .NET application, then life is simple! Use System.Diagnostics.Process.Start(fileName).

A If you're writing C++, then use the Win32 function ShellExecute(). This examines the Windows' registry to find the application associated with a file extension. You can specify various parameters that modify how the application starts and how it displays a document. Visit MSDN and search all topics to find more information.

I've wrapped the function as shown below. It passes MicroStation's native window handle, but that's not essential.

void DisplayFileWithDefaultViewer (const char* path, const char* file)
{
	HWND		hWnd;
	mdlWindow_nativeWindowHandleGet (&hWnd, mdlDialog_overallTitleBarGet (), HANDLETYPE_HWND);
	HINSTANCE hInst = ::ShellExecute (hWnd,
            nullptr,            //	Verb, e.g. 'open'		
            file,               //	Document to open		
            nullptr,            //	Params if executable	
            path,               //	Default directory		
            SW_SHOWNORMAL);     //	Window display mode		
}

The example above was implemented in a C++ DLL.

Return to MicroStationAPI articles index.