Mega Code Archive

 
Categories / Delphi / Examples
 

ShellExecute

Title: ShellExecute Question: Ever wanted to open a .doc in Word? Or a .htm in Internet Explorer? Then you should use ShellExecute: Answer: HINSTANCE ShellExecute( HWND hwnd, // Handle to parent window, usually self.Handle LPCTSTR lpOperation, // pointer to string that specifies operation to perform // Possible values are: // 'open' : Open File // 'print' : Print file // 'explore': The function explores the // folder specified by lpFile. LPCTSTR lpFile, // pointer to filename or folder name string LPCTSTR lpParameters, // pointer to string that specifies executable-file parameters LPCTSTR lpDirectory, // pointer to string that specifies default directory INT nShowCmd // whether file is shown when opened. You have a lot of options. // Among them are: // SW_HIDE, SW_SHOWDEFAULT, SW_SHOWMINIMIZED and so on... ); Ready-To-Use function: This function simplifies the use of ShellExecute, and is able to raise and exception depending on the error: uses ShellApi; function ShellOpenFile( hWnd : HWND; AFileName, AParams, ADefaultDir : string ) : integer; begin result := shellapi.ShellExecute( hWnd, 'open', pChar( AFileName ), pChar(AParams), pChar(ADefaultDir), SW_SHOWDEFAULT ); case result of 0 : raise Exception.Create( 'The operating system is out of memory or resources.' ); ERROR_FILE_NOT_FOUND : raise Exception.Create( 'The specified file was not found.' ); ERROR_PATH_NOT_FOUND : raise Exception.Create( 'The specified path was not found.' ); ERROR_BAD_FORMAT : raise Exception.Create( 'The .EXE file is invalid (non-Win32 .EXE or error ' + 'in .EXE image).' ); SE_ERR_ACCESSDENIED : raise Exception.Create( 'The operating system denied access to the specified file.' ); SE_ERR_ASSOCINCOMPLETE : raise Exception.Create( 'The filename association is incomplete or invalid.' ); SE_ERR_DDEBUSY : raise Exception.Create( 'The DDE transaction could not be completed because ' + 'other DDE transactions were being processed.' ); SE_ERR_DDEFAIL : raise Exception.Create( 'The DDE transaction failed.' ); SE_ERR_DDETIMEOUT : raise Exception.Create( 'The DDE transaction could not be completed because the ' + 'request timed out.' ); SE_ERR_DLLNOTFOUND : raise Exception.Create( 'The specified dynamic-link library was not found.' ); SE_ERR_NOASSOC : raise Exception.Create( 'There is no application associated with the given ' + 'filename extension.' ); SE_ERR_OOM : raise Exception.Create( 'There was not enough memory to complete the operation.' ); SE_ERR_SHARE : raise Exception.Create( 'A sharing violation occurred.' ); else end; end; Usage: hWnd: Handle to owner window. Usually you would use self.Handle. AFileName: Name of file to be opened. AParams: Additional parameters. ADefaultDir: The default (working) directory. Examples // Opens applog.txt in the notepad: ShellOpenFile( self.Handle, 'c:\applog.txt', '', '' ); // Opens the document in Word: ShellOpenFile( self.Handle, 'mydoc.doc', '', '' ); // Opens a web page in the default browser: ShellOpenFile( self.Handle, 'http://www.caos.dk', '', '' ); // Opens your default e-mail client ShellOpenFile( self.Handle, 'mailto:caos@caos.dk', '', '' );