2016-01-06 27 views
3

我試圖以編程方式查找爲給定的MSI lnk文件(廣告快捷方式)運行的exe文件。我使用了與Is there a way to resolve a .lnk target that works for links that end up in c:\windows\installer?的答案類似的方法。這種方法適用於大多數MSI lnk文件。不幸的是,有一小部分lnk文件運行正常,但MsiGetShortcutTarget不返回組件ID。所以對MsiGetComponentPath的後續調用返回InvalidArg。MsiGetShortcutTarget MSI LNK文件沒有返回組件ID

下面是我使用的代碼(從here拍攝):

public const int MaxFeatureLength = 38; 
public const int MaxGuidLength = 38; 
public const int MaxPathLength = 1024; 

public static string ParseShortcut(string shortcutFilename) 
{ 
    StringBuilder product = new StringBuilder(MaxGuidLength + 1); 
    StringBuilder feature = new StringBuilder(MaxFeatureLength + 1); 
    StringBuilder component = new StringBuilder(MaxGuidLength + 1); 

    var returnValue = MsiGetShortcutTarget(shortcutFilename, product, feature, component); 

    if (returnValue != 0) 
    { 
     return null; 
    } 

    int pathLength = MaxPathLength; 
    StringBuilder path = new StringBuilder(pathLength); 

    InstallState installState = MsiGetComponentPath(product.ToString(), component.ToString(), path, ref pathLength); 
    if (installState == InstallState.Local) 
    { 
     return path.ToString(); 
    } 
    else 
    { 
     return null; 
    } 
} 

我的機器上的一個例子是C:\ ProgramData \微軟\的Windows \開始菜單\程序\的Microsoft Office 2013 \辦公室2013工具\辦公室2013語言Preferences.lnk

產品編號:{} 91150000-0011-0000-0000-0000000FF1CE功能 ID:SetLanguageFiles

相信接口的IShellLink不能用於返回可運行Ë xe for MSI Advertised Shortcuts,我嘗試這樣做已經返回包含圖標資源的exe文件的路徑。

顯然,操作系統可以找到相應的exe文件(在本例中爲C:\ Program Files(x86)\ Microsoft Office \ Office15 \ SETLANG.EXE)。

如何獲得哪個exe文件與此lnk文件使用代碼相關聯?

+0

可以肯定的是,它可能有助於發佈您的代碼。 – PhilDW

+0

添加代碼,謝謝。 – fractor

+0

看看[this](http://www.symantec.com/connect/articles/working-darwin-descriptors)我可以看到爲什麼lnk文件沒有返回組件ID。這是因爲其中的達爾文描述符只包含產品代碼。所以我想這個問題歸結爲:給定產品代碼(和功能),我如何獲得(單一)組件ID? – fractor

回答

0

如果沒有從MsiGetShortcutTarget返回組件ID,則可以從MSI數據庫中獲取組件ID。執行此操作的一種方法是使用WiX DTF,如下所示:https://stackoverflow.com/a/34680682/3051702

然後可以在對MsiGetComponentPath的調用中使用返回的組件ID。或者,本地方法可以直接使用。