blez's : blog

code / design / music and more

Leave a comment

How to create sandboxed plugins with .NET?

I’m currently writing an app that has a plugin architecture in it. Long ago when I searched about making sandboxed plugins, it was quite hard to find something relevant. So I’ve wrote an example of a plugin that can be optionally restricted from everything (file access, web access and so on).

Continue reading

1 Comment

How to create transparent labels in WinForms

If you come from VB6, you’ll notice that WinForms doesn’t have transparent labels. WHICH SUCKS if you really need them. There are workarounds like using WPF instead or drawing the ‘labels’ with DrawText. But I recently found a better way – using the LinkLabel control. It supports transparency just fine. The only thing you have to do is to disable all its link properties.
The code below shows how:

// if your LinkLabel is called linkLabel

linkLabel.ForeColor = this.ForeColor;
linkLabel.LinkColor = this.ForeColor;
linkLabel.VisitedLinkColor = this.ForeColor;
linkLabel.ActiveLinkColor = this.ForeColor;
linkLabel.DisabledLinkColor = this.ForeColor;
linkLabel.LinkArea = new LinkArea(0, 0);
linkLabel.LinkBehavior = LinkBehavior.NeverUnderline;
linkLabel.Cursor = Cursors.Arrow;
linkLabel.BackColor = Color.Transparent;

Leave a comment

Fixing the Apple keyboards layout under Windows

This lovely piece of aluminum needs special configuration under Windows. The alt and win keys are reversed and it doesn’t have PrintScreen button or media buttons.
Here’s a script I used to remap the layout on Windows for this lovely keyboard. That’s my custom layout, you can remap it whatever you like.
It uses AutoHotkey. Just save it as .ahk file and run it. If you like it you can compile it with the AutoHotKey compiler.

Continue reading

1 Comment

How to read file while it’s in use?

Let’s say you have to use a file which another process locked. You’ll get an error trying to open it. Of course there’s a workaround.
Just duplicate the handle for the current process:

// NOTE:
// PID is the process's ID who opened the file (ex. firefox -> 1234)
// hFile is the original file's handle (you can get it using NtQuerySystemInformation)

HANDLE hFileNew;
HANDLE hSourceProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, PID);
DuplicateHandle(hSourceProcess, hFile, GetCurrentProcess(), &hFileNew, DUPLICATE_SAME_ACCESS, TRUE, DUPLICATE_SAME_ACCESS);

// Now you have a new file handle in hFileNew, which you can use with ReadFile or similar
// hFileNew's file pointer will have the last position from PID.
// If you change it, it will change for PID too (which can cause a crash or messed up stuff).
// But if you do it fast or pause the process, everything should be fine.

Related: Enumerating opened handles from a process