Problem with PageSetupDlg function !

Hi,

I have a problem to get the structure DEVMODE pointed by the hDevMode returned from the PageSetupFunction.
When I copy the hDevMode handle to a DEVMODE structure, the structure contain not valid datas.

Here's the code :

HGLOBAL m_hDevMode  = NULL;
HGLOBAL m_hDevNames = NULL;

BOOL CMMALogListBox::Print(HWND hParent)
 {
  if ((!m_hDevMode || !m_hDevNames) && !PrinterSetup(hParent,TRUE)) return FALSE;

  return TRUE;
 }

BOOL PrinterSetup(HWND hParent, BOOL bDefault)
 {
  PAGESETUPDLG stPSD;

  memset(&stPSD, 0, sizeof(stPSD));

  stPSD.lStructSize = sizeof(stPSD);
  stPSD.hwndOwner   = hParent;
  stPSD.hInstance   = NULL;
  stPSD.Flags       = bDefault ? PSD_RETURNDEFAULT : 0;
  stPSD.hDevMode    = m_hDevMode;
  stPSD.hDevNames   = m_hDevNames;

  if (PageSetupDlg(&stPSD) && CommDlgExtendedError()) return FALSE;

  if (m_hDevMode ) GlobalFree(m_hDevMode);
  if (m_hDevNames) GlobalFree(m_hDevNames);

  m_hDevMode  = stPSD.hDevMode;
  m_hDevNames = stPSD.hDevNames;

  DEVMODE  stDevMode;
  DEVNAMES stDevNames;

  memset(&stDevMode , 0, sizeof(stDevMode));
  memset(&stDevNames, 0, sizeof(stDevNames));

  if (m_hDevMode ) memcpy(&stDevMode , m_hDevMode , sizeof(DEVMODE ));
  if (m_hDevNames) memcpy(&stDevNames, m_hDevNames, sizeof(DEVNAMES));

  // The structures contain no valid datas. Where's the problem ?
  // for example, I want to get the device name from the
  // structure DEVNAMES
  //
 
  char szDeviceName[32];

  strcpy(szDeviceName, (char *)((long)m_hDevNames + stDevNames.wDeviceOffset));

  return TRUE;
 }
mike_marquetAsked:
Who is Participating?
 
chensuConnect With a Mentor Commented:
Thank you.

By the way, EE has implemented a new feature that allows you to accept a comment as an answer. Have you seen those hyperlinks?
0
 
jkrCommented:
Shouldn't


  char szDeviceName[32];

  strcpy(szDeviceName, (char *)((long)m_hDevNames + stDevNames.wDeviceOffset));


read


  char szDeviceName[CCHDEVICENAME + 1];

  strcpy(szDeviceName, ((DEVMODE*)m_hDevMode)->dmDeviceName);

?
0
 
mike_marquetAuthor Commented:
OK, but the values in the structure are not valid !

For example, the dmOrientation is egal to 215 when permited values are 1 or 2.
The dmDeviceName is egal to "He@"

Can I do this :

DEVMODE stDevMode;

memcpy(&stDevMode, m_hDevMode, sizeof(DEVMODE));
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
chensuCommented:
1. m_hDevMode and m_hDevNames are global memory handles. You should use GlobalLock before accessing the memory and use GlobalUnlock after accessing.

2. CommDlgExtendedError should be called only if PageSetupDlg returns FALSE.
0
 
chensuCommented:
1. Use the return value of GlobalLock in memcpy.
0
 
mike_marquetAuthor Commented:
Thanks to chensu.
0
 
chensuCommented:
mike_marquet,

Do you mean you have solved the problem? If so, you may accept my comment as an answer so that the question goes to the PAQ area. Thank you.
0
 
chensuCommented:
mike_marquet,

Are you there?
0
 
mike_marquetAuthor Commented:
Yes, I accept the answer but you must answer the question so that I can give you the points.
0
 
mike_marquetAuthor Commented:
No I haven't seen it.
0
 
chensuCommented:
They are on the right of the "Comment" titles and right above the date and time.
0
All Courses

From novice to tech pro — start learning today.