**Developer**,
| Will I know that I need COM memory?
Yes! At least I would hope one would, by being familiar with the API that
you are calling to know if it requires the COM allocator or not.
Looking at the DocumentProperties API I do not see that any of its
parameters require the COM allocator, ergo I would not use AllocCoTaskMem, I
would use AllocHGlobal instead. My choice to use AllocHGlobal of course may
be wrong as I quickly looked at the DocumentProperties API on MSDN just now.
For example I know the Structured Storage API & MAPI "requires" COM
allocator, however I don't see where on MSDN that Structured Storage
requires the COM allocator... MAPI has its own API calls for memory that I
understand uses an IMalloc.
When reviewing AllocCoTaskMem & AllocHGlobal it helps to understand the
CoTaskMemAlloc API:
http://msdn.microsoft.com/library/de...b604540d5c.asp
And GlobalAlloc (LocalAlloc):
http://msdn.microsoft.com/library/de...lobalalloc.asp
My understanding is that AllocCoTaskMem (aka CoTaskMemAlloc) will allocate
memory via the standard implementation of IMalloc. What I don't know is
where CoTaskMemAlloc allocates its memory out of (if its the "directly" the
same "pool" as GlobalAlloc (if it simply calls GlobalAlloc) or if it
allocates a Heap & allocates from that (HeapCreate & HeapAlloc APIs).
| Will I know that I need COM memory?
I think the question might really be: does it hurt calling AllocCoTaskMem
instead of AllocHGlobal? I suspect calling AllocCoTaskMem when you don't
need to is not as bad as not calling AllocCoTaskMem when you do need to. (if
that made any sense ;-))
Or: can FreeHGlobal (GlobalFree API) really safely be used interchangable
with FreeCoTaskMem (CoTaskMemFree API)? If you are allocating & freeing the
memory it really doesn't matter; if you are allocating the memory & someone
else frees it then I would say it does matter.
I would expect Adam Nathan's book to address this, however I'm not seeing it
in his book right now.
Hope this helps
Jay
" **Developer**" <RE*************@a-znet.com> wrote in message
news:Of**************@TK2MSFTNGP09.phx.gbl...
| I've seen a lot of examples that use AllocCoTaskMem (see below)
|
| Will I know that I need COM memory? What I mean is I will be obvious donig
| "COM" things not simply calling some Windows API and not knowing I'm
dealing
| with COM. Somehow, I wonder about the FORMATRANGE example becaues I know
| the RichTextBox windows control uses COM at times.
|
| Thanks again
|
|
|
|
| 'Allocate memory for the FORMATRANGE struct and
|
| 'copy the contents of our struct to this memory
|
| Dim lParam As IntPtr
|
| lParam = Marshal.AllocCoTaskMem(Marshal.SizeOf(lFr))
|
| Marshal.StructureToPtr(lFr, lParam, False)
|
|
|
|
|
|
|
| Dim lPointerToDevMode As IntPtr = Marshal.AllocCoTaskMem(lDevModeDataSize)
|
| lRet = WinSpool.DocumentProperties(IntPtr.Zero, lPrinterHandle,
printerName,
| lPointerToDevMode, IntPtr.Zero, GDI.DM_OUT_BUFFER)
|
| If (lRet < 0) Then
|
| MsgBox("Cannot get the DEVMODE structure.")
|
| GoTo cleanup
|
| End If
|
|
|
|
|
|
|
| lPointerToDevMode = Marshal.AllocCoTaskMem(lSizeOfDevMode)
|
| lFlag = WinSpool.DocumentProperties(IntPtr.Zero, lPrinterHandle,
| printerName, lPointerToDevMode, IntPtr.Zero, GDI.DM_OUT_BUFFER)
|
|
|
|
|
| "Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
| news:Oa**************@TK2MSFTNGP10.phx.gbl...
| > **Developer**,
| > AllocCoTaskMem is used to allocate COM memory, unless I knew I needed to
| > allocate COM memory (memory that another COM object/API needed to
release)
| > I
| > would use AllocHGlobal.
| >
| > In other words I use AllocHGlobal, as I have yet needed to use
| > AllocCoTaskMem.
| >
| > Hope this helps
| > Jay
| >
| > " **Developer**" <RE*************@a-znet.com> wrote in message
| > news:uw**************@TK2MSFTNGP10.phx.gbl...
| > | If I need to allocate memory, maybe because I'm going to:
| > | Marshal.StructureToPtr
| > | Does it matter which of the following I use?
| > |
| > | Marshal.AllocCoTaskMem
| > | Marshal.AllocHGlobal
| > | I know the arguments are different which would make one more convient
in
| > a
| > | given situation, but other that that does it make any signifficant
| > | difference.
| > |
| > |
| > |
| > | Thanks
| > |
| > |
| > |
| > |
| >
| >
|
|