VC + + implementation file and application Association

Source: Internet
Author: User

Daily work, the doc file directly double-click, you can start the word software, and read the contents of the document in the software display, which is due to the configuration of the registry, our software also need to implement such a function, how to write the registry and write what? The following two functions are available for this function. Checkfilerelation is to check whether the file format we expect has been associated with the appropriate software in the registry, and Registerfilerelation is writing the relevant key and value directly to the registry.

/***************************************************** Detection File association * strext: The extension to be detected (for example: ". txt") * Strappkey: EXEName extension in the registry key value (for example: "txtfile") * Returns true: Indicates an association, FALSE: Represents an association ***************************************************** */BOOL checkfilerelation (Const Char*strext,Const Char*Strappkey) {    intnret=FALSE;    HKEY Hextkey; CharSzpath[_max_path]; DWORD dwsize=sizeof(szpath); if(RegOpenKey (hkey_classes_root,strext,&hextkey) = =error_success) {RegQueryValueEx (Hextkey,null,null,null, (LPBYTE) szpath,&dwsize); if(_stricmp (szpath,strappkey) = =0) {Nret=TRUE;        } regclosekey (Hextkey); returnNret; }    returnnret;}
/***************************************************** Registry File Association * Strexe: The extension to be detected (for example: ". txt") * Strappname: The application name to associate ( For example: "C:/myapp/myapp.exe") * strappkey:exename extension in the registry key value (for example: "txtfile") * Strdefaulticon: An icon file with the extension Strappname (for example: * "C :/myapp/myapp.exe,0 ") * Strdescribe: File type description ****************************************************/voidRegisterfilerelation (Char*strext,Char*strappname,Char*strappkey,Char*strdefaulticon,Char*strdescribe) {    CharStrtemp[_max_path];    HKEY 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_s (strtemp,"%s\\defaulticon", Strappkey); RegCreateKey (HKEY_CLASSES_ROOT, strtemp,&HKey); RegSetValue (HKey,"", REG_SZ, Strdefaulticon, strlen (Strdefaulticon) +1);    RegCloseKey (HKey); sprintf_s (strtemp,"%s\\shell", Strappkey); RegCreateKey (HKEY_CLASSES_ROOT, strtemp,&HKey); RegSetValue (HKey,"", REG_SZ,"Open", strlen ("Open") +1);    RegCloseKey (HKey); sprintf_s (strtemp,"%s\\shell\\open\\command", Strappkey); RegCreateKey (HKEY_CLASSES_ROOT, strtemp,&HKey); sprintf_s (strtemp,"%s%%1", Strappname); RegSetValue (HKey,"", REG_SZ, strtemp, strlen (strtemp) +1); RegCloseKey (HKey);}

With these two functions, you can implement the relationship between the document and the software, but after double-clicking the document, how to read the contents of the document? The main use of command-line arguments here, we need to get command-line arguments in Ctestapp's InitInstance function, such as:

BOOL ctestapp::initinstance () {    /// m_lpCmdLine Here is a member variable of CWinApp, and when you double-click the document, the path of the document is passed to the parameter    CString pathName = m_lpcmdline;     if (PathName! = _t (""))    {      //TODO: Read file, parse file, render file     }}

VC + + implementation file and application Association

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.