In daily work, after you double-click the DOC file, you can start the wordsoftware and read the content of the file to be displayed in the software. This is due to the Registry configuration, our software also needs to implement such a function. How should we write the registry and what content should we write? The following two functions can implement this function. Checkfilerelation is to check whether the expected file format has been associated with the corresponding software in the registry; registerfilerelation is to directly write the relevant key and value to the Registry.
/* **************************************** * *********** Check file association information * strext: the extension (for example :". TXT ") * strappkey: key value of the exename extension in the Registry (for example," txtfile ") * returns true: indicates that the key is associated, and false: this parameter indicates that it is not associated ************************************* **************** */ Bool checkfilerelation ( Const Char * Strext, Const Char *Strappkey ){ Int Nret = False; hkey hextkey; Char Szpath [_ max_path]; DWORD dwsize = Sizeof (Szpath ); If (Regopenkey (hkey_classes_root, strext, & hextkey) = Error_success) {regqueryvalueex (hextkey, null, null, (lpbyte) szpath, & Dwsize ); If (_ Stricmp (szpath, strappkey) = 0 ) {Nret = True;} regclosekey (hextkey ); Return Nret ;} Return Nret ;}
/* **************************************** * ************ Registration File Association * strexe: the extension (for example :". TXT ") * strappname: application to be associatedProgramName (for example, "C:/MyApp/myapp.exe") * key value of the strappkey: exename extension in the Registry (for example, "txtfile") * strdefaulticon: * strdescribe: file type description ************************************* ************** */ Void Registerfilerelation ( Char * Strext, Char * Strappname, Char * Strappkey, Char * Strdefaulticon, Char * Strdescribe ){ Char Strtemp [_ max_path]; hkey; regcreatekey (hkey_classes_root, strext, & Hkey); regsetvalue (hkey, "" , REG_SZ, strappkey, strlen (strappkey) + 1 ); Regclosekey (hkey); regcreatekey (hkey_classes_root, strappkey, & Hkey); regsetvalue (hkey, "" , REG_SZ, strdescribe, strlen (strdescribe) + 1 ); Regclosekey (hkey); sprintf (strtemp, " % S // defaulticon " , Strappkey); regcreatekey (hkey_classes_root, strtemp, & Hkey); regsetvalue (hkey, "" , REG_SZ, strdefaulticon, strlen (strdefaulticon) +1 ); Regclosekey (hkey); sprintf (strtemp, " % S // Shell " , Strappkey); regcreatekey (hkey_classes_root, strtemp, & Hkey); regsetvalue (hkey, "" , REG_SZ, " Open " , Strlen ( " Open " ) +1 ); Regclosekey (hkey); sprintf (strtemp, " % S // shell // open // command " , Strappkey); regcreatekey (hkey_classes_root, strtemp, & Hkey); sprintf (strtemp, " % S/ " % 1 / "" , Strappname); regsetvalue (hkey, "" , REG_SZ, strtemp, strlen (strtemp) + 1 ); Regclosekey (hkey );}
With these two functions, You can associate the document with the software. But after you double-click the document, how does one read the document content? The command line parameters are used here. We need to obtain the command line parameters in the initinstance function of ctestapp, for example:
Bool ctestapp: initinstance (){//Here m_lpcmdline is a member variable of cwinapp. When you double-click a document, the document path will be passed to this parameter.Cstring pathname=M_lpcmdline;If(Pathname! = _ T ("")){//Todo: read, parse, and render files
}}