Click here to Skip to main content
15,922,015 members
Home / Discussions / COM
   

COM

 
QuestionHow to release memory? when calling COM with param type BSTR* from VB exe application Pin
doisy18-Sep-03 2:22
doisy18-Sep-03 2:22 
AnswerRe: How to release memory? when calling COM with param type BSTR* from VB exe application Pin
Steve S18-Sep-03 2:36
Steve S18-Sep-03 2:36 
GeneralRe: How to release memory? when calling COM with param type BSTR* from VB exe application Pin
Vi218-Sep-03 19:54
Vi218-Sep-03 19:54 
GeneralRe: How to release memory? when calling COM with param type BSTR* from VB exe application Pin
Steve S18-Sep-03 21:43
Steve S18-Sep-03 21:43 
GeneralRe: How to release memory? when calling COM with param type BSTR* from VB exe application Pin
Lim Bio Liong19-Sep-03 1:19
Lim Bio Liong19-Sep-03 1:19 
GeneralOffice::CommandBarControl problem Pin
dorutzu17-Sep-03 4:30
dorutzu17-Sep-03 4:30 
GeneralDVTARGETDEVICE structure Pin
CodeBrain17-Sep-03 3:02
CodeBrain17-Sep-03 3:02 
GeneralRe: DVTARGETDEVICE structure Pin
CodeBrain17-Sep-03 4:14
CodeBrain17-Sep-03 4:14 
OMG, I like those MS guys. After wasting 1 day, I finally found out how MS internally do it! Mad | :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:
<br />
DVTARGETDEVICE* AFXAPI _AfxOleCreateTargetDevice(LPDEVNAMES pDN, LPDEVMODE pDM)<br />
{<br />
	USES_CONVERSION;<br />
<br />
	DVTARGETDEVICE* ptd = NULL;<br />
	DWORD dwDevNamesSize, dwDevModeSize, dwPtdSize;<br />
<br />
	LPCTSTR lpszDriverName = DEVNAMEPART(pDN, wDriverOffset);<br />
	LPCTSTR lpszDeviceName = DEVNAMEPART(pDN, wDeviceOffset);<br />
	LPCTSTR lpszPortName = DEVNAMEPART(pDN, wOutputOffset);<br />
<br />
	LPCOLESTR lpszDriverNameOle = T2COLE(lpszDriverName);<br />
	LPCOLESTR lpszDeviceNameOle = T2COLE(lpszDeviceName);<br />
	LPCOLESTR lpszPortNameOle = T2COLE(lpszPortName);<br />
	int nDriverNameSize = (lpszDriverNameOle == NULL) ? 0 : (ocslen(lpszDriverNameOle)+1)*sizeof(OLECHAR);<br />
	int nDeviceNameSize = (lpszDeviceNameOle == NULL) ? 0 : (ocslen(lpszDeviceNameOle)+1)*sizeof(OLECHAR);<br />
	int nPortNameSize = (lpszPortNameOle == NULL) ? 0 : (ocslen(lpszPortNameOle)+1)*sizeof(OLECHAR);<br />
<br />
	LPDEVMODEOLE lpDevModeOle = DEVMODET2OLE(pDM);<br />
<br />
	dwDevNamesSize = nDriverNameSize + nDeviceNameSize + nPortNameSize;<br />
	dwDevModeSize = (DWORD)(lpDevModeOle->dmSize + lpDevModeOle->dmDriverExtra);<br />
<br />
	dwPtdSize = sizeof(DVTARGETDEVICE) + dwDevNamesSize + dwDevModeSize;<br />
<br />
	if ((ptd = (DVTARGETDEVICE*)CoTaskMemAlloc(dwPtdSize)) != NULL)<br />
	{<br />
		// copy in the info<br />
		ptd->tdSize = (UINT)dwPtdSize;<br />
<br />
		ptd->tdDriverNameOffset = sizeof(DVTARGETDEVICE);<br />
		ocscpy((LPOLESTR)((BYTE*)ptd + ptd->tdDriverNameOffset), lpszDriverNameOle);<br />
		ptd->tdDeviceNameOffset = (WORD)(ptd->tdDriverNameOffset + nDriverNameSize);<br />
		ocscpy((LPOLESTR)((BYTE*)ptd + ptd->tdDeviceNameOffset), lpszDeviceNameOle);<br />
		ptd->tdPortNameOffset = (WORD)(ptd->tdDeviceNameOffset + nDeviceNameSize);<br />
		ocscpy((LPOLESTR)((BYTE*)ptd + ptd->tdPortNameOffset), lpszPortNameOle);<br />
		ptd->tdExtDevmodeOffset = (WORD)(ptd->tdPortNameOffset + nPortNameSize);<br />
		memcpy((BYTE*)ptd + ptd->tdExtDevmodeOffset, lpDevModeOle,<br />
			sizeof(DEVMODEOLE)+lpDevModeOle->dmDriverExtra);<br />
	}<br />
	return ptd;<br />
}<br />


