473,503 Members | 9,912 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Marshal.AllocHGlobal versus Marshal.AllocCoTaskMem

Hello,
Regarding Marshal.AllocHGlobal versus Marshal.AllocCoTaskMem Does anyone
know the circumstances in which one should use one over the other?

http://msdn.microsoft.com/library/de...us/cpref/html/
frlrfsystemruntimeinteropservicesmarshalclassalloc cotaskmemtopic.asp

http://msdn.microsoft.com/library/de...us/dllproc/bas
e/overlapped_str.asp

Above are the relevant links to MSDN. The details are fairly sparse in
terms of useage guidance.

Thanks in advance.
-Mike
Nov 21 '05 #1
2 4549
Regarding Marshal.AllocHGlobal versus Marshal.AllocCoTaskMem Does anyone
know the circumstances in which one should use one over the other?


If you're passing the pointer to an API that specifically says the
memory should be allocated with CoTaskMemAlloc, then you should use
AllocCoTaskMem. In most other situations AllocHGlobal will work just
as well.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 21 '05 #2
Thanks Mattias. Just as you say, so far i've found that either works just
as well. I'm thinking maybe ill stick to using CoTaskMemAlloc whenever i
need to pass a pointer to an API and AllocHGlobal in all other circumstances
that may arise.
-Mike
"Mattias Sjögren" <ma********************@mvps.org> wrote in message
news:eZ*************@TK2MSFTNGP12.phx.gbl...
Regarding Marshal.AllocHGlobal versus Marshal.AllocCoTaskMem Does anyone
know the circumstances in which one should use one over the other?


If you're passing the pointer to an API that specifically says the
memory should be allocated with CoTaskMemAlloc, then you should use
AllocCoTaskMem. In most other situations AllocHGlobal will work just
as well.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Nov 21 '05 #3

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

Similar topics

2
10202
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
4190
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...
0
1325
by: Hypnotron | last post by:
>Hello, >Regarding Marshal.AllocHGlobal versus Marshal.AllocCoTaskMem Does anyone >know the circumstances in which one should use one over the other? >...
13
2149
by: Just Me | last post by:
The following almost works. The problem is Marshal.PtrToStringAuto seems to terminate at the first null so I don't get the full string. Any suggestions on how to fix this? Or how to improve the...
4
7600
by: **Developer** | last post by:
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...
2
8122
by: scottt | last post by:
I need to call into a C++ DLL from my C# code. The function is expecting a void pointer to an unsigned short. Which would be more correct? UInt16 wRegData; IntPtr p =...
1
6297
by: nicewenyan | last post by:
I want to pass a managed c# byte (8 bit) array into a unmanaged c++ function: extern "C" void AddData(unsigned int* data); I use P/Invoke on managed side to do the marshaling as following: ...
2
7177
by: O.B. | last post by:
When using Marshal to copy data from a byte array to the structure below, only the first byte of the "other" array is getting copied from the original byte array. What do I need to specify to get...
0
2100
by: Charming12 | last post by:
Hi All, I have a strange problem and due to my inefficiency with IntPtr i am unable to figure it out. I have an structure something like: public struct Detail { public int age; public...
0
7207
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7095
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
7294
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
7361
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...
1
7015
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
5602
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,...
0
4693
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
1523
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
749
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.