有谁能解决?用程序修改了默人打印设置后,当前程序仍读不出来这个新的设置!

zyjnb 2003-09-28 10:28:49
有谁能解决?用程序修改了默人打印设置后,当前程序仍读不出来这个新的设置!
以下是我的部分代码:
BOOL CCoRP::DoPrinterSetup(HWND ParenthWnd)
{
PRINTDLG pd;


// Initialize PRINTDLG
ZeroMemory(&pd, sizeof(PRINTDLG));
pd.lStructSize = sizeof(PRINTDLG);
pd.hwndOwner = ParenthWnd;
pd.hDevMode = NULL; // Don't forget to free or store hDevMode
pd.hDevNames = NULL; // Don't forget to free or store hDevNames
pd.Flags = PD_USEDEVMODECOPIESANDCOLLATE;
pd.nCopies = 1;
pd.nFromPage = 0xFFFF;
pd.nToPage = 0xFFFF;
pd.nMinPage = 1;
pd.nMaxPage = 0xFFFF;

if (PrintDlg(&pd)==TRUE)
{
// Lock memory handle.
DEVMODE FAR* pDevMode =
(DEVMODE FAR*)::GlobalLock(pd.hDevMode);
LPDEVNAMES lpDevNames;
LPTSTR lpszDriverName, lpszDeviceName, lpszPortName;
//HANDLE hPrinter;


if (pDevMode)
{
// Unlock memory handle.
lpDevNames = (LPDEVNAMES)GlobalLock(pd.hDevNames);
lpszDriverName = (LPTSTR )lpDevNames + lpDevNames->wDriverOffset;
lpszDeviceName = (LPTSTR )lpDevNames + lpDevNames->wDeviceOffset;
lpszPortName = (LPTSTR )lpDevNames + lpDevNames->wOutputOffset;

//m_hDevMode=pd.hDevMode;

DWORD nBufferSize=255;
CString szPrinterName;
LPTSTR lpszPrinterName=szPrinterName.GetBufferSetLength(nBufferSize);
BOOL bRet = RPGetDefaultPrinter(lpszPrinterName, &nBufferSize);

RPSetPrinter(ParenthWnd,lpszDeviceName,pDevMode);

if (szPrinterName.CompareNoCase(CString(lpszDeviceName))!=0)
RPSetDefaultPrinter(lpszDeviceName);

//RPSetPrinter(ParenthWnd,lpszDeviceName,pDevMode);
//SetPrinterDeviceDefaults(pd.hDevNames,pd.hDevMode);
/*
::OpenPrinter(lpszDeviceName, &hPrinter, NULL);
::DocumentProperties(NULL,hPrinter,lpszDeviceName,pDevMode,
pDevMode, DM_IN_BUFFER|DM_OUT_BUFFER);

// Sync the pDevMode.
// See SDK help for DocumentProperties for more info.

::ClosePrinter(hPrinter);*/
::GlobalUnlock(m_hDevNames);
::GlobalUnlock(m_hDevMode);
}

// GDI calls to render output.
return (TRUE);
}
return (FALSE);
}


