返回首页

好吧,我真的需要一些帮助,在这里!我不是最好的C程序员那里,但我通常可以找到我的方式通过。但是,这可难倒我了。我工作的公司已经使用的开放源码程序打印PDF文件,从约6年的会计软件。该方案最初是在V $ C + + 6.0编写。在居留权的infinate wisdome的现在,他们决定改变; 10.X被释放时的程序中使用的函数的名称。 (我理解他们的理由,但是......)我发现和下载程序,我导入一个VS 2005 C项目的程序。我可以修改它与Adobe 10.X工作,但它不会工作; 9.x或以下。我做了一些研究,发现了Adobe在Windows注册表中添加应用程序的名称,所以我不得不这样做是读取注册表项,并用它做。现在这方面的工作后2天,我很沮丧,我不能得到这个程序编译。我不知道我错过任何帮助将不胜感激!在下面的代码,你可以搜索"adobeversion",你可以看到我想要做什么。我试图使用RegistryKey类,但我得到的编译错误。我知道这是漫长的,但我认为这将是更好地看到整个程序。

再次提前感谢!


#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=" ";

//char szAdobe[MAX_PATH];

//HKEY hKey=0;

//char regbuf[255]="";

//DWORD dwType=0;

//DWORD dwBufSize=sizeof(regbuf);

const char* subkey="acrobat\\shell\\open\\ddeexec\\application";

 



// Try to find the version of Adobe installed on the workstation

/*char getPDFVersion()

{

	 if( RegOpenKey(HKEY_CLASSES_ROOT, subkey, &hKey) == ERROR_SUCCESS)

	{

		dwType = REG_SZ;

		if ( RegQueryValueEx(hKey, "(Default)", 0, &dwType, (BYTE*)regbuf, &dwBufSize) == ERROR_SUCCESS)

		{

			return 1;

		}

		else

		{

			return 0;

		}

	}

	else

	{

		return 0;

	}

} */

 

// Hide Adobe Window 

// Adobe Reader 7 always pops up when printing, ignores SW_HIDE, so hide it offscreen

void HideAdobe() 

{

	ShowWindow (hWndAdobe, SW_RESTORE);

	MoveWindow (hWndAdobe, -100, -100, 0, 0, TRUE);

	ShowWindow (hWndAdobe, SW_RESTORE);

}

 



// Determine if Acrobat or Reader is running

BOOL isAdobeRunning()

{

	// Search all windows for title starting with szWnd

    // hWndAdobe = FindWindow("AdobeAcrobat", NULL);

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

}

 

// Callback function for DDE messages

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;

	}

}

 



// Send a DDE execute request to Acrobat/Reader 

static BOOL SendExecCmd(LPSTR lpszCmd)

