1
votes

I'm learning the Wix to build a installer.

in a custom Dialog, I have a Control, which type is Text, and I have a Button. I want to click the Button to copy the Text into the Clipboard.

Here are the codes. First is the Controls.

<Control Id="AboutUsInfo" Type="Text" Property="AboutUsText"
                     X="150" Y="20" Width="140" Height="150">
                <Text SourceFile="sample\info2.txt" />
            </Control>
<Control Id="CopyAboutUsButton" Type="PushButton" Text="Copy to the clipboard"
                     X="100" Y="180" Width="80" Height="17">
                <Publish Event="DoAction" Value="CopyAboutUsAction"></Publish>
            </Control>

<Binary Id="Customactions" SourceFile="sample\CustomAction1.CA.dll"></Binary>

Here is the CustomAction.

    <CustomAction Id="CopyAboutUsAction" BinaryKey="Customactions" DllEntry="CopyToClipboard" Return="ignore">
    </CustomAction>

Now the C# code

namespace CustomAction1
{
public class CustomActions
{
    [CustomAction]
    public static ActionResult CopyToClipboard(Session session)
    {
        session.Log("Begin Copy");
        String s=session["AboutUsText"];
        Clipboard.SetText("this is copy");
        return ActionResult.Success;
    }
}
}

The Problem is, each time I click the button, my installer says nothing. And nothing happens in my Clipboard. How can I make this work?

1
Most likely it can't find the DLL or can't load it. Run your installation with logging: msiexec /i package.msi /l*v package.log. You should see why the installation is aborted in the package.log file.Alexey Ivanov
That's the problem, it says NOTHING! Nothing wrong, Nothing right, just the custom action executed, and return value 1. It doesn't even have a item called "Begin Copy", which is defined in the CustomAction function. The total customaction just doesn't work. I set the return property to ignore, now it doesn't exit because of the error.cdytoby
The requirement to copy something to clipboard during the installation process by pushing a button sounds really strange... Have you considered any alternatives? Probably, it's better just using MSI properties for passing data?Yan Sklyarenko
Some users require that .msi installations run in silent mode (without GUI). They would run the .msi file with command line that would set all the required properties. Would your installation work in this case?Dialecticus
No, I want to show the "About Us" strings, and let the user copy them, and the string are provided as HTML or TXT files. As the scrollabletext Control, it can only read the RTF files, not TXTs. But use Text Control, I cannot do select the Text and ctrl+c. Does anybody have a solution? And my msi will not be run as silent mode.cdytoby

1 Answers

0
votes

I've already solved this. I need to start a new thread so that this thread is STA. And it worked. Thank you everyone who added the comments.

public class CustomActions
{
    [CustomAction]
    public static ActionResult CopyToClipboard(Session session)
    {
        ActionResult ar=ActionResult.Success;
        session.Log("Begin CopyToClipboard");
        //MessageBox.Show("Begin Copy");
        string str="abcde";
        MessageBox.Show(" "+str);
        try
        {
            Thread th=new Thread(new ParameterizedThreadStart(ClipboardThread));
            th.SetApartmentState(ApartmentState.STA);
            th.Start(str);
            th.Join();
        }
        catch(Exception ex)
        {
            MessageBox.Show(ex.Message+" \n"+ex.StackTrace);
            ar=ActionResult.Failure;
        }
        //MessageBox.Show("End Copy");
        return ar;
    }

    static void ClipboardThread(object s)
    {
        try
        {
            Clipboard.SetText(s.ToString());
        }
        catch(Exception ex)
        {
            MessageBox.Show(ex.Message+" \n"+ex.StackTrace);
        }
    }