50% OFF!!!

Booking.com

Sunday, May 22, 2011

c# WinForm | Check form window size and location

Hello,


I wanted to open a new window form, exactly near another form. (pinning one form to another).
Because of the border difference among Windows XP, Vista and Windows7 (win7), it didn't work easily.

So to determine windows size under win7/vista, I had to check if Windows Aero. (checking made by short&easy code)

Now, after checking this, I had to also support winXP (which does not have AERO technology).

For finding the left and right window border size (width) I used the SystemInformation Class which provides information about the current system environment.

Note:
I recommend to check if it works for other FormBorderStyle.

The code:
internal static bool IsAeroEnabled()
{
    bool isEnabled = false;
    if (Environment.OSVersion.Version.Major >= 6)
    {
        try
        {
            DwmIsCompositionEnabled(out isEnabled);
        }
        catch (Exception ex)
        {
            Logger.WriteLog(ex);
        }
    }
    return isEnabled;
}
[DllImport("dwmapi.dll")]
internal static extern int DwmIsCompositionEnabled(out bool enabled);

Best regard,
MDB Blog

Monday, May 2, 2011

Check HOSTS file - C# WinForms

Hello,

In today's post i'm giving a good (and simple) anti-hacking tool which allow your application to CHECK for any changes in the Windows HOSTS file.

So, the operation is simple:
Just read the hosts file content, and validate that your site DNS is not included in the list.
[basiclly, check if hosts file contains your dns name]
if so, the user probably is trying to bypass your server so you can stop him from doing so.

The code:
static bool IsDnsByPassed(string p_dnsName)
{
    string path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System), @"drivers\etc\hosts");
    string hostsText = File.ReadAllText(path);
    return hostsText.ToLower().Contains(p_dnsName.ToLower());
}

Note:
Although reading the HOSTS file is allowed to all user, do not try to edit the HOSTS file, because on Vista & Windows7 this operation is allowed only for administrators. if your application is NOT running as Admin, then you won't have permission for this (and an Exception will be thrown).

Best regard,
MDB-Blog