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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.