{

	int test=0;

	DWORD dwDDEInst = 0;

	UINT ui;

	HSZ hszApplication;

	HSZ hszTopic;

	HCONV hConv;

	HDDEDATA hExecData;

	

	// Initialize DDEML

	ui = DdeInitialize(&dwDDEInst,

			DDECallback,

			CBF_FAIL_ALLSVRXACTIONS,

			0l);

 

	if (ui != DMLERR_NO_ERROR) {

		return FALSE;

	}

 

	// Initiate a conversation with the acroview application

	// on the control topic.

	

/* Jim Whalen - 11-10-2011 The release of Reader 10.X does not work with this application

                           Found we need to change the commad acroview to acroviewR10

*/

/*	hszApplication = DdeCreateStringHandle( dwDDEInst,

						"acroview",

						CP_WINANSI);

*/

	//test=getPDFVersion();

	//if (test != 1)

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

 

	// Free the HSZ now

	DdeFreeStringHandle(dwDDEInst, hszApplication);

	DdeFreeStringHandle(dwDDEInst, hszTopic);

 

	if (!hConv) {

		return FALSE;

	}

 

	// Create a data handle for the exec string

	hExecData = DdeCreateDataHandle(dwDDEInst,

					lpszCmd,

					lstrlen(lpszCmd)+1,

					0,

					NULL,

					0,

					0);

 

	// Send the execute request

	DdeClientTransaction(	(void FAR *)hExecData,

				(DWORD)-1,

				hConv,

				NULL,

				0,

				XTYP_EXECUTE,

				TIMEOUT * 1000 * multiplier, // ms timeout

				NULL);

 

	// Done with the conversation now.

	DdeDisconnect(hConv);

 

	// Done with DDEML

	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;

 

	// Find the file's length

	f = fopen(lpszPath, "r");

	if(f == NULL)

		return 0;

 

	fseek(f,0,SEEK_END);

	lSize=ftell(f);

	fclose(f);

 

	// Extend the timeout for each MB of filesize

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

	}

 

	//HideAdobe();

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

 

	// Get default printer name

	GetDefaultPrinterName(szPrinterName, sizeof(szPrinterName));

 

	// Get default printer settings

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

						// -p must be first option, if used

						printf("-p MUST be first option, if used\n");

						usage(argv[0]);

					}

 

					strcpy(szPrinter, argv[i]);

					

					// Make sure printer name is valid

					if(OpenPrinter(szPrinter, &hPrinter, NULL)==0)

					{

						// If not valid, return

						printf("Printer not found: '%s'\n", szPrinter);

						return 1;

					}

 

					// Find out how much space is needed

					GetPrinter(hPrinter, 2, 0, 0, &dwNeeded);

					if (dwNeeded == 0)

					{

						printf("Printer error\n");

						ClosePrinter(hPrinter);

						return 2;

					}

 

					// Allocate enough space for PRINTER_INFO_2...

					ppi2 = (PRINTER_INFO_2 *)GlobalAlloc(GPTR, dwNeeded);

					if (!ppi2)

					{

						printf("Malloc error\n");

						ClosePrinter(hPrinter);

						return 3;

					}

 

					// The second GetPrinter() will fill in all the current information

					// so that all you need to do is modify what you're interested in...

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

 

					// Get default printer settings

					pDevMode1 = GetPrinterDevMode(szPrinterName);

					pDevMode2 = GetPrinterDevMode(szPrinterName);

 

					// Wrap printer name in double quotes to pass as arg to DDE

					// in case there are spaces in the name

					sprintf(szPrinter, "\"%s\"", argv[i]);

					printf("Printer: %s\n", szPrinter);

 

					continue;

 

				case 'd' :

					// Set duplex mode

					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' :

					// Set Orientation

					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' :

					// Set Copies

					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;

 

/*  This doesn't appear to be possible...

				case 'n' :

					// Set N-Up

					p = argv[i];

					if(!isdigit(*p)) {

						printf("Invalid N-Up setting: %s\n", p);

					}

					else {

						val = atoi(p);

						if(val != 1 && val != 2 && val != 4 && val != 6 && val != 9)

							usage(argv[0]);

 

						pDevMode2->dmScale = (100 / val);

						printf("Scale:%d\n", pDevMode2->dmScale);

						pDevMode2->dmFields |= DM_SCALE;

						pDevMode1->dmFields |= DM_SCALE;

						bSetProperties = TRUE;

					}

 

					continue;

*/

 

				default	 :

					continue;

			}

 

		}

 

		if(bSetProperties)

			SetPrinterProperties(szPrinterName, pDevMode2);

 

		// Expand any wildcards

		pattern = argv[i];

		hFind = FindFirstFile(pattern, &find_data);

		if (hFind != INVALID_HANDLE_VALUE)

		{

			do {

 

				// Construct full path to file

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

 

				// Get the path to the executable 

				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;

				}

 

				// Make sure it's a PDF file

				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;

				// Set flags if Acrobat/Reader already running

				//isAdobeRunning();

				if(!isAdobeRunning())

				{

					wasRunning=FALSE;

                    ShellExecute(NULL,"open",szFile,NULL,NULL,SW_HIDE);

 

					Sleep(3000);

 

					HideAdobe();

					

					if(!isAdobeRunning())

						Sleep(3000);

 

					HideAdobe();

				}

 

				// Tell Acrobat/Reader to print the file

				if(pdfprint(szPath, szPrinter, szPort, szDriver))

					nPrinted++;

 

			} while (FindNextFile(hFind, &find_data));

		}

 

		FindClose(hFind);

	}

 



	// If Acrobat/Reader wasn't running when we started, terminate it

	if(!wasRunning)

	{

		if(isAdobeRunning()) {

			Sleep(1000);

			// Ask Acrobat/Reader to close

			PostMessage (hWndAdobe, WM_CLOSE, 0, 0); 

			//PostQuitMessage(hWndAdobe, WM_QUIT);

		

			//SendExecCmd("[AppExit()]");

			//Sleep(3000);

		}

 

	}	

 

	// Reset printer defaults

	if(bSetProperties)

		SetPrinterProperties(szPrinterName, pDevMode1);

 

	// Cleanup

	if(pDevMode1)

		free(pDevMode1);

 

	if(pDevMode2)

		free(pDevMode2);

 

	printf("%d File(s) Printed\n", nPrinted);	

	return(nPrinted);

}