// RPSetPrinter
//
// Demonstrates how to use the SetPrinter API. This particular function changes the orienation
// for the printer specified in pPrinterName to the orientation specified in dmOrientation.
//
// Valid values for dmOrientation are:
// DMORIENT_PORTRAIT (1)
// or
// DMORIENT_LANDSCAPE (2)
BOOL CCoRP::RPSetPrinter(HWND ParenthWnd, LPTSTR pPrinterName, DEVMODE *pDevModeSet)
{
HANDLE hPrinter = NULL;
DWORD dwNeeded = 0;
PRINTER_INFO_2 *pi2 = NULL;
DEVMODE *pDevMode = NULL;
PRINTER_DEFAULTS pd;
BOOL bFlag;
LONG lFlag;

// Open printer handle (on Windows NT, you need full-access because you
// will eventually use SetPrinter)...
ZeroMemory(&pd, sizeof(pd));
pd.DesiredAccess = PRINTER_ALL_ACCESS;
bFlag = OpenPrinter(pPrinterName, &hPrinter, &pd);
if (!bFlag || (hPrinter == NULL))
return FALSE;

// The first GetPrinter tells you how big the buffer should be in
// order to hold all of PRINTER_INFO_2. Note that this should fail with
// ERROR_INSUFFICIENT_BUFFER. If GetPrinter fails for any other reason
// or dwNeeded isn't set for some reason, then there is a problem...
SetLastError(0);
bFlag = GetPrinter(hPrinter, 2, 0, 0, &dwNeeded);
if ((!bFlag) && (GetLastError() != ERROR_INSUFFICIENT_BUFFER) ||
(dwNeeded == 0))
{
ClosePrinter(hPrinter);
return FALSE;
}

// Allocate enough space for PRINTER_INFO_2...
pi2 = (PRINTER_INFO_2 *)GlobalAlloc(GPTR, dwNeeded);
if (pi2 == NULL)
{
ClosePrinter(hPrinter);
return FALSE;
}

// The second GetPrinter fills in all the current settings, so all you
// need to do is modify what you're interested in...
bFlag = GetPrinter(hPrinter, 2, (LPBYTE)pi2, dwNeeded, &dwNeeded);
if (!bFlag)
{
GlobalFree(pi2);
ClosePrinter(hPrinter);
return FALSE;
}

// If GetPrinter didn't fill in the DEVMODE, try to get it by calling
// DocumentProperties...
if (pi2->pDevMode == NULL)
{
dwNeeded = DocumentProperties(ParenthWnd, hPrinter,
pPrinterName,
NULL, NULL, 0);
if (dwNeeded <= 0)
{
GlobalFree(pi2);
ClosePrinter(hPrinter);
return FALSE;
}

pDevMode = (DEVMODE *)GlobalAlloc(GPTR, dwNeeded);
if (pDevMode == NULL)
{
GlobalFree(pi2);
ClosePrinter(hPrinter);
return FALSE;
}

lFlag = DocumentProperties(ParenthWnd, hPrinter,
pPrinterName,
pDevMode, NULL,
DM_OUT_BUFFER);
if (lFlag != IDOK || pDevMode == NULL)
{
GlobalFree(pDevMode);
GlobalFree(pi2);
ClosePrinter(hPrinter);
return FALSE;
}

pi2->pDevMode = pDevMode;
}

// Driver is reporting that it doesn't support this change...
if (!(pi2->pDevMode->dmFields & DM_ORIENTATION))
{
GlobalFree(pi2);
ClosePrinter(hPrinter);
if (pDevMode)
GlobalFree(pDevMode);
return FALSE;
}

// Specify exactly what we are attempting to change...
//pi2->pDevMode->dmFields = DM_ORIENTATION;
//pi2->pDevMode->dmOrientation = dmOrientation;

//pi2->pDevMode = pDevModeSet;
memcpy(pi2->pDevMode,pDevModeSet,
(size_t)::GlobalSize(pDevModeSet));

// Do not attempt to set security descriptor...
pi2->pSecurityDescriptor = NULL;

// Make sure the driver-dependent part of devmode is updated...
lFlag = DocumentProperties(ParenthWnd, hPrinter,
pPrinterName,
pi2->pDevMode, pi2->pDevMode,
DM_IN_BUFFER | DM_OUT_BUFFER);
if (lFlag != IDOK)
{
GlobalFree(pi2);
ClosePrinter(hPrinter);
if (pDevMode)
GlobalFree(pDevMode);
return FALSE;
}

// Update printer information...
bFlag = SetPrinter(hPrinter, 2, (LPBYTE)pi2, 0);
if (!bFlag)
// The driver doesn't support, or it is unable to make the change...
{
GlobalFree(pi2);
ClosePrinter(hPrinter);
if (pDevMode)
GlobalFree(pDevMode);
return FALSE;
}

// Tell other apps that there was a change...
SendMessageTimeout(HWND_BROADCAST, WM_DEVMODECHANGE, 0L,
(LPARAM)(LPCSTR)pPrinterName,
SMTO_NORMAL, 1000, NULL);

// Clean up...

if (hPrinter)
ClosePrinter(hPrinter);
if (pDevMode)
GlobalFree(pDevMode);
if (pi2)
GlobalFree(pi2);

return TRUE;
}
...全文
51 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
zyjnb 2003-09-28
  • 打赏
  • 举报
回复
最后一部分:

// CoRP.cpp: implementation of the CCoRP class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "CoRP.h"

const size_t BUFSIZE = 255;
static TCHAR g_szBuf[BUFSIZE];

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CCoRP::CCoRP()
{

}

CCoRP::~CCoRP()
{

}

// Size of internal buffer used to hold "printername,drivername,portname"
// string. Increasing this may be necessary for huge strings.
#define MAXBUFFERSIZE 250

