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
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
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);
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.
Re: how to get file association of a file type for open and print
Quote:
Originally Posted by
hobnob
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
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);
}
}