473,396 Members | 1,712 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Which to use, AllocCoTaskMem or AllocHGlobal

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


Nov 21 '05 #1
4 7588
**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
|
|
|
|
Nov 21 '05 #2
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
|
|
|
|

Nov 21 '05 #3
**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
| > |
| > |
| > |
| > |
| >
| >
|
|
Nov 21 '05 #4

I'm waiting for your book. You really get to the bottom of it.
Thanks

PS
| 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 ;-))

How about:
need to is not as bad as calling AllocHGlobal when you should call
AllocCoTaskMem when you do need to.

Hope this helps
Jay

..
Nov 21 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Jason | last post by:
Let's say I have an html form with 20 or 30 fields in it. The form submits the fields via POST to a php page which updates a table in a database with the $_POST vars. Which makes more sense? ...
2
by: Raymond H. | last post by:
Hello, I create a vb4 project which can also naviger on Internet via the WebBrowser control which I put on my form. My question is: if this program is installed on a station having already...
17
by: John Bentley | last post by:
John Bentley: INTRO The phrase "decimal number" within a programming context is ambiguous. It could refer to the decimal datatype or the related but separate concept of a generic decimal number....
65
by: Skybuck Flying | last post by:
Hi, I needed a method to determine if a point was on a line segment in 2D. So I googled for some help and so far I have evaluated two methods. The first method was only a formula, the second...
2
by: Stefan | last post by:
I have the following question The Marshal class contains a function to allocate a block of memory 1. Marshal.AllocHGlobal ( int cb 2. Marshal.AllocHGlobal ( IntPtr cb The first version I can...
1
by: dhornyak | last post by:
I have been banging my head against the wall for a while now, and can't seem to id the problem. I've been through a ton of posts and the code doesn't seem any different. Can anybody see it? When...
17
by: clintonG | last post by:
I'm using an .aspx tool I found at but as nice as the interface is I think I need to consider using others. Some can generate C# I understand. Your preferences please... <%= Clinton Gallagher ...
2
by: O.B. | last post by:
I have a structure named EntityState with an explicit layout. The following two operations exist within the class to return a byte array representing the current object. Upon executing them each a...
2
by: O.B. | last post by:
I have operation within a class that marshals the data into a byte array. Below are three different ways that work. Are there any downsides to using one over the the other? public virtual byte...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.