Tuesday, 19 July 2011

Get File Association in C++ for open and print - directly from shlwapi dll

This is a slick way without the lastest sdk libs and headers by calling the DLL shlwapi directly, this way is more OS platform friendly.
NOTE: this function is not available in win98

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);

}

}

No comments:

Post a Comment