ALFA.Shared.ServerInterop.AllocateServerHeap C# (CSharp) Метод

AllocateServerHeap() статический приватный Метод

Allocate unmanaged memory on the server heap. The caller is responsible for freeing the memory.
static private AllocateServerHeap ( UInt32 Size ) : IntPtr
Size System.UInt32 Supplies the allocation size.
Результат System.IntPtr
        internal static IntPtr AllocateServerHeap(UInt32 Size)
        {
            IntPtr HeapMgr = NWN2_HeapMgr_Instance();
            IntPtr Heap = NWN2_HeapMgr_GetDefaultHeap(HeapMgr);
            IntPtr P;

            P = NWN2_Heap_Allocate(Heap, Size);

            if (P == IntPtr.Zero)
                throw new OutOfMemoryException();
            else
                return P;
        }

Usage Example

            /// <summary>
            /// RetrieveCampaignObject hook.  Called when the server script
            /// function RetrieveCampaignObject is called by any script.
            /// </summary>
            /// <param name="CodeBaseObject">Supplies the code base this
            /// pointer.</param>
            /// <param name="CampaignName">Supplies the campaign name.</param>
            /// <param name="VarName">Supplies the variable name.</param>
            /// <param name="PlayerName">Supplies the player name.</param>
            /// <param name="Cls">Always receives 'O'.</param>
            /// <param name="Size">Receives the size of the restored object GFF
            /// data.</param>
            /// <returns>An unmanaged pointer on the server heap that contains
            /// the raw GFF data for the object.  IntPtr.Zero if there is no
            /// data to restore.</returns>
            private IntPtr GetBinaryDataHook(IntPtr CodeBaseObject, IntPtr CampaignName, IntPtr VarName, IntPtr PlayerName, out Byte Cls, out UInt32 Size)
            {
                string CampaignNameStr = ServerInterop.ReadCExoString(CampaignName);

                //
                // If the campaign is the virtual database, call registered
                // event handlers.
                //

                if (CampaignNameStr.StartsWith("VDB_"))
                {
                    Cls = (byte)'O';

                    try
                    {
                        RetrieveCampaignDatabaseEventArgs EventArgs;
                        string VarNameStr    = ServerInterop.ReadCExoString(VarName);
                        string PlayerNameStr = ServerInterop.ReadCExoString(PlayerName);

                        EventArgs = new RetrieveCampaignDatabaseEventArgs(CampaignNameStr.Substring(4), VarNameStr, PlayerNameStr);
                        CampaignDatabase.RetrieveCampaignDatabaseEvent(null, EventArgs);

                        //
                        // If no handler instantiated a GFF, then return no
                        // data found.  Otherwise, allocate a buffer from the
                        // unmanaged server heap, and copy the contents of the
                        // GFF into the heap buffer and return the heap buffer.
                        //

                        if (EventArgs.GFF == null || EventArgs.GFF.Length == 0)
                        {
                            Size = 0;
                            return(IntPtr.Zero);
                        }
                        else
                        {
                            IntPtr GFFBuffer = IntPtr.Zero;

                            try
                            {
                                GFFBuffer = ServerInterop.AllocateServerHeap((uint)EventArgs.GFF.Length);
                                Marshal.Copy(EventArgs.GFF, 0, GFFBuffer, EventArgs.GFF.Length);
                                Size = (uint)EventArgs.GFF.Length;

                                return(GFFBuffer);
                            }
                            catch (Exception e)
                            {
                                Logger.Log("CampaignDatabaseHook.GetBinaryDataHook: Exception: {0}", e);

                                if (GFFBuffer != IntPtr.Zero)
                                {
                                    ServerInterop.FreeServerHeap(GFFBuffer);
                                    GFFBuffer = IntPtr.Zero;
                                }
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        Logger.Log("CampaignDatabaseHook.GetBinaryDataHook: Exception: {0}", e);

                        Size = 0;
                        return(IntPtr.Zero);
                    }
                }

                //
                // Pass through to the original server implementation that uses
                // the standard campaign database.
                //

                return(CCodeBase_GetBinaryData_OriginalDelegate(CodeBaseObject, CampaignName, VarName, PlayerName, out Cls, out Size));
            }