主题:有关PageSetupDlg的内存释放问题
关于PageSetupDlg的使用MSDN上有下列代码:
PAGESETUPDLG psd; // common dialog box structure
HWND hwnd; // owner window
// Initialize PAGESETUPDLG
ZeroMemory(&psd, sizeof(psd));
psd.lStructSize = sizeof(psd);
psd.hwndOwner = hwnd;
psd.hDevMode = NULL; // Don't forget to free or store hDevMode
psd.hDevNames = NULL; // Don't forget to free or store hDevNames
psd.Flags = PSD_INTHOUSANDTHSOFINCHES | PSD_MARGINS |
PSD_ENABLEPAGEPAINTHOOK;
psd.rtMargin.top = 1000;
psd.rtMargin.left = 1250;
psd.rtMargin.right = 1250;
psd.rtMargin.bottom = 1000;
psd.lpfnPagePaintHook = PaintHook;
if (PageSetupDlg(&psd)==TRUE) {
// check paper size and margin values here
}
上面hDevMode和hDevNames右注释:
psd.hDevMode = NULL; // Don't forget to free or store hDevMode
psd.hDevNames = NULL; // Don't forget to free or store hDevNames
是不是要这样:
if (PageSetupDlg(&psd)==TRUE)
{
if(psd.hDevMode) GlobalFree(psd.hDevMode);
if(psd.hDevNames) GlobalFree(psd.hDevNames);
}
还有如果初始化hDevMode和hDevNames时,分配了内存(GlobalAlloc)要不要释放?
像这种句柄的使用,操作剪贴板时GlobalAlloc分配了内存后,就不能释放的。
相似的MSDN中使用PRINTDLGEX时有下了代码:
HRESULT DisplayPrintPropertySheet(
HWND hWnd // Window that owns the property sheet
)
{
HRESULT hResult;
LPPRINTDLGEX pPDX = NULL;
LPPRINTPAGERANGE pPageRanges = NULL;
// Allocate the PRINTDLGEX structure.
pPDX = (LPPRINTDLGEX)GlobalAlloc(GPTR, sizeof(PRINTDLGEX));
if (!pPDX)
return E_OUTOFMEMORY;
// Allocate an array of PRINTPAGERANGE structures.
pPageRanges = (LPPRINTPAGERANGE) GlobalAlloc(GPTR,
10 * sizeof(PRINTPAGERANGE));
if (!pPageRanges)
return E_OUTOFMEMORY;
// Initialize the PRINTDLGEX structure.
pPDX->lStructSize = sizeof(PRINTDLGEX);
pPDX->hwndOwner = hWnd;
pPDX->hDevMode = NULL;
pPDX->hDevNames = NULL;
pPDX->hDC = NULL;
pPDX->Flags = PD_RETURNDC | PD_COLLATE;
pPDX->Flags2 = 0;
pPDX->ExclusionFlags = 0;
pPDX->nPageRanges = 0;
pPDX->nMaxPageRanges = 10;
pPDX->lpPageRanges = pPageRanges;
pPDX->nMinPage = 1;
pPDX->nMaxPage = 1000;
pPDX->nCopies = 1;
pPDX->hInstance = 0;
pPDX->lpPrintTemplateName = NULL;
pPDX->lpCallback = NULL;
pPDX->nPropertyPages = 0;
pPDX->lphPropertyPages = NULL;
pPDX->nStartPage = START_PAGE_GENERAL;
pPDX->dwResultAction = 0;
// Invoke the Print property sheet.
hResult = PrintDlgEx(pPDX);
if ( (hResult == S_OK) &&
pPDX->dwResultAction == PD_RESULT_PRINT) {
// User clicked the Print button, so
// use the DC and other information returned in the
// PRINTDLGEX structure to print the document
}
if (pPDX->hDC != NULL)
DeleteDC(pPDX->hDC);
if (pPDX->hDevMode != NULL)
GlobalFree(pPDX->hDevMode);
if (pPDX->hDevNames != NULL)
GlobalFree(pPDX->hDevNames);
return hResult;
}
这里面hDevMode 和hDevNames释放了,但是pPDX 和pPageRanges确没有释放
是不是有问题,还是应该这样?
最后要问的就是:
hDevMode和hDevNames到底应该怎样使用?
大家帮帮忙吧,谢谢~
PAGESETUPDLG psd; // common dialog box structure
HWND hwnd; // owner window
// Initialize PAGESETUPDLG
ZeroMemory(&psd, sizeof(psd));
psd.lStructSize = sizeof(psd);
psd.hwndOwner = hwnd;
psd.hDevMode = NULL; // Don't forget to free or store hDevMode
psd.hDevNames = NULL; // Don't forget to free or store hDevNames
psd.Flags = PSD_INTHOUSANDTHSOFINCHES | PSD_MARGINS |
PSD_ENABLEPAGEPAINTHOOK;
psd.rtMargin.top = 1000;
psd.rtMargin.left = 1250;
psd.rtMargin.right = 1250;
psd.rtMargin.bottom = 1000;
psd.lpfnPagePaintHook = PaintHook;
if (PageSetupDlg(&psd)==TRUE) {
// check paper size and margin values here
}
上面hDevMode和hDevNames右注释:
psd.hDevMode = NULL; // Don't forget to free or store hDevMode
psd.hDevNames = NULL; // Don't forget to free or store hDevNames
是不是要这样:
if (PageSetupDlg(&psd)==TRUE)
{
if(psd.hDevMode) GlobalFree(psd.hDevMode);
if(psd.hDevNames) GlobalFree(psd.hDevNames);
}
还有如果初始化hDevMode和hDevNames时,分配了内存(GlobalAlloc)要不要释放?
像这种句柄的使用,操作剪贴板时GlobalAlloc分配了内存后,就不能释放的。
相似的MSDN中使用PRINTDLGEX时有下了代码:
HRESULT DisplayPrintPropertySheet(
HWND hWnd // Window that owns the property sheet
)
{
HRESULT hResult;
LPPRINTDLGEX pPDX = NULL;
LPPRINTPAGERANGE pPageRanges = NULL;
// Allocate the PRINTDLGEX structure.
pPDX = (LPPRINTDLGEX)GlobalAlloc(GPTR, sizeof(PRINTDLGEX));
if (!pPDX)
return E_OUTOFMEMORY;
// Allocate an array of PRINTPAGERANGE structures.
pPageRanges = (LPPRINTPAGERANGE) GlobalAlloc(GPTR,
10 * sizeof(PRINTPAGERANGE));
if (!pPageRanges)
return E_OUTOFMEMORY;
// Initialize the PRINTDLGEX structure.
pPDX->lStructSize = sizeof(PRINTDLGEX);
pPDX->hwndOwner = hWnd;
pPDX->hDevMode = NULL;
pPDX->hDevNames = NULL;
pPDX->hDC = NULL;
pPDX->Flags = PD_RETURNDC | PD_COLLATE;
pPDX->Flags2 = 0;
pPDX->ExclusionFlags = 0;
pPDX->nPageRanges = 0;
pPDX->nMaxPageRanges = 10;
pPDX->lpPageRanges = pPageRanges;
pPDX->nMinPage = 1;
pPDX->nMaxPage = 1000;
pPDX->nCopies = 1;
pPDX->hInstance = 0;
pPDX->lpPrintTemplateName = NULL;
pPDX->lpCallback = NULL;
pPDX->nPropertyPages = 0;
pPDX->lphPropertyPages = NULL;
pPDX->nStartPage = START_PAGE_GENERAL;
pPDX->dwResultAction = 0;
// Invoke the Print property sheet.
hResult = PrintDlgEx(pPDX);
if ( (hResult == S_OK) &&
pPDX->dwResultAction == PD_RESULT_PRINT) {
// User clicked the Print button, so
// use the DC and other information returned in the
// PRINTDLGEX structure to print the document
}
if (pPDX->hDC != NULL)
DeleteDC(pPDX->hDC);
if (pPDX->hDevMode != NULL)
GlobalFree(pPDX->hDevMode);
if (pPDX->hDevNames != NULL)
GlobalFree(pPDX->hDevNames);
return hResult;
}
这里面hDevMode 和hDevNames释放了,但是pPDX 和pPageRanges确没有释放
是不是有问题,还是应该这样?
最后要问的就是:
hDevMode和hDevNames到底应该怎样使用?
大家帮帮忙吧,谢谢~