forked from alesimula/wsa_pacman
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Option to create desktop shortcut; close on launch
- Loading branch information
Showing
5 changed files
with
127 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// ignore_for_file: non_constant_identifier_names | ||
|
||
import 'dart:ffi'; | ||
import 'package:ffi/ffi.dart'; | ||
import 'package:win32/win32.dart'; | ||
|
||
class RegistryKeyValuePair { | ||
final String key; | ||
final String value; | ||
|
||
const RegistryKeyValuePair(this.key, this.value); | ||
} | ||
|
||
class WinIO { | ||
/// Creates a Windows shortcut (.lnk); | ||
static void createShortcut(String filePath, String linkPath, {String? description, String? args, String? icon}) { | ||
final shellLink = ShellLink.createInstance(); | ||
final lpPath = filePath.toNativeUtf16(); | ||
final lpArgs = args?.toNativeUtf16(); | ||
final lpIcon = icon?.toNativeUtf16(); | ||
final lpLinkPath = "$linkPath.lnk".toNativeUtf16(); | ||
final lpDescription = description?.toNativeUtf16() ?? nullptr; | ||
final ptrIID_IPersistFile = convertToCLSID(IID_IPersistFile); | ||
final ppf = calloc<COMObject>(); | ||
|
||
try { | ||
shellLink.SetPath(lpPath); | ||
if (lpArgs != null) shellLink.SetArguments(lpArgs); | ||
if (description != null) shellLink.SetDescription(lpDescription); | ||
if (lpIcon != null) shellLink.SetIconLocation(lpIcon, 0); | ||
|
||
final hr = shellLink.QueryInterface(ptrIID_IPersistFile, ppf.cast()); | ||
if (SUCCEEDED(hr)) { | ||
final persistFile = IPersistFile(ppf); | ||
persistFile.Save(lpLinkPath, TRUE); | ||
persistFile.Release(); | ||
} | ||
shellLink.Release(); | ||
} finally { | ||
free(lpPath); | ||
if (lpArgs != null) free(lpArgs); | ||
if (lpIcon != null) free(lpIcon); | ||
free(lpLinkPath); | ||
if (lpDescription != nullptr) free(lpDescription); | ||
free(ptrIID_IPersistFile); | ||
free(ppf); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// Copyright (c) 2020, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
// Demonstrates usage of various shell APIs to retrieve known folder locations | ||
|
||
import 'dart:ffi'; | ||
import 'package:ffi/ffi.dart'; | ||
import 'package:win32/win32.dart'; | ||
|
||
class WinPath { | ||
// Get the path of the temporary directory (typically %TEMP%) | ||
static late String temp = (){ | ||
final buffer = wsalloc(MAX_PATH + 1); | ||
final length = GetTempPath(MAX_PATH, buffer); | ||
|
||
try { | ||
if (length == 0) { | ||
final error = GetLastError(); | ||
throw WindowsException(error); | ||
} else { | ||
var path = buffer.toDartString(); | ||
|
||
// GetTempPath adds a trailing backslash, but SHGetKnownFolderPath does not. | ||
// Strip off trailing backslash for consistency with other methods here. | ||
if (path.endsWith('\\')) { | ||
path = path.substring(0, path.length - 1); | ||
} | ||
return path; | ||
} | ||
} finally { | ||
free(buffer); | ||
} | ||
}(); | ||
|
||
/// Get the desktop path | ||
static late String desktop = (){ | ||
final appsFolder = GUIDFromString(FOLDERID_Desktop); | ||
final ppszPath = calloc<PWSTR>(); | ||
|
||
try { | ||
final hr = | ||
SHGetKnownFolderPath(appsFolder, KF_FLAG_DEFAULT, NULL, ppszPath); | ||
|
||
if (FAILED(hr)) { | ||
throw WindowsException(hr); | ||
} | ||
|
||
final path = ppszPath.value.toDartString(); | ||
return path; | ||
} finally { | ||
free(appsFolder); | ||
free(ppszPath); | ||
} | ||
}(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters