/******************************************************************** * Modified by Nattee Niparnan * date: 2007 Mar 3 * http://our.obor.us/ */ /********************************************************************* ADOBE SYSTEMS INCORPORATED Copyright (C) 1994-2003 Adobe Systems Incorporated All rights reserved. NOTICE: Adobe permits you to use, modify, and distribute this file in accordance with the terms of the Adobe license agreement accompanying it. If you have received this file from a source other than Adobe, then your use, modification, or distribution of it requires the prior written permission of Adobe. *********************************************************************/ #include #include /*------------------------------------------------------- Constants/Declarations -------------------------------------------------------*/ const char *REG_INSTALL_KEY = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths\\Acrobat.exe"; const char *ACRO_DDESERVER = "acroview"; const char *ACRO_DDETOPIC = "control"; const int MAX_CMD_LENGTH = 1000; const DWORD MAX_TIMEOUT = 3000, STEP_SIZE = 200; HDDEDATA CALLBACK DDE_ProcessMessage (UINT uType, UINT uFmt, HCONV hconv, HSZ hsz1, HSZ hsz2, HDDEDATA hdata, DWORD dwData1, DWORD dwData2); /*------------------------------------------------------- Implementation -------------------------------------------------------*/ /* WinMain ** ------------------------------------------------------ ** ** Main entrypoint for application. */ int main(int argc,char **argv) { long lRetCode; HKEY hkey; DWORD pathBuf[MAX_PATH + 1]; DWORD size = MAX_PATH + 1; bool shouldOpen = argc >= 3; char pdfFile[1024]; if (argc == 1) { printf("usage: AcroDDE [filename] [open flag]\n"); return -1; } //convert file extension, if extension is given, change it to "pdf", if not, add ".pdf" if ( argv[1][strlen(argv[1])-4] == '.') { //has extension strcpy(pdfFile,argv[1]); pdfFile[strlen(pdfFile)-3] = 'p'; pdfFile[strlen(pdfFile)-2] = 'd'; pdfFile[strlen(pdfFile)-1] = 'f'; } else { sprintf(pdfFile,"%s.pdf",argv[1]); } // Determine if a PDF viewer is installed. if (shouldOpen) { lRetCode = RegOpenKeyEx(HKEY_LOCAL_MACHINE, REG_INSTALL_KEY, 0, KEY_READ, &hkey); if (lRetCode == ERROR_SUCCESS) { // Get the path to the viewer executable and launch it. lRetCode = RegQueryValueEx (hkey, "", 0, 0, (unsigned char *)pathBuf, &size); RegCloseKey (hkey); STARTUPINFO startupInfo; memset (&startupInfo, 0, sizeof(STARTUPINFO)); startupInfo.cb = sizeof(STARTUPINFO); PROCESS_INFORMATION processInfo; memset (&processInfo, 0, sizeof(PROCESS_INFORMATION)); // Launch the viewer. printf("openning %s\n",pathBuf); CreateProcess ((const char *)pathBuf, NULL, NULL, NULL, false, NORMAL_PRIORITY_CLASS, NULL, NULL, &startupInfo, &processInfo); } else { // Advertise the problem and bail. MessageBox (NULL,"No PDF Viewer installed. Aborting...", "DdeOpen - Error",MB_OK); return -1; } } char ddeCmdBuf[1024]; UINT retVal; DWORD id = 0; DWORD dwSleep = 0; // Start the DDE work now that the viewer is launched. memset (ddeCmdBuf, 0, sizeof(ddeCmdBuf)); //strcpy (ddeCmdBuf, DDE_CMD_STRING); retVal = DdeInitialize (&id, &DDE_ProcessMessage, APPCMD_CLIENTONLY, 0); if (DMLERR_NO_ERROR == retVal) { DWORD dwResult; HCONV hConversation = NULL; HSZ hszServerName, hszTopicName; // Initialize DDE conversation with server. hszServerName = DdeCreateStringHandle (id, ACRO_DDESERVER, 0); hszTopicName = DdeCreateStringHandle (id, ACRO_DDETOPIC, 0); // Acrobat can take a while to launch. We repeatedly attempt // to connect to the server until MAX_TIMEOUT expires. do { hConversation = DdeConnect (id, hszServerName, hszTopicName, NULL); if (hConversation || (dwSleep > MAX_TIMEOUT)) break; // Give Acrobat some more time to launch.... Sleep (dwSleep += STEP_SIZE); } while (true && shouldOpen); if (!hConversation) { //MessageBox (NULL, "Could not connect to server.", "DdeOpen - Error", MB_OK); } else { // Execute the DDE Command. // even the user wish to close the file, we still need to call DocOpen // so that the document is added into a dde-manipulable list sprintf(ddeCmdBuf,"[DocOpen(\"%s\")]",pdfFile); printf("sending %s\n",ddeCmdBuf); DdeClientTransaction ((unsigned char *)ddeCmdBuf, (DWORD)strlen(ddeCmdBuf), (HCONV)hConversation, NULL, (UINT)CF_TEXT, (UINT)XTYP_EXECUTE, (DWORD)1000, &dwResult); if (shouldOpen == false ) { sprintf(ddeCmdBuf,"[DocClose(\"%s\")]",pdfFile); printf("sending %s\n",ddeCmdBuf); DdeClientTransaction ((unsigned char *)ddeCmdBuf, (DWORD)strlen(ddeCmdBuf), (HCONV)hConversation, NULL, (UINT)CF_TEXT, (UINT)XTYP_EXECUTE, (DWORD)1000, &dwResult); } DdeDisconnect (hConversation); } // Release resources. DdeFreeStringHandle (id, hszServerName); DdeFreeStringHandle (id, hszTopicName); DdeUninitialize (id); } else { //MessageBox (NULL, "Failed to initialize DDE.", "DdeOpen - Error", MB_OK); } } HDDEDATA CALLBACK DDE_ProcessMessage (UINT uType, UINT uFmt, HCONV hconv, HSZ hsz1, HSZ hsz2, HDDEDATA hdata, DWORD dwData1, DWORD dwData2) { return NULL; } /******************************************************************** * Modified by Nattee Niparnan * date: 2007 Mar 3 * http://our.obor.us/ */