OK I really need some help here! I am not the best C programmer out there but I can usually find my way through. But this one has me stumped. The company I work for has been using an open source program to print PDF file from our accounting software for about 6 years. The program was originally written in V$C 6.0. Now in Abode's infinate wisdome they decided to change the name of the function that was used in the program when Adobe Reader 10.x was released. (I understand their reason, but...) I found and downloaded the program and I imported the program into a VS 2005 C project. I was able to modify it to work with Adobe 10.x but it would not work for Adobe 9.x or below. I did some research and found that Adobe was adding the application name in the Windows Registry so all I had to do was read the registry key and be done with it. Now after working on this for 2 days, I am frustrated that I can not get this to program to compile. I do not know what I am missing and any help would be MUCH appreciated! In the code below you can search on "adobeversion" and you can see what I am trying to do. I am trying to use the RegistryKey class but I get compile errors. I know this is long but I felt it would be better to see the entire program.
Thanks again in advance!!
#include <windows.h>
#include <ddeml.h>
#include <string.h>
#include <stdio.h>
#include "PrinterUtils.h"
#define TIMEOUT 60
int multiplier=1;
HWND hWndAdobe=0;
BOOL isRunning=FALSE;
BOOL wasRunning=TRUE;
char* adobeversion=" ";
const char* subkey="acrobat\\shell\\open\\ddeexec\\application";
void HideAdobe()
{
ShowWindow (hWndAdobe, SW_RESTORE);
MoveWindow (hWndAdobe, -100, -100, 0, 0, TRUE);
ShowWindow (hWndAdobe, SW_RESTORE);
}
BOOL isAdobeRunning()
{
hWndAdobe = FindWindow(NULL, "Adobe Reader");
isRunning = (hWndAdobe > 0 ? TRUE: FALSE);
return(isRunning);
}
void usage(char *p)
{
printf("usage:\t%s [options] [drive:][path]<filename>\n", p);
printf("specifies drive, directory, and/or file(s) to print.\n");
printf("prints to default printer unless [printer] specified.\n\n");
printf("options:\n");
printf("\t-p printer : print to specified printer (must be first option)\n");
printf("\t\tSpecify complete printer UNC name for network printers:\n");
printf("\t\t\t\"\\\\printserver\\printer\"\n");
printf("\t\t\tsurround with double-quotes if any spaces in printer name\n\n");
printf("\t-o orientation : set orientation\n");
printf("\t\t1=portrait, 2=landscape\n\n");
printf("\t-d duplex : set duplex mode (if supported by printer)\n");
printf("\t\t1=none, 2=long side, 3=short side\n\n");
printf("\t-c copies : set number of copies to print\n");
printf("\t\tcopies=1 to 10\n");
exit(0);
}
HDDEDATA CALLBACK DDECallback( UINT wType,
UINT wFmt,
HCONV hConv,
HSZ hsz1,
HSZ hsz2,
HDDEDATA hDDEData,
DWORD dwData1,
DWORD dwData2)
{
switch (wType) {
default:
return NULL;
break;
}
}
static BOOL SendExecCmd(LPSTR lpszCmd)
{
int test=0;
DWORD dwDDEInst = 0;
UINT ui;
HSZ hszApplication;
HSZ hszTopic;
HCONV hConv;
HDDEDATA hExecData;
ui = DdeInitialize(&dwDDEInst,
DDECallback,
CBF_FAIL_ALLSVRXACTIONS,
0l);
if (ui != DMLERR_NO_ERROR) {
return FALSE;
}
RegistryKey reg = (Registry.ClassesRoot).OpenSubKey(subkey);
adobeversion = reg.GetValue("(Default)");
hszApplication = DdeCreateStringHandle( dwDDEInst,
adobeversion,
CP_WINANSI);
hszTopic = DdeCreateStringHandle(dwDDEInst,
"control",
CP_WINANSI);
hConv = DdeConnect(dwDDEInst,
hszApplication,
hszTopic,
NULL);
DdeFreeStringHandle(dwDDEInst, hszApplication);
DdeFreeStringHandle(dwDDEInst, hszTopic);
if (!hConv) {
return FALSE;
}
hExecData = DdeCreateDataHandle(dwDDEInst,
lpszCmd,
lstrlen(lpszCmd)+1,
0,
NULL,
0,
0);
DdeClientTransaction( (void FAR *)hExecData,
(DWORD)-1,
hConv,
NULL,
0,
XTYP_EXECUTE,
TIMEOUT * 1000 * multiplier, NULL);
DdeDisconnect(hConv);
DdeUninitialize(dwDDEInst);
return TRUE;
}
BOOL pdfprint(LPSTR lpszPath, LPSTR lpszPrinter, LPSTR lpszPort, LPSTR lpszDriver)
{
char buf[2048];
int i;
BOOL ret;
FILE *f;
long lSize;
f = fopen(lpszPath, "r");
if(f == NULL)
return 0;
fseek(f,0,SEEK_END);
lSize=ftell(f);
fclose(f);
multiplier = (int)(lSize / 1000000)+1;
if(lpszPath && lstrlen(lpszPath)) {
if(lpszPrinter && lstrlen(lpszPrinter))
{
sprintf(buf,
"[FilePrintTo(\"%s\",%s,\"%s\",\"%s\")]",
lpszPath, lpszPrinter, lpszDriver, lpszPort);
}
else
{
sprintf(buf,
"[FilePrintSilent(\"%s\")]",
lpszPath);
}
}
printf("Printing: %s\n", lpszPath);
for(i=0;i<3;i++)
{
HideAdobe();
ret = SendExecCmd(buf);
if(ret)
break;
Sleep(1000);
}
return(ret);
}
int main(int argc, char* argv[])
{
char szPrinter[MAX_PATH];
char szPrinterName[MAX_PATH];
char szPort[MAX_PATH];
char szDriver[MAX_PATH];
char szFile[MAX_PATH];
char szPath[MAX_PATH];
char *p;
int i;
int s=1;
int nPrinted=0;
int val=0;
HANDLE hPrinter=0;
WIN32_FIND_DATA find_data;
HANDLE hFind;
PRINTER_INFO_2 *ppi2 = NULL;
LPDEVMODE pDevMode1 = NULL;
LPDEVMODE pDevMode2 = NULL;
BOOL bSetProperties = FALSE;
DWORD dwNeeded = 0;
BOOL bFlag;
char *pattern;
BOOL bFirst = TRUE;
LPTSTR lpFilePart;
DWORD dwStatus;
if (argc < 2)
{
usage(argv[0]);
return 0;
}
*szPrinter='\0';
*szPrinterName='\0';
GetDefaultPrinterName(szPrinterName, sizeof(szPrinterName));
pDevMode1 = GetPrinterDevMode(szPrinterName);
pDevMode2 = GetPrinterDevMode(szPrinterName);
for(i=1;i<argc;i++)>
{
p = argv[i];
if(*p == '-')
{
if(++i >= argc)
usage(argv[0]);
switch (*(++p)) {
case 'p' :
if(i != 2) {
printf("-p MUST be first option, if used\n");
usage(argv[0]);
}
strcpy(szPrinter, argv[i]);
if(OpenPrinter(szPrinter, &hPrinter, NULL)==0)
{
printf("Printer not found: '%s'\n", szPrinter);
return 1;
}
GetPrinter(hPrinter, 2, 0, 0, &dwNeeded);
if (dwNeeded == 0)
{
printf("Printer error\n");
ClosePrinter(hPrinter);
return 2;
}
ppi2 = (PRINTER_INFO_2 *)GlobalAlloc(GPTR, dwNeeded);
if (!ppi2)
{
printf("Malloc error\n");
ClosePrinter(hPrinter);
return 3;
}
bFlag = GetPrinter(hPrinter, 2, (LPBYTE)ppi2, dwNeeded, &dwNeeded);
if (!bFlag)
{
printf("Printer error\n");
ClosePrinter(hPrinter);
GlobalFree(ppi2);
return 4;
}
strcpy(szPort, ppi2->pPortName );
strcpy(szDriver, ppi2->pDriverName);
strcpy(szPrinterName, argv[i]);
ClosePrinter(hPrinter);
if(pDevMode1)
free(pDevMode1);
if(pDevMode2)
free(pDevMode2);
pDevMode1 = GetPrinterDevMode(szPrinterName);
pDevMode2 = GetPrinterDevMode(szPrinterName);
sprintf(szPrinter, "\"%s\"", argv[i]);
printf("Printer: %s\n", szPrinter);
continue;
case 'd' :
p = argv[i];
if(!isdigit(*p)) {
printf("Invalid Duplex Mode: %s\n", p);
}
else {
val = atoi(p);
if(val < 1 || val > 3)
usage(argv[0]);
pDevMode2->dmDuplex = val;
pDevMode2->dmFields |= DM_DUPLEX;
pDevMode1->dmFields |= DM_DUPLEX;
bSetProperties = TRUE;
}
continue;
case 'o' :
p = argv[i];
if(!isdigit(*p)) {
printf("Invalid Orientation Mode: %s\n", p);
}
else {
val = atoi(p);
if(val < 1 || val > 2)
usage(argv[0]);
pDevMode2->dmOrientation = val;
pDevMode2->dmFields |= DM_ORIENTATION;
pDevMode1->dmFields |= DM_ORIENTATION;
bSetProperties = TRUE;
}
continue;
case 'c' :
p = argv[i];
if(!isdigit(*p)) {
printf("Invalid Number of Copies: %s\n", p);
}
else {
val = atoi(p);
if(val < 1 || val > 10)
usage(argv[0]);
pDevMode2->dmCopies = val;
pDevMode2->dmFields |= DM_COPIES;
pDevMode1->dmFields |= DM_COPIES;
bSetProperties = TRUE;
}
continue;
default :
continue;
}
}
if(bSetProperties)
SetPrinterProperties(szPrinterName, pDevMode2);
pattern = argv[i];
hFind = FindFirstFile(pattern, &find_data);
if (hFind != INVALID_HANDLE_VALUE)
{
do {
strcpy(szPath, argv[i]);
p = strrchr(szPath, '\\');
if(p == NULL)
*szPath = '\0';
else
*(p+1) = '\0';
strcat(szPath, find_data.cFileName);
dwStatus = GetFullPathName(szPath,
MAX_PATH,
szPath,
&lpFilePart);
ZeroMemory (szFile, MAX_PATH);
FindExecutable(szPath, NULL, szFile);
if (!szFile[0])
{
printf("'%s' not found or Adobe Acrobat or Reader not installed.\n", szPath);
continue;
}
p = strrchr(szPath,'.');
if(p==NULL)
{
printf("Not a PDF file: '%s'\n", szPath);
continue;
}
if( (stricmp(p,".pdf")!=0) && (stricmp(p,".fdf")!=0) && (stricmp(p,".xfdf")!=0) )
{
printf("Not a PDF file: '%s'\n", szPath);
continue;
}
bFirst = FALSE;
if(!isAdobeRunning())
{
wasRunning=FALSE;
ShellExecute(NULL,"open",szFile,NULL,NULL,SW_HIDE);
Sleep(3000);
HideAdobe();
if(!isAdobeRunning())
Sleep(3000);
HideAdobe();
}
if(pdfprint(szPath, szPrinter, szPort, szDriver))
nPrinted++;
} while (FindNextFile(hFind, &find_data));
}
FindClose(hFind);
}
if(!wasRunning)
{
if(isAdobeRunning()) {
Sleep(1000);
PostMessage (hWndAdobe, WM_CLOSE, 0, 0);
}
}
if(bSetProperties)
SetPrinterProperties(szPrinterName, pDevMode1);
if(pDevMode1)
free(pDevMode1);
if(pDevMode2)
free(pDevMode2);
printf("%d File(s) Printed\n", nPrinted);
return(nPrinted);
}
</filename>