Skip to content

Commit

Permalink
Infrastructure Preparation 291
Browse files Browse the repository at this point in the history
  • Loading branch information
Taiizor committed Aug 7, 2024
1 parent 40029a3 commit 04d01ab
Show file tree
Hide file tree
Showing 8 changed files with 224 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Skylark" Version="3.1.4.6" />
<PackageReference Include="Skylark.Wing" Version="3.1.6.1" />
<PackageReference Include="Skylark.Wing" Version="3.1.6.3" />
</ItemGroup>
</Project>
104 changes: 104 additions & 0 deletions src/Skylark.Wing/Helper/ShortcutBasic.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
using System;
using System.IO;
using System.Reflection;
using SWMI = Skylark.Wing.Manage.Internal;
using SWNM = Skylark.Wing.Native.Methods;

namespace Skylark.Wing.Helper
{
/// <summary>
///
/// </summary>
public static class ShortcutBasic
{
/// <summary>
/// Create shortcut in current path.
/// </summary>
/// <param name="linkFileName">shortcut name(include .lnk extension.)</param>
/// <param name="targetPath">target path</param>
/// <param name="workingDirectory">working path</param>
/// <param name="arguments">arguments</param>
/// <param name="hotkey">hot key(ex: Ctrl+Shift+Alt+A)</param>
/// <param name="shortcutWindowStyle">window style</param>
/// <param name="description">shortcut description</param>
/// <param name="iconNumber">icon index(start of 0)</param>
/// <returns></returns>
/// <exception cref="FileNotFoundException"></exception>
public static void Create(string linkFileName, string targetPath, string workingDirectory = "", string arguments = "", string hotkey = "", SWNM.ShortcutWindowStyles shortcutWindowStyle = SWNM.ShortcutWindowStyles.WshNormalFocus, string description = "", int iconNumber = 0)
{
if (linkFileName.EndsWith(SWMI.DEFAULT_SHORTCUT_EXTENSION) == false)
{
linkFileName = string.Format("{0}{1}", linkFileName, SWMI.DEFAULT_SHORTCUT_EXTENSION);
}

if (File.Exists(targetPath) == false)
{
throw new FileNotFoundException(targetPath);
}

if (workingDirectory == string.Empty)
{
workingDirectory = Path.GetDirectoryName(targetPath);
}

string iconLocation = string.Format("{0},{1}", targetPath, iconNumber);

#if NET6_0_OR_GREATER
Type shellType = SWMI.M_TYPE;
dynamic shell = SWMI.M_SHELL;
dynamic shortcut = shell.CreateShortcut(linkFileName);

shortcut.TargetPath = targetPath;
shortcut.WorkingDirectory = workingDirectory;
shortcut.Arguments = arguments;
shortcut.Hotkey = hotkey;
shortcut.WindowStyle = shortcutWindowStyle;
shortcut.Description = description;
shortcut.IconLocation = iconLocation;

shortcut.Save();
#else
Type shellType = SWMI.M_TYPE;
object shell = SWMI.M_SHELL;
object shortcut = shellType.InvokeMethod("CreateShortcut", shell, linkFileName);
Type shortcutType = shortcut.GetType();

shortcutType.InvokeSetMember("TargetPath", shortcut, targetPath);
shortcutType.InvokeSetMember("WorkingDirectory", shortcut, workingDirectory);
shortcutType.InvokeSetMember("Arguments", shortcut, arguments);
shortcutType.InvokeSetMember("Hotkey", shortcut, hotkey);
shortcutType.InvokeSetMember("WindowStyle", shortcut, shortcutWindowStyle);
shortcutType.InvokeSetMember("Description", shortcut, description);
shortcutType.InvokeSetMember("IconLocation", shortcut, iconLocation);

shortcutType.InvokeMethod("Save", shortcut);
#endif
}

/// <summary>
///
/// </summary>
/// <param name="type"></param>
/// <param name="methodName"></param>
/// <param name="targetInstance"></param>
/// <param name="arguments"></param>
/// <returns></returns>
private static object InvokeSetMember(this Type type, string methodName, object targetInstance, params object[] arguments)
{
return type.InvokeMember(methodName, BindingFlags.Public | BindingFlags.Instance | BindingFlags.SetProperty, null, targetInstance, arguments);
}

/// <summary>
///
/// </summary>
/// <param name="type"></param>
/// <param name="methodName"></param>
/// <param name="targetInstance"></param>
/// <param name="arguments"></param>
/// <returns></returns>
private static object InvokeMethod(this Type type, string methodName, object targetInstance, params object[] arguments)
{
return type.InvokeMember(methodName, BindingFlags.Public | BindingFlags.Instance | BindingFlags.InvokeMethod, null, targetInstance, arguments);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Skylark.Wing.Helper
/// <summary>
///
/// </summary>
public static class Shortcut
public static class ShortcutDefault
{
/// <summary>
///
Expand Down
57 changes: 57 additions & 0 deletions src/Skylark.Wing/Helper/ShortcutRuntime.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#if NET6_0_OR_GREATER

using IWshRuntimeLibrary;
using System.IO;
using SWNM = Skylark.Wing.Native.Methods;

namespace Skylark.Wing.Helper
{
/// <summary>
///
/// </summary>
public static class ShortcutRuntime
{
/// <summary>
/// Creates a shortcut at a specified location with specified parameters.
/// </summary>
/// <param name="shortcutLocation">The location where the shortcut will be created (e.g., "C:\\SomeFolder").</param>
/// <param name="shortcutName">The name of the shortcut (e.g., "Notepad.lnk").</param>
/// <param name="description">The description for the shortcut.</param>
/// <param name="hotkey">The hotkey for the shortcut (e.g., "Ctrl+Shift+N").</param>
/// <param name="targetPath">The path to the executable file that the shortcut points to.</param>
/// <param name="iconLocation">The location of the icon to be used for the shortcut.</param>
/// <param name="workingDirectory">The working directory for the shortcut.</param>
/// <param name="arguments">Arguments to be passed to the target application.</param>
/// <param name="windowStyle">The window style for the shortcut.</param>
public static void Create(string shortcutLocation, string shortcutName, string description, string hotkey, string targetPath, string iconLocation = null, string workingDirectory = null, string arguments = null, SWNM.WindowStyle windowStyle = SWNM.WindowStyle.Normal)
{
WshShell shell = new();
string shortcutAddress = Path.Combine(shortcutLocation, shortcutName);
IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(shortcutAddress);

shortcut.Description = description;
shortcut.Hotkey = hotkey;
shortcut.TargetPath = targetPath;

if (iconLocation != null)
{
shortcut.IconLocation = iconLocation;
}

if (workingDirectory != null)
{
shortcut.WorkingDirectory = workingDirectory;
}

if (arguments != null)
{
shortcut.Arguments = arguments;
}

shortcut.WindowStyle = (int)windowStyle;
shortcut.Save();
}
}
}

#endif
12 changes: 11 additions & 1 deletion src/Skylark.Wing/Manage/Internal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@ internal static class Internal
/// </summary>
public const int WS_EX_TOOLWINDOW = 0x00000080;

/// <summary>
/// Default shortcut extension
/// </summary>
public const string DEFAULT_SHORTCUT_EXTENSION = ".lnk";

/// <summary>
///
/// </summary>
public const string WSCRIPT_SHELL_NAME = "WScript.Shell";

/// <summary>
///
/// </summary>
Expand All @@ -30,7 +40,7 @@ internal static class Internal
/// <summary>
///
/// </summary>
public static Type M_TYPE => Type.GetTypeFromProgID("WScript.Shell");
public static Type M_TYPE => Type.GetTypeFromProgID(WSCRIPT_SHELL_NAME);

/// <summary>
///
Expand Down
39 changes: 39 additions & 0 deletions src/Skylark.Wing/Native/Methods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,37 @@ public enum RestartFlags
[DllImport("user32.dll")]
public static extern bool GetLastInputInfo(ref LASTINPUTINFO plii);

/// <summary>
/// windows styles
/// </summary>
public enum ShortcutWindowStyles
{
/// <summary>
/// Hide
/// </summary>
WshHide = 0,
/// <summary>
/// NormalFocus
/// </summary>
WshNormalFocus = 1,
/// <summary>
/// MinimizedFocus
/// </summary>
WshMinimizedFocus = 2,
/// <summary>
/// MaximizedFocus
/// </summary>
WshMaximizedFocus = 3,
/// <summary>
/// NormalNoFocus
/// </summary>
WshNormalNoFocus = 4,
/// <summary>
/// MinimizedNoFocus
/// </summary>
WshMinimizedNoFocus = 6,
}

/// <summary>
/// Holds information, whether the Windows is a server, workstation or domain controller.
/// </summary>
Expand Down Expand Up @@ -1745,6 +1776,14 @@ public enum GWL
GWL_ID = -12
}

public enum WindowStyle
{
Hidden = 0,
Normal = 1,
Maximized = 3,
Minimized = 7
}

public abstract class WindowStyles
{
public const uint WS_OVERLAPPED = 0x00000000;
Expand Down
2 changes: 1 addition & 1 deletion src/Skylark.Wing/Skylark.Wing.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
// Website: www.Vegalya.com
// Created: 17.Jun.2023
// Changed: 07.Aug.2024
// Version: 3.1.6.2
// Version: 3.1.6.4
//
// |---------DO-NOT-REMOVE---------|

Expand Down
11 changes: 10 additions & 1 deletion src/Skylark.Wing/Skylark.Wing.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

<PropertyGroup>
<ApplicationIcon>Resources\Skylark.Wing.ico</ApplicationIcon>
<Version>3.1.6.2</Version>
<Version>3.1.6.4</Version>
<AssemblyVersion>$(Version)</AssemblyVersion>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Title>Skylark.Wing</Title>
Expand Down Expand Up @@ -65,6 +65,15 @@
</ItemGroup>

<ItemGroup Condition="!$(TargetFramework.StartsWith('net48'))">
<COMReference Include="IWshRuntimeLibrary">
<WrapperTool>tlbimp</WrapperTool>
<VersionMinor>0</VersionMinor>
<VersionMajor>1</VersionMajor>
<Guid>f935dc20-1cf0-11d0-adb9-00c04fd58a0b</Guid>
<Lcid>0</Lcid>
<Isolated>false</Isolated>
<EmbedInteropTypes>true</EmbedInteropTypes>
</COMReference>
<PackageReference Condition="$(TargetFramework.StartsWith('net6'))" Include="System.Management" Version="6.0.2" />
<PackageReference Condition="$(TargetFramework.StartsWith('net7'))" Include="System.Management" Version="7.0.2" />
<PackageReference Condition="$(TargetFramework.StartsWith('net8'))" Include="System.Management" Version="8.0.0" />
Expand Down

0 comments on commit 04d01ab

Please sign in to comment.