在GNU/Linux上生成lnk文件以传输到Windows [英] Produce lnk file on GNU/Linux to transfer to windows

查看:75
本文介绍了在GNU/Linux上生成lnk文件以传输到Windows的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了避免对"XY问题"的误解,我将首先说明我的目标.我希望能够生成一个文件(在 Linux 上),当下载到 Windows 机器并双击时,将打开一个(ms office,但它无关紧要 IIUC)具有已知路径的文件(在本地 Windows 上)机器)由服务器预设.

I'll explain my goal first to avoid "XY Problem" misunderstandings. I want to be able to produce a file (on Linux) that, when downloaded to a Windows machine and double clicked, will open a (ms office, but it shouldn't matter IIUC) file with a known path (on the local windows machine) preset by the server.

如果我要在lnk文件中包含要打开的文件的路径(以及有关存储在其中的卷的其他详细信息,都不包含其他信息),似乎我想做的事情是可能的.我首先检查了 lnk 文件格式规范,试图在 python 脚本中生成文件,但这不是我的小菜一碟(我以前从未编写过处理二进制文件的代码).此外,我想抛弃该格式的许多功能,但我仍然必须查看其标志/字段/长度/终止等.

It seems what I'm trying to do is possible if I include the path of the file I want to open (and neither of the other details regarding the volume it's stored in etc) in the lnk file. I first checked the lnk file format specs trying to generate the file in a python script but that's not my piece of cake (I never wrote code to handle binary files before). Besides, there are many features of that format that I want to ditch, but I still have to grok their flags/fields/lenghts/termination etc.

然后我偶然发现了winedump实用程序以及WINE在〜/.wine文件夹中创建的一些.lnk文件.WINE代码库中必须有一些能够生成lnk文件的功能,但我找不到它.它在哪里?是像win的mklink这样的命令形式吗?如果不是(我想这是某个函数,如果不是),您可以将其指向我,以便我编写包装器命令吗?

Then I stumbled upon the winedump utility and some .lnk files created by WINE in my ~/.wine folder. There must be some function in the WINE codebase that is able to generate a lnk file, but I couldn't find it. Where is it? Is it in the form of a command like win's mklink? If not (and I guess it's a function somewhere if it's not) can you point it to me so that I can write a wrapper command?

或者有没有可以编写lnk文件并可以在GNU/Linux上运行的库?

Or is there any library that can write lnk files and run on GNU/Linux?

推荐答案

这些未经我测试,但是我确实在

These are untested by me but I did find 2 tools on this site which does describe the 2 tools as follows:

此应用程序允许您在不需要Windows操作系统的情况下创建Windows快捷方式文件(扩展名为.LNK).

This application allow you to create Windows Shortcut files (extension .LNK) without needing a Windows OS.

第一个应用程序是 C应用程序,第二个应用程序是Bash shell脚本.这是Shell脚本, mslink.sh :

The first app is a C app, the second is a Bash shell script. Here's the Shell script, mslink.sh:

#!/bin/bash

#############################################################################################
# mslink.sh v1.0
#############################################################################################
# Ce script permet de créer un Raccourci Windows (Fichier .LNK)
# Script créé en se basant sur la doc 
#   http://msdn.microsoft.com/en-us/library/dd871305.aspx
#############################################################################################

OPTIONS=$(getopt -q -n ${0} -o hl:o:p -l help,lnk-target:,output-file:,printer-link -- "$@")

eval set -- ${OPTIONS}

IS_PRINTER_LNK=0
while true; do
  case "$1" in
    -h|--help) HELP=1 ;;
    -l|--lnk-target) LNK_TARGET="$2" ; shift ;;
    -o|--output-file) OUTPUT_FILE="$2" ; shift ;;
    -p|--printer-link) IS_PRINTER_LNK=1 ;;
    --)        shift ; break ;;
    *)         echo "Option inconnue : $1" ; exit 1 ;;
  esac
  shift
done

