0

How can I get a path to file being installed from a WiX custom action?

I'm creating a WIX Extension, which has a custom element which can be nested under <File> component like this:

<Component Id="FooComponent">
   <File Id="filekey" Name="foo.txt">
       <myextension:Stuff />
   </File>
</Component>

The extension has its custom table which has a foreign key columns pointing to "Component" and "File" tables, and it is executed when a file's component is being installed/uninstalled (like the built-in IIS extension or SQL extension for example)

What I want to achieve is, in my deferred (sheduled) custom action, figure out the target path of the file the extension isbound to. I.e. basically in the differed custom action, I want to get value of [!filekey] (in terms of MSI formatted string). How can I go about that?

I have found a somewhat similar topic here

One of the solutions suggested was to use MsiFormatRecord from a custom action and pass that [#filekey] to that function. It resolves properly then.

I've found examples of using this approach in WiX sources, in gaming extension and NetFX extensions; they use code like this:

    StrAllocFormatted(&pwzFormattedFile, L"[#%s]", pwzFileId);
    WcaGetFormattedString(pwzFormattedFile, &pwzGamePath);

Here WcaGetFormattedString is basically a wrapper for MsiFormatRecord

Still unanswered, is this a right approach to the issue?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Nikolay
  • 10,752
  • 2
  • 23
  • 51

1 Answers1

0

Basically you pass that [!filekey] to the deferred custom action in that format, using the CustomActionData indirect way of doing that. Then in your custom action you get the property value [CustomActionData] . This might help:

How to pass CustomActionData to a CustomAction using WiX?

To spell it out, if your type 51 custom action that is used to prepare CustomActionData has a Target of [!filekey] then when you retrieve the value of the CustomActionData property inside your custom action then it will contain the full path to the file, the entire path and filename. This demonstrably works, although it wouldn't be practical if you were doing this for many files.

Community
  • 1
  • 1
PhilDW
  • 20,260
  • 1
  • 18
  • 28
  • I mean, I need this to be expanded into real file path on the target system. That is, given a file ID (filekey) I need to know where this file will be installed (physical file path). Or you men that formatted strings are auto-expanded in Customactiondata? – Nikolay Dec 18 '15 at 19:21
  • To clarify - the question is not how to pass strings from immediate CA or user interface to a deffered CA... What I meant was: given a file id (filekey) I need to know where this file will be/was installed.. I.e. the directory path of the component the file belongs to. I need get this info from a custom action.. I hope this clarifies the question... I mean, user can select target paths for features, right, and component in those features are installed to those paths. How do I find those, given a file key? – Nikolay Dec 18 '15 at 21:29
  • I can't tell whether I'm not explaining it properly or you don't understand, so I added an explicit example that I just tested and it works. – PhilDW Dec 19 '15 at 19:48
  • Probably I put the question somewhat unclear... Anyways, now I'm happy after looking at the WIX gaming and NetFX extensions source code. It seems to have solved the issue. Thank you for your time. Here is the link to the code fragment that helped: https://github.com/wixtoolset/wix3/blob/develop/src/ext/GamingExtension/ca/gaming.cpp#L688 – Nikolay Dec 20 '15 at 21:20