コード例 #1
0
ファイル: API.cs プロジェクト: WalterBarrett/win3mu
        public static void BuildStub(string win3muExe, string win16Exe, string targetExe, bool renameWin16Exe)
        {
            unsafe
            {
                string createdTemp = null;
                try
                {
                    // Open the source exe and locate the first icon
                    using (var neFile = new NeFileReader(win16Exe))
                    {
                        // Copy stub to target directory as a temp file
                        var tmp = targetExe + ".tmp";
                        if (System.IO.File.Exists(tmp))
                        {
                            System.IO.File.Delete(tmp);
                        }
                        System.IO.File.Copy(win3muExe, tmp);
                        createdTemp = tmp;


                        // Get the icon
                        var rtIconGroup = neFile.FindResourceType(Win16.ResourceType.RT_GROUP_ICON.ToString());
                        if (rtIconGroup != null)
                        {
                            // Get the first one
                            var rIconGroup = rtIconGroup.resources[0];

                            var iconDir = Resources.LoadIconOrCursorGroup(neFile.GetResourceStream(rtIconGroup.name, rIconGroup.name));

                            // Load it
                            var rIconGroupData = neFile.LoadResource(rtIconGroup.name, rIconGroup.name);

                            // Start up
                            var hUpdate = ResourceUtils.BeginUpdateResource(createdTemp, false);

                            // Copy the group
                            fixed(byte *p = rIconGroupData)
                            {
                                ResourceUtils.UpdateResource(hUpdate, (IntPtr)Win16.ResourceType.RT_GROUP_ICON, (IntPtr)rIconGroup.id, 0, (IntPtr)p, (uint)rIconGroupData.Length);
                            }

                            // Copy each icon from the group
                            foreach (var e in iconDir.Entries)
                            {
                                var rIconData = neFile.LoadResource(Win16.ResourceType.RT_ICON.ToString(), string.Format("#{0}", e.nId));
                                fixed(byte *p = rIconData)
                                {
                                    ResourceUtils.UpdateResource(hUpdate, (IntPtr)Win16.ResourceType.RT_ICON, (IntPtr)e.nId, 0, (IntPtr)p, (uint)rIconData.Length);
                                }
                            }

                            // Delete the old version resource so that "Win3muProxy" doesn't appear in task manager
                            ResourceUtils.UpdateResource(hUpdate, (IntPtr)Win16.ResourceType.RT_VERSION, (IntPtr)1, 0, IntPtr.Zero, 0);

                            /*
                             * var rVersion = neFile.LoadResource(Win16.ResourceType.RT_VERSION.ToString(), "#1");
                             * if (rVersion!=null)
                             * {
                             *  fixed (byte* p = rVersion)
                             *  {
                             *      ResourceUtils.UpdateResource(hUpdate, (IntPtr)Win16.ResourceType.RT_VERSION, (IntPtr)1, 0, (IntPtr)p, (uint)rVersion.Length);
                             *  }
                             * }
                             */

                            // Update!
                            ResourceUtils.EndUpdateResource(hUpdate, false);
                        }
                    }

                    if (renameWin16Exe)
                    {
                        // Rename .exe to .exe16
                        System.IO.File.Move(win16Exe, win16Exe + "16");
                    }

                    // Rename the target
                    if (System.IO.File.Exists(targetExe))
                    {
                        System.IO.File.Delete(targetExe);
                    }
                    System.IO.File.Move(createdTemp, targetExe);
                }
                catch
                {
                    if (createdTemp != null)
                    {
                        System.IO.File.Delete(createdTemp);
                    }
                    throw;
                }
            }
        }