Adobe Reader DDE автоматизация
Пример автоматизации работы Adobe Reader с помощью DDE-интерфейса. Этот пример предназначен для
работы в Microsoft Visual C++ версии 6.0 или выше. Adobe Reader версии 5.0 или выше должен быть
установлен на вашем компьютере.
#include <DDEML.H>
inline void AppProcessMessages()
{
MSG msg;
while ( PeekMessage(&msg, 0 , 0 , 0 , PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
HDDEDATA CALLBACK DdeCallback(
UINT uType,
UINT uFmt,
HCONV hconv,
HSZ hsz1,
HSZ hsz2,
HDDEDATA hdata,
DWORD dwData1,
DWORD dwData2)
{
return 0 ;
}
void ExecuteQuery( CString stQuery, BOOL bSync, DWORD dwIdInst, HCONV hConv )
{
if ( !dwIdInst || !hConv )
{
OutputDebugString( "\t ExecuteQuery Error\n" );
return ;
}
HDDEDATA hData = DdeCreateDataHandle(dwIdInst, (LPBYTE)( stQuery.GetBuffer( stQuery.GetLength() + 1 ) ), stQuery.GetLength(), 0, 0, CF_TEXT, 0);
if ( !hData )
{
CString msg = "\t ExecuteQuery: Command failed: \"" + stQuery + "\"\n" ;
OutputDebugString( (LPCTSTR)msg );
return ;
}
DWORD dwTimeout = TIMEOUT_ASYNC;
if ( bSync )
dwTimeout = TIMEOUT_ASYNC - 1 ;
DdeClientTransaction( (LPBYTE)hData, -1 , hConv, 0L , 0 , XTYP_EXECUTE, dwTimeout, 0 );
}
BOOL ReaderPrint( CString sExec, CString sPDFFile, CString sPrinter, CString sPrinterDrv, CString sPrinterPort, float fReaderVer )
{
HSZ hszApp(0 ), hszTopic(0 );
CString stDDECmdLine;
DWORD dwIdInst = 0 ;
HCONV hConv = 0 ;
if ( DMLERR_NO_ERROR != DdeInitialize( &dwIdInst, (PFNCALLBACK)DdeCallback, APPCLASS_STANDARD | APPCMD_CLIENTONLY, 0 ) )
return 0 ;
if ( fReaderVer < 10 .f )
hszApp = DdeCreateStringHandle( dwIdInst, "acroview" , 0 );
{
CString s;
s.Format( "AcroViewR%d" , (int )fReaderVer );
hszApp = DdeCreateStringHandle( dwIdInst, s, 0 );
}
hszTopic = DdeCreateStringHandle( dwIdInst, "control" , 0 );
if ( !( hConv = DdeConnect( dwIdInst, hszApp, hszTopic, 0 ) ) )
{
SHELLEXECUTEINFO si = { sizeof ( SHELLEXECUTEINFO ) };
si.lpVerb = "open" ;
si.lpFile = sExec;
si.nShow = SW_HIDE | SW_MINIMIZE;
if ( ShellExecuteEx( &si ) )
{
do
{
AppProcessMessages();
Sleep( 300 );
}
while ( !( hConv = DdeConnect( dwIdInst, hszApp, hszTopic, 0 ) ) );
}
}
DdeFreeStringHandle( dwIdInst, hszApp );
DdeFreeStringHandle( dwIdInst, hszTopic );
if ( !hConv )
{
OutputDebugString( "\t CAdobePrint: DDE Connection Failed in ReaderPrint(...)\n" );
return 0 ;
}
ExecuteQuery( "[DocOpen(\"" + sPDFFile + "\")]" , 0 , dwIdInst, hConv );
if ( fReaderVer >= 8 .f )
ExecuteQuery( "[DocOpen(\"" + sPDFFile + "\")]" , 0, dwIdInst, hConv );
ExecuteQuery( "[FilePrintTo(\"" + sPDFFile + "\",\"" + sPrinter + "\",\"" + sPrinterDrv + "\",\"" + sPrinterPort +"\")]" , 0 , dwIdInst, hConv );
ExecuteQuery( "[DocClose(\"" + sPDFFile + "\")]" , 0 , dwIdInst, hConv );
ExecuteQuery( "[AppExit()]" , 0 , dwIdInst, hConv );
if ( hConv )
{
DdeDisconnect( hConv );
hConv = 0 ;
}
if ( dwIdInst )
{
DdeUninitialize( dwIdInst );
dwIdInst = 0 ;
}
return 1 ;
}
Back