Use clipboard (copy/paste) in C# console application

net_logo.jpgThis took me few minutes to figure out and was quite annoying. First you must add a reference to System.Windows.Forms in your application. Go to Project -> Add reference, select System.Windows.Forms from .NET tab in the window that just opened.  You must avoid the ThreadStateException by applying the STAThread attribute to your Main() function. Then you can use the Clipboard functions without any problems.

using System;
using System.Windows.Forms;
class Program {
    static void Main(string[] args) {
         Clipboard.SetText("this is in clipboard now");

Ho-To: Install Alchemy

For those of you who tried but didn't succed, I found some straigthforward instructions for this.

Setup Hell:

Install cygwin to "c:\cygwin" - make sure to check boxes to install Perl, "Archive" (ZIP), and Devel/gcc:g++ ( presumably) (is Perl necessary?)
Copy the alchemy directory to: c:\cygwin\home\Lee
Copy the flex sdk bin dir from c:\program files\etc to "c:\flex" (to avoid paths with spaces in it!)
Run the alchemy config script once:

   run cygwin
   cd alchemy

Edit the textfile "C:\cygwin\home\Lee\alchemy\alchemy-setup" and uncomment and edit line 22 to: add "export ADL=c:\flex\adl.exe"

Edit .bashrc (in C:\cygwin\home\Lee)

   echo "LEE PROFILE"
   export FLEX_HOME=~/flex
   export ALCHEMY_HOME=~/alchemy
   # "This should be added before your PATH is modified" !!
   source /home/Lee/alchemy/alchemy-setup
   export PATH

Do this (just once, I think):

   cd $ALCHEMY_HOME/bin
   ln -s llvm-stub llvm-stub.exe

Compiling a SW:

CD to the directory with the source
Always do "alc-on" before compiling, cuz it doesn't work without it even though i added it to the startup (dunno)

    gcc stringecho.c -O3 -Wall -swc -o stringecho.swc
    -- should give you a swc.

Import the swc into your flex builder 4 project and make sure compiler targets SDK v4


alc-home - takes you to the Alchemy install folder.
alc-on - puts Alchemy gcc toolchain replacements at the front of your path.
alc-off - restores original path.
alc-util - shows you various Alchemy-related environment vars


which gcc - tells you which gcc it will use (should be the one in the achacks dir)
ln - links shit
rm - deletes links as well as files

Ugh: Make sure to do "alc-on" and "alchemy-setup" even though you put it in the startup script 🙁 (?)





Communicate betwen C# and an embeded Flash application

The External API allows an ActionScript developer to easily interact with the container program that is hosting Flash Player 8 and vice versa. The majority of the time, this will most likely be a Web browser, but this does not always have to be the case.

As many C# developers know, it is easy to house an ActiveX control (the IE version of Flash Player) in a .NET Windows application. This means we can now load an SWF in our Windows application and easily send data back and forth. Keep in mind that the keyword in this statement is "easily;" although possible before, it was not nearly as simple as the External API makes it now!

C# to ActionScript Communication

As I said before, communication between Flash Player and its container has been made extremely easy. The new class that makes this process so easy is the ExternalInterface. We will begin in the ActionScript. First, we need to import this new class so we can use it (as2 only, in as3 it will work without the import):

import flash.external.ExternalInterface;

Next, we have to register any function we want to make available externally:

ExternalInterface.addCallback("addText", addText);

Basically, the code above will allow us to call the addText function (which I will show in a minute) from the C# application.
The addText function is as below. Basically, it takes a string input and appends it to a text box

function addText(val:String):void
	inTxt.appendText(val + "\n"); // append text recieved from c#

That's it from the ActionScript side. Now all we need to do is call the function from C#. First, I add an instance of the Flash Player ActiveX control to my form and load the SWF we created in the form's constructor:

private AxShockwaveFlash player;
public DemoForm ()
     player.LoadMovie(0, Application.StartupPath + "\\EITest.swf");

Next, all we have to do is call the externalized method when desired. In my case, it is in response to the user clicking the send button:

private void sendBtn_Click(object sender, EventArgs e)
    player.CallFunction("" + outTxt.Text + "");

ActionScript to C# Communication

Again, you will need to use the ExternalInterface in the ActionScript:

function send(evt : Event):void
{"sendText", outTxt.text); // function to call and it's parameters
	outTxt.text = ""; // reset text box

As you can see, I am calling a method sendText and passing the input string as a parameter. Now to receive the message in C#, we first have to subscribe to the FlashCall event. You can do this in the constructor or from the activex properties panel on events tab.

Now the call made in ActionScript will be received in the request property of the event argument. For my particular call, the XML will look like this:

<invoke name="sendText" returntype="xml">
<string>some text message here</string>

So now all we have to do is parse the XML in the event handler and invoke the C# function locally:

private void player_FlashCall(object sender, _IShockwaveFlashEvents_FlashCallEvent e)
    // message is in xml format so we need to parse it
    XmlDocument document = new XmlDocument();
    // get attributes to see which command flash is trying to call
    XmlAttributeCollection attributes = document.FirstChild.Attributes;
    String command = attributes.Item(0).InnerText;
    // get parameters
    XmlNodeList list = document.GetElementsByTagName("arguments");
    // Interpret command
    switch (command)
        case "sendText" : resultTxt.Text = list[0].InnerText; break;
        case "Some_Other_Command" : break;


c# to flash activex demo

I have made the simple example discussed available here.