h3mm3's blog

Stories from behind the keyboard

  • RSS
  • Twitter
Showing posts with label Windows. Show all posts
Showing posts with label Windows. Show all posts

ScriptCS-GUI has evolved a little bit. At the moment the following helpers are declared inside the static class Gui.

  • Gui.OpenFileDialog(): open a classic Open File Dialog and return the path of the selected file.
  • Gui.SaveFileDialog(): act similar to OpenFileDialog()...
  • Gui.PopUp(): open a pop-up and returns a form object. You can use its Sta.Enqueue(action) method to inject code in the form's main thread (e.g. to add controls at runtime) while working inside the console!
  • Gui.Drawing(): open a drawing form and returns the Graphics object you can use to draw on the form (the console stays responsive in this case too!)

Probably the most useful thing is that I've managed to open Windows Forms from the console while leaving the console "alive responsive" (tipically the console would (in-)act like a zombie after any Application.Run() over the form!). This means that now you can go on using the ScriptCS console and send any command to any window you created FROM the console. This was not obvious and I've got lots of ideas about how to implement ScriptCS-GUI further! - Stay tuned :)

I'm going to write more about these in my next post. For now you can just launch the interactive script "gui-demo.csx", run it and - *hopefully* - send me some feedback :)

The interactive script "gui-demo.csx" lets you play with classic Windows Forms while the console stays responsive!

Quick and Dirty: you can enable the Windows Explorer Preview Panel to show the content of various text file format (such as .cs, .inf, .xaml, and so on…). Just open REGEDIT.EXE, browse HKEY_CLASSES_ROOT down to the file extension you want to preview, and add a new string value “PerceivedType” with value “text” and you are done.

For instance, here’s how you enable the text preview handler for C# files:

image

From now on, when you select a .cs file in Windows Explorer, the Preview Panel will show its content.

image

Happy regediting!

Some days ago one trainee of mine asked me how to write an HTML link to «a file hidden inside another file». After my eyebrows jumped high over my head, he explained to me that he was thinking about a feature of the NTFS File System called Alternative Data Streams (ADS) but he couldn’t help building a working HTML code.

Meet an Alternative Data Stream

In simple terms, ADS technology lets you store multiple pieces of data (i.e. multiple files) “inside” a single file. The easiest way to try it is to open a Command Prompt and follow a couple of steps:

  1. Inside Command Prompt, type: “notepad myfile.txt” and press Return
  2. Let NotePad create the file and type something inside it. Then save and close NotePad
  3. Back to Command Prompt, type: “notepad myfile.txt:hidden.txt”
  4. Similarly to the previous steps, NotePad creates a new file. This file is an Alternate Data Stream, stored “under” myfile.txt and called hidden.txt
  5. Type something inside the NotePad window, save the file and close it.

The file you’ve just created is not visible in Explorer, but you can give a “DIR /R” inside Command Prompt.

Sysinternals has a nice utility that lets you look for (or delete) ADS’s inside a path or a volume; the utility is called “Streams” (!) and you can download it from Microsoft Technet website under Windows Sysinternals File and Disk Utilities area.

One File To Rule Them All?

When my student asked me how to link to an ADS, he had a greater vision: using a file and all its alternative data streams as a unit of deployment. IMO I don’t think it’s a good idea, since it is quite difficult to manage single streams inside a file. Moreover, lots of software behave just like ADS don’t exist at all, so you’ll probably end up losing your streams during typical website up-keeping operations (such as doing FTP or editing files with “evolved” editors).

Anyway, I tried to write some HTML anchor in order to address an ADS. Supposing the main file is called “file.html” and it contains a stream called “otherfile.html”, my first attempt was to write:

<A HREF=”file.html:otherfile.html”>Other file</A>

That link didn’t worked. In my second attempt I used URL encoding, and I wrote:

<A HREF=”file.html%3Aotherfile.html”>Other file</A>

This worked in FireFox and Chrome but was totally despised by IE9. Of course, I used a file URI:

image

Figure: Navigation back and forth using a file URI works both in Chrome and Firefox.

Then I tried to host the file in IIS (I used WebMatrix and IIS7.5 Express) and I addressed the inner stream using an HTTP URI (e.g. http://). All I got was an Error 404.0 – Not Found. Moreover, whenever I saved the outer stream (i.e. the “visible” file) with WebMatrix, I completely lost the inner streams.

Who cares about Alternative Data Streams?

If you’ve gone this far you are probably thinking that this story won’t lead you anywhere. I don’t like to lead my reader nowhere and I want to conclude di post with a positive note, that is: Windows uses alternative data streams. When? For instance, when you download a file, or when you save an e-mail  attachment. In these cases, Windows appends an ADS to the file, in order to “remember” that it comes from an untrusted zone. Actually, The ADS is called Zone.Identifier and contains 24/26 bytes;

[ZoneTransfer]
ZoneId=3

Whenever you remove the lock to the file (e.g. right click the file > Properties > Unblock), you actually delete this ADS.

Since Windows uses ADS to remember the source zone of untrusted files, you can take part to this process, for instance, tagging files as (un-)trusted during batch or administrative operations.

Happy programming!

Today a friend of mine was looking for the Windows environment variable that defines the x86 version of Program Files folder inside an x64 Windows OS. After a little search, I found that you can simply open a Command Prompt (cmd.exe) and launch the good old DOS command “SET” in order to list all the environment variables. You can even get the list of all the environment variables starting with a letter or a string (for instance “program”), typing something like

C:\> SET program

For instance, these are the environment variables defined in my system and starting with the string “program”:

ProgramData=C:\ProgramData
ProgramFiles=C:\Program Files
ProgramFiles(x86)=C:\Program Files (x86)
ProgramW6432=C:\Program Files

Of course you can execute something like

C:\> SET > environment.txt

in order to save all values in a text file called environment.txt.

By the way, a good source of information about SET and other Command Prompt’s goodies is http://SS64.com/nt.

> exit