/*----------------------------------------------------------------*/
/* RPGetDefaultPrinter */
/* */
/* Parameters: */
/* pPrinterName: Buffer alloc'd by caller to hold printer name. */
/* pdwBufferSize: On input, ptr to size of pPrinterName. */
/* On output, min required size of pPrinterName. */
/* */
/* NOTE: You must include enough space for the NULL terminator! */
/* */
/* Returns: TRUE for success, FALSE for failure. */
/*----------------------------------------------------------------*/
BOOL CCoRP::RPGetDefaultPrinter(LPTSTR pPrinterName, LPDWORD pdwBufferSize)
{
BOOL bFlag;
OSVERSIONINFO osv;
TCHAR cBuffer[MAXBUFFERSIZE];
PRINTER_INFO_2 *ppi2 = NULL;
DWORD dwNeeded = 0;
DWORD dwReturned = 0;

// What version of Windows are you running?
osv.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
GetVersionEx(&osv);

// If Windows 95 or 98, use EnumPrinters...
if (osv.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS)
{
// The first EnumPrinters() tells you how big our buffer should
// be in order to hold ALL of PRINTER_INFO_2. Note that this will
// usually return FALSE. This only means that the buffer (the 4th
// parameter) was not filled in. You don't want it filled in here...
EnumPrinters(PRINTER_ENUM_DEFAULT, NULL, 2, NULL, 0, &dwNeeded, &dwReturned);
if (dwNeeded == 0)
return FALSE;

// Allocate enough space for PRINTER_INFO_2...
ppi2 = (PRINTER_INFO_2 *)GlobalAlloc(GPTR, dwNeeded);
if (!ppi2)
return FALSE;

// The second EnumPrinters() will fill in all the current information...
bFlag = EnumPrinters(PRINTER_ENUM_DEFAULT, NULL, 2, (LPBYTE)ppi2, dwNeeded, &dwNeeded, &dwReturned);
if (!bFlag)
{
GlobalFree(ppi2);
return FALSE;
}

// If given buffer too small, set required size and fail...
if ((DWORD)lstrlen(ppi2->pPrinterName) >= *pdwBufferSize)
{
*pdwBufferSize = (DWORD)lstrlen(ppi2->pPrinterName) + 1;
GlobalFree(ppi2);
return FALSE;
}

// Copy printer name into passed-in buffer...
lstrcpy(pPrinterName, ppi2->pPrinterName);

// Set buffer size parameter to min required buffer size...
*pdwBufferSize = (DWORD)lstrlen(ppi2->pPrinterName) + 1;
}

// If Windows NT, use the GetDefaultPrinter API for Windows 2000,
// or GetProfileString for version 4.0 and earlier...
else if (osv.dwPlatformId == VER_PLATFORM_WIN32_NT)
{
#if(WINVER >= 0x0500)
if (osv.dwMajorVersion >= 5) // Windows 2000 or later
{
bFlag = GetDefaultPrinter(pPrinterName, pdwBufferSize);
if (!bFlag)
return FALSE;
}

else // NT4.0 or earlier
#endif
{
// Retrieve the default string from Win.ini (the registry).
// String will be in form "printername,drivername,portname".
if (GetProfileString("windows", "device", ",,,", cBuffer, MAXBUFFERSIZE) <= 0)
return FALSE;

// Printer name precedes first "," character...
strtok(cBuffer, ",");

// If given buffer too small, set required size and fail...
if ((DWORD)lstrlen(cBuffer) >= *pdwBufferSize)
{
*pdwBufferSize = (DWORD)lstrlen(cBuffer) + 1;
return FALSE;
}

// Copy printer name into passed-in buffer...
lstrcpy(pPrinterName, cBuffer);

// Set buffer size parameter to min required buffer size...
*pdwBufferSize = (DWORD)lstrlen(cBuffer) + 1;
}
}

// Cleanup...
if (ppi2)
GlobalFree(ppi2);

return TRUE;
}

#undef MAXBUFFERSIZE






zyjnb 2003-09-28
  • 打赏
  • 举报
回复
这是另一部分代码

