Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. COM
  4. DVTARGETDEVICE structure

DVTARGETDEVICE structure

Scheduled Pinned Locked Moved COM
questionc++comalgorithmshelp
2 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    CodeBrain
    wrote on last edited by
    #1

    I am searching for a way to create a DVTARGETDEVICE structure with the information from the currently selected printer in a MFC application. Is there a way to do this automatically (e.g. if I have a CDC object)? If this is not possible, how do I create the DVTARGETDEVICE structure if I have the DEVNAMES and DEVMODE structures. Unfortunatly the MSDN docu is not very good here and I have not found any information about that. By the way, why do I have to use a DVTARGETDEVICE, an Information context AND a Device context in so many COM methods (like IViewObject::Draw)? The 2 HDCs I have should be enough!? Thanks in advance for your help.

    C 1 Reply Last reply
    0
    • C CodeBrain

      I am searching for a way to create a DVTARGETDEVICE structure with the information from the currently selected printer in a MFC application. Is there a way to do this automatically (e.g. if I have a CDC object)? If this is not possible, how do I create the DVTARGETDEVICE structure if I have the DEVNAMES and DEVMODE structures. Unfortunatly the MSDN docu is not very good here and I have not found any information about that. By the way, why do I have to use a DVTARGETDEVICE, an Information context AND a Device context in so many COM methods (like IViewObject::Draw)? The 2 HDCs I have should be enough!? Thanks in advance for your help.

      C Offline
      C Offline
      CodeBrain
      wrote on last edited by
      #2

      OMG, I like those MS guys. After wasting 1 day, I finally found out how MS internally do it! :mad: There is an internal function in OLEMISC.CPP called _AfxOleCreateTargetDevice. It takes the DEVAMES and DEVMODE structures and returns a the DVTARGETDEVICE structure: Here is the code of this function: DVTARGETDEVICE* AFXAPI _AfxOleCreateTargetDevice(LPDEVNAMES pDN, LPDEVMODE pDM) { USES_CONVERSION; DVTARGETDEVICE* ptd = NULL; DWORD dwDevNamesSize, dwDevModeSize, dwPtdSize; LPCTSTR lpszDriverName = DEVNAMEPART(pDN, wDriverOffset); LPCTSTR lpszDeviceName = DEVNAMEPART(pDN, wDeviceOffset); LPCTSTR lpszPortName = DEVNAMEPART(pDN, wOutputOffset); LPCOLESTR lpszDriverNameOle = T2COLE(lpszDriverName); LPCOLESTR lpszDeviceNameOle = T2COLE(lpszDeviceName); LPCOLESTR lpszPortNameOle = T2COLE(lpszPortName); int nDriverNameSize = (lpszDriverNameOle == NULL) ? 0 : (ocslen(lpszDriverNameOle)+1)*sizeof(OLECHAR); int nDeviceNameSize = (lpszDeviceNameOle == NULL) ? 0 : (ocslen(lpszDeviceNameOle)+1)*sizeof(OLECHAR); int nPortNameSize = (lpszPortNameOle == NULL) ? 0 : (ocslen(lpszPortNameOle)+1)*sizeof(OLECHAR); LPDEVMODEOLE lpDevModeOle = DEVMODET2OLE(pDM); dwDevNamesSize = nDriverNameSize + nDeviceNameSize + nPortNameSize; dwDevModeSize = (DWORD)(lpDevModeOle->dmSize + lpDevModeOle->dmDriverExtra); dwPtdSize = sizeof(DVTARGETDEVICE) + dwDevNamesSize + dwDevModeSize; if ((ptd = (DVTARGETDEVICE*)CoTaskMemAlloc(dwPtdSize)) != NULL) { // copy in the info ptd->tdSize = (UINT)dwPtdSize; ptd->tdDriverNameOffset = sizeof(DVTARGETDEVICE); ocscpy((LPOLESTR)((BYTE*)ptd + ptd->tdDriverNameOffset), lpszDriverNameOle); ptd->tdDeviceNameOffset = (WORD)(ptd->tdDriverNameOffset + nDriverNameSize); ocscpy((LPOLESTR)((BYTE*)ptd + ptd->tdDeviceNameOffset), lpszDeviceNameOle); ptd->tdPortNameOffset = (WORD)(ptd->tdDeviceNameOffset + nDeviceNameSize); ocscpy((LPOLESTR)((BYTE*)ptd + ptd->tdPortNameOffset), lpszPortNameOle); ptd->tdExtDevmodeOffset = (WORD)(ptd->tdPortNameOffset + nPortNameSize); memcpy((BYTE*)ptd + ptd->tdExtDevmodeOffset, lpDevModeOle, sizeof(DEVMODEOLE)+lpDevModeOle->dmDriverExtra); } return ptd; } And here is a sample from me and the MS file OLEDOCCL.cpp how to use, incl. getting the current application printer: PRINTDLG pd; pd.lStructSize=(DWORD)sizeof(PRINTDLG); AfxGetApp()->GetPrinterDeviceDefaults(&pd); DVTARGETDEVICE* pTargetDevice = NULL; LPDEVNAMES lpDevNames = NULL; LPDEVMODE lpDevMode = NU

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups