|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Which to use, AllocCoTaskMem or AllocHGlobalIf 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 **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 Show quoteHide quote " **Developer**" <REMOVEdevelo***@a-znet.com> wrote in message news:uwAWFQ0fFHA.1960@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 | | | | 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) Show quoteHide quote "Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_***@msn.com> wrote in message news:OaWhQDSgFHA.3512@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**" <REMOVEdevelo***@a-znet.com> wrote in message > news:uwAWFQ0fFHA.1960@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 > | > | > | > | > > **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/default.asp?url=/library/en-us/com/html/c4cb588d-9482-4f90-a92e-75b604540d5c.asp And GlobalAlloc (LocalAlloc): http://msdn.microsoft.com/library/default.asp?url=/library/en-us/memory/base/globalalloc.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 Show quoteHide quote " **Developer**" <REMOVEdevelo***@a-znet.com> wrote in message news:Of$nN6VgFHA.2644@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]" <Jay_Harlow_***@msn.com> wrote in message | news:OaWhQDSgFHA.3512@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**" <REMOVEdevelo***@a-znet.com> wrote in message | > news:uwAWFQ0fFHA.1960@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 | > | | > | | > | | > | | > | > | | I'm waiting for your book. You really get to the bottom of it.
Thanks PS > | Will I know that I need COM memory? How about:> 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 ;-)) > need to is not as bad as calling AllocHGlobal when you should call AllocCoTaskMem when you do need to. Show quoteHide quote > > Hope this helps > Jay > ..
Does String mashal default to UnmanagedType.LPTStr
(newbie warning) vb.net, stdregprov, deletekey - Invalid cast use ADOX for this :-) Transparent Color in an Icon questions about VB.NET, and uses in education Is there code to convert a c# module to VB? Loading two separate instance of the same assembly score object property values against a decision rule created from from database table record values Adding points to a bitmap Access DB questions |
|||||||||||||||||||||||