-
July 18th, 2011, 10:23 AM
#1
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
-
July 18th, 2011, 10:58 AM
#2
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...
-
July 18th, 2011, 11:41 AM
#3
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);
-
July 18th, 2011, 12:32 PM
#4
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.
-
July 18th, 2011, 07:19 PM
#5
Re: how to get file association of a file type for open and print
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
-
July 19th, 2011, 04:46 AM
#6
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|