And here is a sample from me and the MS file OLEDOCCL.cpp how to use, incl. getting the current application printer:

<br />
	PRINTDLG pd;<br />
	pd.lStructSize=(DWORD)sizeof(PRINTDLG);<br />
	AfxGetApp()->GetPrinterDeviceDefaults(&pd);<br />
<br />
	DVTARGETDEVICE* pTargetDevice = NULL;<br />
	LPDEVNAMES lpDevNames = NULL;<br />
	LPDEVMODE lpDevMode = NULL;<br />
<br />
	lpDevNames = (LPDEVNAMES) GlobalLock(pd.hDevNames);<br />
	if (lpDevNames != NULL)<br />
	{<br />
		lpDevMode = (LPDEVMODE) GlobalLock(pd.hDevMode);<br />
		if (lpDevMode != NULL)<br />
		{<br />
			pTargetDevice = _AfxOleCreateTargetDevice(lpDevNames, lpDevMode);<br />
			if (pTargetDevice != NULL)<br />
			{<br />
			   // DO WHAT YOU WANT WITH pTargetDevice !!!<br />
                           CoTaskMemFree(pTargetDevice);<br />
			}	<br />
			GlobalUnlock(pd.hDevMode);<br />
		}<br />
		GlobalUnlock(pd.hDevNames);<br />
	}<br />


Thx MS for the "good" documentation! Dead | X|
QuestionAny ideas on gettin a IDispatch interface from Remote Computer? Pin
FearlessBurner15-Sep-03 9:47
FearlessBurner15-Sep-03 9:47 
AnswerRe: Any ideas on gettin a IDispatch interface from Remote Computer? Pin
Vi215-Sep-03 21:45
Vi215-Sep-03 21:45 
GeneralRe: Any ideas on gettin a IDispatch interface from Remote Computer? Pin
FearlessBurner16-Sep-03 11:22
FearlessBurner16-Sep-03 11:22 
GeneralMFC + TNA Pin
BERNERT15-Sep-03 4:02
BERNERT15-Sep-03 4:02 
GeneralRight Click Pin
Srikar Y14-Sep-03 0:20
Srikar Y14-Sep-03 0:20 
GeneralRe: Right Click Pin
Jagadeesh VN16-Sep-03 22:39
Jagadeesh VN16-Sep-03 22:39 
Questioninstallation and DLL? Pin
Maverick13-Sep-03 1:05
Maverick13-Sep-03 1:05 
AnswerRe: installation and DLL? Pin
Jagadeesh VN16-Sep-03 22:43
Jagadeesh VN16-Sep-03 22:43 
GeneralIOleCommandTarget Impleimentation Pin
suri babu12-Sep-03 5:16
suri babu12-Sep-03 5:16 
GeneralIs this safe? (map) Pin
Jeremy Pullicino11-Sep-03 3:45
Jeremy Pullicino11-Sep-03 3:45 
GeneralUsing ActiveX Pin
daphna10-Sep-03 4:21
daphna10-Sep-03 4:21 
GeneralRe: Using ActiveX Pin
Jagadeesh VN10-Sep-03 9:44
Jagadeesh VN10-Sep-03 9:44 
Question_bstr_t::copy and memory leak? Pin
Maverick9-Sep-03 22:56
Maverick9-Sep-03 22:56 
AnswerRe: _bstr_t::copy and memory leak? Pin
Jagadeesh VN9-Sep-03 23:27
Jagadeesh VN9-Sep-03 23:27 
GeneralRe: _bstr_t::copy and memory leak? Pin
Steve S11-Sep-03 1:58
Steve S11-Sep-03 1:58 
GeneralRe: _bstr_t::copy and memory leak? Pin
Maverick11-Sep-03 22:06
Maverick11-Sep-03 22:06 
GeneralRe: _bstr_t::copy and memory leak? Pin
doisy18-Sep-03 3:01
doisy18-Sep-03 3:01 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.