</filename>
| tazenman |谢尔盖Chepurin:看到这里基本样本,如何读/写Windows API的注册表:]

回答

评论会员:游客 时间:2012/02/07
只是想读之间的线在这里,所以请大家多多包涵...{BR}我注意到你提到的C和编程能力,然后,我注意到一个字,得到了我的注意-"类"。嗯,这是企图从C程序中使用一个类,我不知道?我查MSDN{A}],发现RegistryKey类似乎是的。NET此外,参考为"Registry.ClassesRoot"我怀疑代码被混合了C和。NET的C既然你要使用普通的Win32API函数,我建议你看看为Win32的注册表函数在MSDN。他们{A2}]
| RKnGl:这是示例代码:
{A3的}

刚才看这个代码,你会发现几件事情,你的代码中缺少一些方法已经过时,根据NET /编译器版本。 ..您正在使用..

using namespace System;

using namespace Microsoft::Win32;

 

public ref class RegGetDef

{

public:

    static void Main()

    {

        // Create a reference to a valid key.  In order for this code to

        // work, the indicated key must have been created previously.

        // The key name is not case-sensitive.

        RegistryKey^ rk = Registry::LocalMachine->OpenSubKey("Software\\myTestKey", false);

        // Get the value from the specified name/value pair in the key.

        String^ valueName = "myTestValue";

 

        Console::WriteLine("Retrieving registry value ...");

        Console::WriteLine();

        Object^ o = rk->GetValue(valueName);

        Console::WriteLine("Object Type = " + o->GetType()->FullName);

        Console::WriteLine();

        switch (rk->GetValueKind(valueName))

        {

            case RegistryValueKind::String:

            case RegistryValueKind::ExpandString:

                Console::WriteLine("Value = " + o);

                break;

            case RegistryValueKind::Binary:

                for each (Byte^ b in (array<byte^>^)o)

                {

                    Console::Write("{0:x2} ", b);

                }

                Console::WriteLine();

                break;

            case RegistryValueKind::DWord:

                Console::WriteLine("Value = " + Convert::ToString((Int32^)o));

                break;

            case RegistryValueKind::QWord:

                Console::WriteLine("Value = " + Convert::ToString((Int64^)o));

                break;

            case RegistryValueKind::MultiString:

                for each (String^ s in (array<string^>^)o)

                {

                    Console::Write("[{0:s}], ", s);

                }

                Console::WriteLine();

                break;

            default:

                Console::WriteLine("Value = (Unknown)");

                break;

        }

 

        // Attempt to retrieve a value that does not exist; the specified

        // default value is returned.

        String^ def = (String^)rk->GetValue("notavalue", "The default to return");

        Console::WriteLine();

        Console::WriteLine(def);

 

        rk->Close();

    }

};

 

int main()

{

    RegGetDef::Main();

}

/*

Output:

Retrieving registry value ...

 

Object Type = System.String

 

Value = testData

 

The default to return

*/
评论会员:tazenman 时间:2012/02/07
感谢所有应用和你用我的耐心!你们所有的摇滚!谢尔盖例如链接感谢,这是什么帮助我通过这个得到这个应用程序的工作!也我不知道这个C程序导入到VS2005中不会转换从WIN32API净,就是被扔我一件事。

再次感谢!!!|