Beispiel #1
0
        /// <summary>
        /// Stops the timer and deletes it
        /// </summary>
        public bool Stop()
        {
            if (_isRunning == false)
            {
                Log.Debug("Not running. returning");
                return(true);
            }

            bool retVal = true;

            try
            {
                retVal = Kernel32Interop.DeleteTimerQueueTimer(IntPtr.Zero, _timerHandle, IntPtr.Zero);

                _isRunning = false;

                if (!retVal)
                {
                    var getLastError = Marshal.GetLastWin32Error();
                    if (getLastError == ErrorIOPending)
                    {
                        retVal = true;
                    }
                }
            }
            catch
            {
            }

            return(retVal);
        }
Beispiel #2
0
        /// <summary>
        /// Start the timer. Optional param will be sent
        /// to the callback function
        /// </summary>
        /// <param name="param">parameter to send to callback</param>
        /// <returns>true on success</returns>
        public bool Start(IntPtr param)
        {
            int getLastError;

            if (_isRunning == true)
            {
                // already running
                Log.Debug("Timer is already running. returning");
                return(true);
            }

            bool retVal = Kernel32Interop.CreateTimerQueueTimer(
                out _timerHandle,
                IntPtr.Zero,
                _callback,
                param,
                _dueTime,
                _period,
                (uint)Flag.WT_EXECUTEINIOTHREAD);

            if (retVal)
            {
                _isRunning = true;
            }
            else
            {
                getLastError = Marshal.GetLastWin32Error();
                Log.Debug("Error while starting timer.  getLastError=" + getLastError);
            }

            return(retVal);
        }
Beispiel #3
0
        /// <summary>
        /// Returns the mapped file name of a memory mapped file
        /// </summary>
        /// <param name="hModule">handle to the module</param>
        /// <returns>mapped file name</returns>
        public static String GetMappedFileName(IntPtr hModule)
        {
            var       mappedFileName = String.Empty;
            const int bufLen         = 260;

            var buffer = new StringBuilder(bufLen);

            int len = Kernel32Interop.GetMappedFileName(Kernel32Interop.GetCurrentProcess(),
                                                        hModule,
                                                        buffer,
                                                        buffer.Capacity);

            if (len != 0)
            {
                mappedFileName = buffer.ToString();
            }

            return(mappedFileName);
        }
Beispiel #4
0
        /// <summary>
        /// Converts a filename from the \\Device\\HarddiskVolume1\\....\\abc.exe
        /// format to a Dos file name
        /// </summary>
        /// <param name="mappedFileName">input mapped file name</param>
        /// <returns>dos file name</returns>
        public static String ConvertMappedFileNameToDosFileName(String mappedFileName)
        {
            const int bufLen   = 260;
            var       fileName = String.Empty;

            for (var driveLetter = 'A'; driveLetter <= 'Z'; driveLetter++)
            {
                var drive  = driveLetter + ":";
                var buffer = new StringBuilder(bufLen);
                if (Kernel32Interop.QueryDosDevice(drive, buffer, buffer.Capacity) == 0)
                {
                    continue;
                }

                var devicePath = buffer.ToString();
                if (mappedFileName.StartsWith(devicePath))
                {
                    fileName = (drive + mappedFileName.Substring(devicePath.Length));
                    break;
                }
            }

            return(fileName);
        }