Example #1
0
        private void RecycleLongMsgBuffer(UIntPtr ptr)
        {
            var newPtr = ptr.ToIntPtr();
            var size   = (uint)Marshal.SizeOf(typeof(MIDIHDR));

            CheckReturnCode(Win32API.midiInUnprepareHeader(_handle, newPtr, size));
            CheckReturnCode(Win32API.midiInPrepareHeader(_handle, newPtr, size));
            CheckReturnCode(Win32API.midiInAddBuffer(_handle, newPtr, size));
            //return unchecked((UIntPtr)(ulong)(long)newPtr);
        }
Example #2
0
        private void DestroyLongMsgBuffer(UIntPtr ptr)
        {
            var newPtr = ptr.ToIntPtr();
            var size   = (uint)Marshal.SizeOf(typeof(MIDIHDR));

            CheckReturnCode(Win32API.midiInUnprepareHeader(_handle, newPtr, size));

            var header = (MIDIHDR)Marshal.PtrToStructure(newPtr, typeof(MIDIHDR));

            Marshal.FreeHGlobal(header.lpData);
            Marshal.FreeHGlobal(newPtr);

            _longMsgBuffers.Remove(newPtr);
        }
Example #3
0
        /// <summary>
        ///     Returns true if the given long message describes a SysEx message.
        /// </summary>
        /// <param name="dwParam1">The dwParam1 arg passed to MidiInProc.</param>
        /// <param name="dwParam2">The dwParam2 arg passed to MidiInProc.</param>
        public static bool IsSysEx(UIntPtr dwParam1, UIntPtr dwParam2)
        {
            var newPtr = dwParam1.ToIntPtr();

            try
            {
                Marshal.PtrToStructure(newPtr, typeof(MIDIHDR));
                return(true);
            }
            catch (Exception)
            {
                return(false);
            }
        }
Example #4
0
        /// <summary>
        ///     Decodes a SysEx long message.
        /// </summary>
        /// <param name="dwParam1">The dwParam1 arg passed to MidiInProc.</param>
        /// <param name="dwParam2">The dwParam2 arg passed to MidiInProc.</param>
        /// <param name="data">The SysEx data to send.</param>
        /// <param name="timestamp">
        ///     Filled in with the timestamp in microseconds since
        ///     midiInStart().
        /// </param>
        public static void DecodeSysEx(UIntPtr dwParam1, UIntPtr dwParam2, out byte[] data, out uint timestamp)
        {
            //if (!IsSysEx(dwParam1, dwParam2))
            //{
            //    throw new ArgumentException("Not a SysEx message.");
            //}
            var newPtr = dwParam1.ToIntPtr();
            var header = (MIDIHDR)Marshal.PtrToStructure(newPtr, typeof(MIDIHDR));

            data = new byte[header.dwBytesRecorded];
            for (var i = 0; i < header.dwBytesRecorded; i++)
            {
                //Array.Resize<byte>(ref data, data.Length + 1);
                //data[data.Length - 1] = System.Runtime.InteropServices.Marshal.ReadByte(header.lpData, i);
                data[i] = Marshal.ReadByte(header.lpData, i);
            }
            timestamp = (uint)dwParam2;
        }
Example #5
0
 /// <summary>
 /// Performs the addition operation with the given values.
 /// </summary>
 /// <param name="left">The left side value.</param>
 /// <param name="right">The right side value.</param>
 /// <param name="wrapAround">Whether or not values will wrap if the operation overflows. Otherwise, cap out at IntPtr.MaxValue or IntPtr.MinValue.</param>
 /// <returns>The result of the operation.</returns>
 public static UIntPtr Add(this UIntPtr left, dynamic right, Boolean wrapAround = true)
 {
     return(IntPtrExtensions.DoOperation(left.ToIntPtr(), right, ExpressionType.Add, wrapAround).ToUIntPtr());
 }
Example #6
0
 /// <summary>
 /// Performs the modulo operation with the given values.
 /// </summary>
 /// <param name="left">The left side value.</param>
 /// <param name="right">The right side value.</param>
 /// <returns>The result of the operation.</returns>
 public static UIntPtr Mod(this UIntPtr left, dynamic right)
 {
     return(IntPtrExtensions.DoOperation(left.ToIntPtr(), right, ExpressionType.Modulo).ToUIntPtr());
 }
Example #7
0
 /// <summary>
 /// Performs the division operation with the given values.
 /// </summary>
 /// <param name="left">The left side value.</param>
 /// <param name="right">The right side value.</param>
 /// <returns>The result of the operation.</returns>
 public static UIntPtr Divide(this UIntPtr left, dynamic right)
 {
     return(IntPtrExtensions.DoOperation(left.ToIntPtr(), right, ExpressionType.Multiply).ToUIntPtr());
 }
Example #8
0
        public void SetUserDefinedSymbolAllocSize(String name, UInt32 size, UIntPtr preferredAddress)
        {
            const String PREV_DEC = "The symbol named %s was previously declared with a size of %s instead of %s." +
                                    " all scripts that use this memory must give the same size. " +
                                    "Adjust the size, or delete the old alloc from the userdefined symbol list";

            if (size == 0)
            {
                throw new Exception("Please provide a bigger size");
            }
            UIntPtr p;
            int     i;

            for (i = 0; i < UserDefinedSymbols.Length; i++)
            {
                if (!UserDefinedSymbols[i].IsMatch(name))
                {
                    continue; //it exists, check first
                }
                if (UserDefinedSymbols[i].AllocSize > 0 && UserDefinedSymbols[i].ProcessId == Process.Native.Id)
                {
                    if (size != UserDefinedSymbols[i].AllocSize)
                    {
                        throw new Exception(UStringUtils.Sprintf(PREV_DEC, UserDefinedSymbols[i].Name, UserDefinedSymbols[i].AllocSize, size));
                    }
                }
                if (UserDefinedSymbols[i].ProcessId != Process.Native.Id)
                {
                    if (preferredAddress != UIntPtr.Zero)
                    {
                        p = AMemoryHelper.Allocate(Process.Handle, preferredAddress.ToIntPtr(), (int)size).ToUIntPtr();
                    }
                    else
                    {
                        p = AMemoryHelper.Allocate(Process.Handle, (int)size).ToUIntPtr();
                    }
                    if (p == UIntPtr.Zero)
                    {
                        throw new Exception("Error allocating memory");
                    }
                    UserDefinedSymbols[i].Address       = p;
                    UserDefinedSymbols[i].AddressString = AStringUtils.IntToHex(p, 8);
                    UserDefinedSymbols[i].AllocSize     = size;
                    UserDefinedSymbols[i].ProcessId     = Process.Native.Id;
                }
                return; // Redefined the symbol and exit;
            }
            //Still here, symbol Not exists, let's define a new one.
            if (preferredAddress != UIntPtr.Zero)
            {
                p = AMemoryHelper.Allocate(Process.Handle, preferredAddress.ToIntPtr(), (int)size).ToUIntPtr();
            }
            else
            {
                p = AMemoryHelper.Allocate(Process.Handle, (int)size).ToUIntPtr();
            }
            if (p == UIntPtr.Zero)
            {
                throw new Exception("Error allocating memory");
            }
            AddUserDefinedSymbol(AStringUtils.IntToHex(p, 8), name);
            UserDefinedSymbols[i].AllocSize = size;
            UserDefinedSymbols[i].ProcessId = Process.Native.Id;
        }
Example #9
0
        public void ParseStringAndWriteToAddress(String value, UIntPtr address, AVariableType variableType, Boolean hexadecimal = false)
        {
            var v = 0UL;
            var s = 0.0f;
            var d = 0.0;
            var x = UIntPtr.Zero;

            if (hexadecimal && (variableType == AVariableType.Single || variableType == AVariableType.Double))
            {
                if (variableType == AVariableType.Single)
                {
                    variableType = AVariableType.DWord;
                }
                else
                {
                    variableType = AVariableType.QWord;
                }
            }
            if (variableType == AVariableType.ByteArray)
            {
                var b = new ATByteArray();
                AStringUtils.ConvertStringToBytes(value, hexadecimal, b);
                for (var i = 0; i < b.Length; i++)
                {
                    Proc.Memory.Write(UIntPtr.Add(address, i).ToIntPtr(), (Byte)b[i]);
                }
            }
            else
            {
                if (variableType == AVariableType.Single || variableType == AVariableType.Double)
                {
                    d = UStringUtils.StringToDouble(value);
                    s = UStringUtils.StringToFloat(value);
                }
                else
                {
                    if (!(variableType == AVariableType.String || variableType == AVariableType.UnicodeString))
                    {
                        if (hexadecimal)
                        {
                            value = "0x" + value;
                        }
                        v = AStringUtils.StrToQWordEx(value);
                        // todo make custom work
                        //if ((variableType == AVariableType.Custom) && (customtype != nil) & customtype.scriptusesfloat)
                        //s = UStringUtils.StringToFloat(value);
                    }
                }
                switch (variableType)
                {
                case AVariableType.Byte:
                    Proc.Memory.Write(address.ToIntPtr(), (Byte)v);
                    break;

                case AVariableType.Word:
                    Proc.Memory.Write(address.ToIntPtr(), (UInt16)v);
                    break;

                case AVariableType.DWord:
                    Proc.Memory.Write(address.ToIntPtr(), (UInt32)v);
                    break;

                case AVariableType.QWord:
                    Proc.Memory.Write(address.ToIntPtr(), v);
                    break;

                case AVariableType.Single:
                    Proc.Memory.Write(address.ToIntPtr(), s);
                    break;

                case AVariableType.Double:
                    Proc.Memory.Write(address.ToIntPtr(), d);
                    break;

                case AVariableType.String:
                    Proc.Memory.Write(address.ToIntPtr(), value, Encoding.ASCII);
                    break;

                case AVariableType.UnicodeString:
                    Proc.Memory.Write(address.ToIntPtr(), value, Encoding.UTF8);
                    break;

                case AVariableType.Custom:     // todo make custom work
                    //{
                    //    if (customtype != nil)
                    //    {
                    //        getmem(ba, customtype.bytesize);
                    //        //try
                    //        if (readprocessmemory(processhandle, (pointer)(address), ba, customtype.bytesize, x))
                    //        {
                    //            if (customtype.scriptusesfloat)
                    //                customtype.convertfloattodata(s, ba, address);
                    //            else
                    //                customtype.convertintegertodata(v, ba, address);
                    //
                    //            writeprocessmemory(processhandle, (pointer)(address), ba, customtype.bytesize, x);
                    //        }
                    //        //finally
                    //        freememandnil(ba);
                    //        //end;
                    //    }
                    //}
                    break;
                }
            }
        }