Exemple #1
0
        /// <summary>
        /// Executes the task
        /// </summary>
        /// <returns></returns>
        public override bool Execute()
        {
            //TODO:Eliminare
            //System.Windows.Forms.MessageBox.Show(string.Format("Please attach the debugger (elevated on Vista or Win 7) to process [{0}].", System.Diagnostics.Process.GetCurrentProcess().Id), "Debug");
            //TODO:Eliminare
            string  tempCTO = null;
            CTCFile ctcFile = null;

            try
            {
                ctcFile = CompileCTO(out tempCTO);
                if (ctcFile != null)
                {
                    var satelliteDllFileName    = this.OutputFile;
                    var gatSatelliteDllFileName = GetGatSatelliteDllFileName();

                    if (File.Exists(satelliteDllFileName))
                    {
                        File.Delete(satelliteDllFileName);
                    }
                    File.Copy(gatSatelliteDllFileName, satelliteDllFileName);
                    File.SetAttributes(satelliteDllFileName, FileAttributes.Normal);
                    IntPtr hUpdate = NativeMethods.BeginUpdateResource(satelliteDllFileName, 1);
                    try
                    {
                        if (hUpdate.Equals(IntPtr.Zero))
                        {
                            Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error());
                        }
                        AddCTMENU(hUpdate, tempCTO);
                        AddBITMAP(hUpdate, ctcFile);

                        return(true);
                    }
                    finally
                    {
                        if (!hUpdate.Equals(IntPtr.Zero))
                        {
                            NativeMethods.EndUpdateResource(hUpdate, 0);
                            hUpdate = IntPtr.Zero;
                        }
                    }
                }
            }
            finally
            {
                ctcFile = null;
                if (tempCTO != null && File.Exists(tempCTO))
                {
#if !DEBUG
                    File.Delete(tempCTO);
#endif
                }
            }
            return(false);
        }
        /// <summary>
        /// Executes the task
        /// </summary>
        /// <returns></returns>
        public override bool Execute()
        {
            string  tempCTO = null;
            CTCFile ctcFile = null;

            try
            {
                ctcFile = CompileCTO(out tempCTO);
                if (ctcFile != null)
                {
                    var satelliteDllFileName    = this.OutputFile;
                    var gatSatelliteDllFileName = GetGatSatelliteDllFileName();

                    if (File.Exists(satelliteDllFileName))
                    {
                        File.Delete(satelliteDllFileName);
                    }
                    File.Copy(gatSatelliteDllFileName, satelliteDllFileName);
                    File.SetAttributes(satelliteDllFileName, FileAttributes.Normal);
                    IntPtr hUpdate = NativeMethods.BeginUpdateResource(satelliteDllFileName, 1);
                    try
                    {
                        if (hUpdate.Equals(IntPtr.Zero))
                        {
                            Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error());
                        }
                        AddCTMENU(hUpdate, tempCTO);
                        AddBITMAP(hUpdate, ctcFile);

                        return(true);
                    }
                    finally
                    {
                        if (!hUpdate.Equals(IntPtr.Zero))
                        {
                            NativeMethods.EndUpdateResource(hUpdate, 0);
                            hUpdate = IntPtr.Zero;
                        }
                    }
                }
            }
            finally
            {
                ctcFile = null;
                if (tempCTO != null && File.Exists(tempCTO))
                {
#if !DEBUG
                    File.Delete(tempCTO);
#endif
                }
            }
            return(false);
        }
 private void AddBITMAP(IntPtr hUpdate, CTCFile ctcFile)
 {
     if (ctcFile == null ||
         ctcFile.CMDSSection == null ||
         ctcFile.CMDSSection.BitmapsSubSection == null ||
         ctcFile.CMDSSection.BitmapsSubSection.Bitmaps == null)
     {
         return;
     }
     foreach (CTCBitmap ctcBitmap in ctcFile.CMDSSection.BitmapsSubSection.Bitmaps)
     {
         using (Bitmap bitmap = ctcBitmap.Bitmap)
         {
             if (bitmap == null)
             {
                 continue;
             }
             using (MemoryStream bitmapStream = new MemoryStream())
             {
                 bitmap.Save(bitmapStream, ImageFormat.Bmp);
                 // For some reason we need to remove the first 14 bytes of the BMP header,
                 // It seems that windows adds this header for us :(.
                 byte[] bitmapArray = new byte[bitmapStream.Length - NativeMethods.BMPHeaderSize];
                 Array.Copy(bitmapStream.GetBuffer(), NativeMethods.BMPHeaderSize, bitmapArray, 0, bitmapArray.Length);
                 int res = NativeMethods.UpdateResource(hUpdate,
                                                        NativeMethods.RT.BITMAP,
                                                        ctcBitmap.Command.ID,
                                                        (short)CultureInfo.CurrentUICulture.LCID,
                                                        bitmapArray,
                                                        bitmapArray.Length);
                 if (res == 0)
                 {
                     Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error());
                 }
             }
         }
     }
 }