CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Dec 1999
    Posts
    85

    how to get file association of a file type for open and print

    Scratching my head here trying to find out how best this is done, seen projects to setup an assocation but not to read the association from the registry, anyone have any simple code to get the file assocation for a file by using the file extension e.g. looking for something like this:-

    CString fileAssoc = GetFileOpenAssocation("txt");
    returns e.g. "NOTEPAD.EXE"

    also for "print" assoaction e.g.

    CString filePrintAssoc = GetFileOpenAssocation("pdf");
    return's e.g. "AcroRd32.exe"

    To work on windows 98 to win7, preferably in VC6.

    Thanks,
    Hobnob

  2. #2
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    Re: how to get file association of a file type for open and print

    If you would agree to drop Win98 support, check out AssocQueryString Function
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinWindows - replacement windows manager for Visual Studio, and more...

  3. #3
    Join Date
    Dec 1999
    Posts
    85

    Re: how to get file association of a file type for open and print

    thanks, that worked a treat... bye bye win98

    DWORD BufferSize = MAX_PATH;
    char* path = new char[BufferSize];
    AssocQueryString(0, ASSOCSTR_EXECUTABLE, ".pdf", "print", path, &BufferSize);

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: how to get file association of a file type for open and print

    Since you are working in VS, why not use ATL::CString or MFC::CString?

    Code:
    CString sPath;
    DWORD dwSize = MAX_PATH;
    AssocQueryString(0, ASSOCSTR_EXECUTABLE, ".pdf", "print", path.GetBuffer( MAX_PATH ), &dwSize); 
    sPath.ReleaseBuffer();
    Allocating c-style strings is an opportunity for memory leaks. You can eliminate the potential problem by using string classes.

  5. #5
    Join Date
    Apr 1999
    Posts
    27,449

    Re: how to get file association of a file type for open and print

    Quote Originally Posted by hobnob View Post
    thanks, that worked a treat... bye bye win98

    DWORD BufferSize = MAX_PATH;
    char* path = new char[BufferSize];
    AssocQueryString(0, ASSOCSTR_EXECUTABLE, ".pdf", "print", path, &BufferSize);
    You know the exact size of the buffer, so why are you dynamically allocating anything? Just declare an array:
    Code:
    	DWORD BufferSize = MAX_PATH;
    	char path[MAX_PATH];
    	AssocQueryString(0, ASSOCSTR_EXECUTABLE, ".pdf", "print", path, &BufferSize);
    Regards,

    Paul McKenzie

  6. #6
    Join Date
    Dec 1999
    Posts
    85

    Re: how to get file association of a file type for open and print

    Sure that's better - worked on a more slick way without the lastest sdk libs and headers by calling the DLL shlwapi directly, this way is more OS platform friendly.

    extern "C" HRESULT ( STDAPICALLTYPE *pAssocQueryString )( UINT,UINT,LPCTSTR,LPCTSTR,LPCTSTR,LPDWORD ) = NULL;

    #define ASSOCSTR_EXECUTABLE 2

    void CTestassocDlg::OnOk()
    {

    HMODULE hMod = 0;

    if ( ( hMod = ::LoadLibrary( _T( "shlwapi.dll" ) ) ) != 0 )
    {
    pAssocQueryString= (HRESULT (__stdcall *)(UINT,UINT,LPCTSTR,LPCTSTR,LPCTSTR,LPDWORD ) )::GetProcAddress( hMod, "AssocQueryStringA" );
    }

    if ( pAssocQueryString )
    {
    DWORD BufferSize = MAX_PATH;
    char path[MAX_PATH];
    pAssocQueryString(0, ASSOCSTR_EXECUTABLE, ".pdf", "print", path, &BufferSize);

    }

    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured