public static string GetMessage(IntPtr dllHandle, int messageTableId, int messageLanguageId, int messageId)
        {
            IntPtr messageEntry;
            string message;

            NtStatus status = Win32.RtlFindMessage(
                dllHandle,
                messageTableId,
                messageLanguageId,
                messageId,
                out messageEntry
                );

            if (status.IsError())
            {
                return(null);
            }

            MemoryRegion         region = new MemoryRegion(messageEntry);
            MessageResourceEntry entry  = region.ReadStruct <MessageResourceEntry>();

            // Read the message, depending on format.
            if ((entry.Flags & MessageResourceFlags.Unicode) == MessageResourceFlags.Unicode)
            {
                message = region.ReadUnicodeString(MessageResourceEntry.TextOffset);
            }
            else
            {
                message = region.ReadAnsiString(MessageResourceEntry.TextOffset);
            }

            return(message);
        }
Exemple #2
0
        /// <summary>
        /// Dynamically lookup a function in the dll via kernel32!GetProcAddress.
        /// </summary>
        /// <param name="functionName">raw name of the function in the export table.</param>
        /// <returns>null if function is not found. Else a delegate to the unmanaged function.</returns>
        /// <remarks>GetProcAddress results are valid as long as the dll is not yet unloaded. This
        /// is very very dangerous to use since you need to ensure that the dll is not unloaded
        /// until after you're done with any objects implemented by the dll. For example, if you
        /// get a delegate that then gets an IUnknown implemented by this dll,
        /// you can not dispose this library until that IUnknown is collected. Else, you may free
        /// the library and then the CLR may call release on that IUnknown and it will crash.</remarks>
        public TDelegate GetUnmanagedFunction <TDelegate>(string functionName) where TDelegate : class
        {
            using (AnsiString str = new AnsiString(functionName))
            {
                IntPtr functionPtr;

                NtStatus result = Win32.LdrGetProcedureAddress(this, str.Buffer, 0, out functionPtr);

                // Failure is a common case, especially for adaptive code.
                if (result.IsError())
                {
                    //result.ReturnException().Log();

                    return(null);
                }

                Delegate function = Marshal.GetDelegateForFunctionPointer(functionPtr, typeof(TDelegate));

                // Ideally, we'd just make the constraint on TDelegate be
                // System.Delegate, but compiler error CS0702 (constrained can't be System.Delegate)
                // prevents that. So we make the constraint system.object and do the cast from object-->TDelegate.
                object o = function;

                return((TDelegate)o);
            }
        }
Exemple #3
0
 /// <summary>
 /// Throws the NT status value as an exception if it is an error or warning.
 /// </summary>
 /// <param name="status">The NT status value.</param>
 public static void ThrowIf(this NtStatus status)
 {
     if (status.IsError() || status.IsWarning())
     {
         status.Throw();
     }
 }
Exemple #4
0
        /// <summary>
        /// LoadLibraryEx constructor to load a dll and be responsible for freeing it.
        /// </summary>
        /// <param name="dllName">full path name of dll to load</param>
        /// <param name="flags"></param>
        /// <remarks>Throws exceptions on failure. Most common failure would be file-not-found, or that the file is not a loadable image.</remarks>
        public NativeLibrary(string dllName, LoadLibraryFlags flags)
        {
            UnicodeString str = new UnicodeString(dllName);

            IntPtr ptr;

            NtStatus result = Win32.LdrLoadDll(null, (int)flags, ref str, out ptr);

            if (result.IsError())
            {
                this.MarkAsInvalid();
            }

            str.Dispose();
        }
Exemple #5
0
        private MemoryAlloc GetPropertiesInformation()
        {
            int retLength;

            MemoryAlloc data = new MemoryAlloc(0x1000);

            NtStatus status = Win32.NtQueryInformationTransaction(
                this,
                TransactionInformationClass.TransactionPropertiesInformation,
                data,
                data.Size,
                out retLength
                );

            if (status == NtStatus.BufferTooSmall)
            {
                // Resize the buffer and try again.
                data.ResizeNew(retLength);

                status = Win32.NtQueryInformationTransaction(
                    this,
                    TransactionInformationClass.TransactionPropertiesInformation,
                    data,
                    data.Size,
                    out retLength
                    );
            }

            if (status.IsError())
            {
                data.Dispose();
                status.Throw();
            }

            return(data);
        }