.Net中直接操作内存


      
        
private void InternalWriteEvent(uint eventID, ushort category, EventLogEntryType type, string[] strings, byte[] rawData, string currentMachineName);
Declaring Type: System.Diagnostics.EventLogInternal
Assembly: System, Version=4.0.0.0
private void InternalWriteEvent(uint eventID, ushort category, EventLogEntryType type, string[] strings, byte[] rawData, string currentMachineName)

{

    if (strings == null) strings = new string[0];

    if (strings.Length >= 0x100) throw new ArgumentException(SR.GetString("TooManyReplacementStrings"));

    for (int i = 0; i < strings.Length; i++)

    {

        if (strings[i] == null) strings[i] = string.Empty;

        if (strings[i].Length > 0x7ffe) throw new ArgumentException(SR.GetString("LogEntryTooLong"));

    }

    if (rawData == null) rawData = new byte[0];

    if (this.Source.Length == 0) throw new ArgumentException(SR.GetString("NeedSourceToWrite"));

    if (!this.IsOpenForWrite) this.OpenForWrite(currentMachineName);

    IntPtr[] ptrArray = new IntPtr[strings.Length];

    GCHandle[] handleArray = new GCHandle[strings.Length];

    GCHandle handle = GCHandle.Alloc(ptrArray, GCHandleType.Pinned);

    try

    {

        for (int j = 0; j < strings.Length; j++)

        {

            handleArray[j] = GCHandle.Alloc(strings[j], GCHandleType.Pinned);

            ptrArray[j] = handleArray[j].AddrOfPinnedObject();

        }

        byte[] userSID = null;

        if (!UnsafeNativeMethods.ReportEvent(this.writeHandle, (short) type, category, eventID, userSID, (short) strings.Length, rawData.Length, new HandleRef(this, handle.AddrOfPinnedObject()), rawData)) throw SharedUtils.CreateSafeWin32Exception();

    }

    finally

    {

        for (int k = 0; k < strings.Length; k++)

        {

            if (handleArray[k].IsAllocated) handleArray[k].Free();

        }

        handle.Free();

    }

}

如果不喜欢GC的自动垃圾回收,可以试试这种方式,说不定效果更好.
 

你可能感兴趣的:(.net)