if [ $# -ne 0 ]; then
  echo "Option(s) inconnue(s) : $@"
  exit 1
fi

[ ${#LNK_TARGET} -eq 0 ] || [ ${#OUTPUT_FILE} -eq 0 ] && echo "
Usage :
 ${0} -l cible_du_fichier_lnk -o mon_fichier.lnk [-p]

Options :
 -l, --lnk-target               Précise la cible du raccourci
 -o, --output-file              Enregistre le raccourci dans un fichier
 -p, --printer-link             Génère un raccourci de type imprimante réseau
" && exit 1

#############################################################################################
# Fonctions
#############################################################################################

function ascii2hex() {
    echo $(echo -n ${1} | hexdump -v -e '/1 " x%02x"'|sed s/\ /\\\\/g)
}

function gen_IDLIST() {
        ITEM_SIZE=$(printf '%04x' $((${#1}/4+2)))
        echo '\x'${ITEM_SIZE:2:2}'\x'${ITEM_SIZE:0:2}${1}
}

function convert_CLSID_to_DATA() {
    echo -n ${1:6:2}${1:4:2}${1:2:2}${1:0:2}${1:11:2}${1:9:2}${1:16:2}${1:14:2}${1:19:4}${1:24:12}|sed s/"\([A-Fa-f0-9][A-Fa-f0-9]\)"/\\\\x\\1/g
}

#############################################################################################
# Variables issues de la documentation officielle de Microsoft
#############################################################################################

HeaderSize='\x4c\x00\x00\x00'                           # HeaderSize
LinkCLSID=$(convert_CLSID_to_DATA "00021401-0000-0000-c000-000000000046")   # LinkCLSID
LinkFlags='\x01\x01\x00\x00'                            # HasLinkTargetIDList ForceNoLinkInfo 

FileAttributes_Directory='\x10\x00\x00\x00'                 # FILE_ATTRIBUTE_DIRECTORY
FileAttributes_File='\x20\x00\x00\x00'                      # FILE_ATTRIBUTE_ARCHIVE

CreationTime='\x00\x00\x00\x00\x00\x00\x00\x00'
AccessTime='\x00\x00\x00\x00\x00\x00\x00\x00'
WriteTime='\x00\x00\x00\x00\x00\x00\x00\x00'

FileSize='\x00\x00\x00\x00'
IconIndex='\x00\x00\x00\x00'
ShowCommand='\x01\x00\x00\x00'                          # SW_SHOWNORMAL
Hotkey='\x00\x00'                               # No Hotkey
Reserved='\x00\x00'                             # Valeur non modifiable
Reserved2='\x00\x00\x00\x00'                            # Valeur non modifiable
Reserved3='\x00\x00\x00\x00'                            # Valeur non modifiable
TerminalID='\x00\x00'                               # Valeur non modifiable

CLSID_Computer="20d04fe0-3aea-1069-a2d8-08002b30309d"               # Poste de travail
CLSID_Network="208d2c60-3aea-1069-a2d7-08002b30309d"                # Favoris réseau

#############################################################################################
# Constantes trouvées à partir de l'analyse de fichiers lnk
#############################################################################################

PREFIX_LOCAL_ROOT='\x2f'                            # Disque local
PREFIX_FOLDER='\x31\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'        # Dossier de fichiers
PREFIX_FILE='\x32\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'          # Fichier
PREFIX_NETWORK_ROOT='\xc3\x01\x81'                      # Racine de serveur de fichiers réseau
PREFIX_NETWORK_PRINTER='\xc3\x02\xc1'                       # Imprimante réseau

END_OF_STRING='\x00'

#############################################################################################

# On retire l'anti-slash final s'il y en a un
LNK_TARGET=${LNK_TARGET%\\}

# On sépare le chemin racine du lien de la cible finale
# On distingue aussi si le lien est de type local ou réseau
# On définie la valeur Item_Data suivant le cas d'un lien réseau ou local

IS_ROOT_LNK=0
IS_NETWORK_LNK=0

if [[ ${LNK_TARGET} == \\\\* ]]; then
    IS_NETWORK_LNK=1
    PREFIX_ROOT=${PREFIX_NETWORK_ROOT}
    Item_Data='\x1f\x58'$(convert_CLSID_to_DATA ${CLSID_Network})

        TARGET_ROOT=${LNK_TARGET%\\*}
        if [[ ${LNK_TARGET} == \\\\*\\* ]]; then
                TARGET_LEAF=${LNK_TARGET##*\\}
        fi
        if [ ${TARGET_ROOT} == \\ ]; then
                TARGET_ROOT=${LNK_TARGET}
        fi
else
    PREFIX_ROOT=${PREFIX_LOCAL_ROOT}
    Item_Data='\x1f\x50'$(convert_CLSID_to_DATA ${CLSID_Computer})

    TARGET_ROOT=${LNK_TARGET%%\\*}
        if [[ ${LNK_TARGET} == *\\* ]]; then
        TARGET_LEAF=${LNK_TARGET#*\\}
        fi
    [[ ! ${TARGET_ROOT} == *\\ ]] && TARGET_ROOT=${TARGET_ROOT}'\'
fi

if [ ${IS_PRINTER_LNK} -eq 1 ]; then
    PREFIX_ROOT=${PREFIX_NETWORK_PRINTER}
    TARGET_ROOT=${LNK_TARGET}
    IS_ROOT_LNK=1
fi

[ ${#TARGET_LEAF} -eq 0 ] && IS_ROOT_LNK=1

#############################################################################################

# On sélectionne le préfixe qui sera utilisé pour afficher l'icône du raccourci

if [[ ${TARGET_LEAF} == *.??? ]]; then
    PREFIX_OF_TARGET=${PREFIX_FILE}
    TYPE_TARGET="fichier"
    FileAttributes=${FileAttributes_File}
else
    PREFIX_OF_TARGET=${PREFIX_FOLDER}
    TYPE_TARGET="dossier"
    FileAttributes=${FileAttributes_Directory}
fi

# On convertit les valeurs des cibles en binaire
TARGET_ROOT=$(ascii2hex ${TARGET_ROOT})
TARGET_ROOT=${TARGET_ROOT}$(for i in `seq 1 21`;do echo -n '\x00';done) # Nécessaire à partir de Vista et supérieur sinon le lien est considéré comme vide (je n'ai trouvé nul part d'informations à ce sujet)

TARGET_LEAF=$(ascii2hex ${TARGET_LEAF})

# On crée l'IDLIST qui représente le cœur du fichier LNK

if [ ${IS_ROOT_LNK} -eq 1 ];then
    IDLIST_ITEMS=$(gen_IDLIST ${Item_Data})$(gen_IDLIST ${PREFIX_ROOT}${TARGET_ROOT}${END_OF_STRING})
else
    IDLIST_ITEMS=$(gen_IDLIST ${Item_Data})$(gen_IDLIST ${PREFIX_ROOT}${TARGET_ROOT}${END_OF_STRING})$(gen_IDLIST ${PREFIX_OF_TARGET}${TARGET_LEAF}${END_OF_STRING})
fi

IDLIST=$(gen_IDLIST ${IDLIST_ITEMS})

#############################################################################################

if [ ${IS_NETWORK_LNK} -eq 1 ]; then
    TYPE_LNK="réseau"
    if [ ${IS_PRINTER_LNK} -eq 1 ]; then
        TYPE_TARGET="imprimante"
    fi
else
    TYPE_LNK="local"
fi

echo "Création d'un raccourci de type \""${TYPE_TARGET}" "${TYPE_LNK}"\" avec pour cible "${LNK_TARGET} 1>&2

echo -ne ${HeaderSize}${LinkCLSID}${LinkFlags}${FileAttributes}${CreationTime}${AccessTime}${WriteTime}${FileSize}${IconIndex}${ShowCommand}${Hotkey}${Reserved}${Reserved2}${Reserved3}${IDLIST}${TerminalID} > "${OUTPUT_FILE}"

这篇关于在GNU/Linux上生成lnk文件以传输到Windows的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