/*-----------------------------------------------------------------*/
/* RPSetDefaultPrinter */
/* */
/* Parameters: */
/* pPrinterName: Valid name of existing printer to make default. */
/* */
/* Returns: TRUE for success, FALSE for failure. */
/*-----------------------------------------------------------------*/
BOOL CCoRP::RPSetDefaultPrinter(LPTSTR pPrinterName)
{
BOOL bFlag;
OSVERSIONINFO osv;
DWORD dwNeeded = 0;
HANDLE hPrinter = NULL;
PRINTER_INFO_2 *ppi2 = NULL;
LPTSTR pBuffer = NULL;
LONG lResult;

// What version of Windows are you running?
osv.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
GetVersionEx(&osv);

if (!pPrinterName)
return FALSE;

// If Windows 95 or 98, use SetPrinter...
if (osv.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS)
{
// Open this printer so you can get information about it...
bFlag = OpenPrinter(pPrinterName, &hPrinter, NULL);
if (!bFlag || !hPrinter)
return FALSE;

// The first GetPrinter() tells you how big our buffer should
// be in order to hold ALL of PRINTER_INFO_2. Note that this will
// usually return FALSE. This only means that the buffer (the 3rd
// parameter) was not filled in. You don't want it filled in here...
GetPrinter(hPrinter, 2, 0, 0, &dwNeeded);
if (dwNeeded == 0)
{
ClosePrinter(hPrinter);
return FALSE;
}

// Allocate enough space for PRINTER_INFO_2...
ppi2 = (PRINTER_INFO_2 *)GlobalAlloc(GPTR, dwNeeded);
if (!ppi2)
{
ClosePrinter(hPrinter);
return FALSE;
}

// 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)
{
ClosePrinter(hPrinter);
GlobalFree(ppi2);
return FALSE;
}

// Set default printer attribute for this printer...
ppi2->Attributes |= PRINTER_ATTRIBUTE_DEFAULT;
bFlag = SetPrinter(hPrinter, 2, (LPBYTE)ppi2, 0);
if (!bFlag)
{
ClosePrinter(hPrinter);
GlobalFree(ppi2);
return FALSE;
}

// Tell all open applications that this change occurred.
// Allow each application 1 second to handle this message.
lResult = SendMessageTimeout(HWND_BROADCAST, WM_SETTINGCHANGE, 0L,
(LPARAM)(LPCTSTR)"windows", SMTO_NORMAL, 1000, NULL);
}

// If Windows NT, use the SetDefaultPrinter API for Windows 2000,
// or WriteProfileString for version 4.0 and earlier...
else if (osv.dwPlatformId == VER_PLATFORM_WIN32_NT)
{
#if(WINVER >= 0x0500)
if (osv.dwMajorVersion >= 5) // Windows 2000 or later...
{
bFlag = SetDefaultPrinter(pPrinterName);
if (!bFlag)
return FALSE;
}

else // NT4.0 or earlier...
#endif
{
// Open this printer so you can get information about it...
bFlag = OpenPrinter(pPrinterName, &hPrinter, NULL);
if (!bFlag || !hPrinter)
return FALSE;

// The first GetPrinter() tells you how big our buffer should
// be in order to hold ALL of PRINTER_INFO_2. Note that this will
// usually return FALSE. This only means that the buffer (the 3rd
// parameter) was not filled in. You don't want it filled in here...
GetPrinter(hPrinter, 2, 0, 0, &dwNeeded);
if (dwNeeded == 0)
{
ClosePrinter(hPrinter);
return FALSE;
}

// Allocate enough space for PRINTER_INFO_2...
ppi2 = (PRINTER_INFO_2 *)GlobalAlloc(GPTR, dwNeeded);
if (!ppi2)
{
ClosePrinter(hPrinter);
return FALSE;
}

// The second GetPrinter() fills in all the current<BR/>
// information...
bFlag = GetPrinter(hPrinter, 2, (LPBYTE)ppi2, dwNeeded, &dwNeeded);
if ((!bFlag) || (!ppi2->pDriverName) || (!ppi2->pPortName))
{
ClosePrinter(hPrinter);
GlobalFree(ppi2);
return FALSE;
}

// Allocate buffer big enough for concatenated string.
// String will be in form "printername,drivername,portname"...
pBuffer = (LPTSTR)GlobalAlloc(GPTR,
lstrlen(pPrinterName) +
lstrlen(ppi2->pDriverName) +
lstrlen(ppi2->pPortName) + 3);
if (!pBuffer)
{
ClosePrinter(hPrinter);
GlobalFree(ppi2);
return FALSE;
}

// Build string in form "printername,drivername,portname"...
lstrcpy(pBuffer, pPrinterName); lstrcat(pBuffer, ",");
lstrcat(pBuffer, ppi2->pDriverName); lstrcat(pBuffer, ",");
lstrcat(pBuffer, ppi2->pPortName);

// Set the default printer in Win.ini and registry...
bFlag = WriteProfileString("windows", "device", pBuffer);
if (!bFlag)
{
ClosePrinter(hPrinter);
GlobalFree(ppi2);
GlobalFree(pBuffer);
return FALSE;
}
}

// Tell all open applications that this change occurred.
// Allow each app 1 second to handle this message.
lResult = SendMessageTimeout(HWND_BROADCAST, WM_SETTINGCHANGE, 0L, 0L,
SMTO_NORMAL, 1000, NULL);
}

// Cleanup...
if (hPrinter)
ClosePrinter(hPrinter);
if (ppi2)
GlobalFree(ppi2);
if (pBuffer)
GlobalFree(pBuffer);

return TRUE;
}
ac1998 2003-09-28
  • 打赏
  • 举报
回复



up.

up.

16,472

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