Thursday, 1 October 2015

Elevate your .Net Application without a manifest

Administrator privileges are used quite frequently for tasks such as allowing your program to make changes to the file system. One way to go at it would be by adding a manifest when installing the program. But if you're a programmer, there's always a better way to do it.

How does the Windows explorer do it?
The verb 'RunAs' is used when launching a process.

How does this work?
On Vista and above, if UAC (User Account Control) is set to active, it would show a dialog box asking for administrator privileges. If you've disabled it, the application should elevate itself automatically.
Note: On Windows XP it would display a 'Run as' dialog box regardless.

How does this help us?
We can use the same method when coding a .Net application.

The Algorithm

- Application starts
- Checks if it has elevated access
- If true, continue to execute program.
- If false, create a new process with the 'RunAs' verb, forward the arguments, and exit itself.
- The new process starts and asks for Administrator privileges using the UAC dialog.

The Code(C++)

using namespace System::Security::Principal;

int main(array<system::string^> ^args)
{
    // Check Elevation
    bool r = ((gcnew WindowsPrincipal(
        WindowsIdentity::GetCurrent()))
        ->IsInRole(WindowsBuiltInRole::Administrator));
    // If Not Elevated
    if(!r)
    {
        // Prep Elevated Process Data
        ProcessStartInfo^ p;
        // Assign the Application's path to the process
        p = gcnew ProcessStartInfo(Application::ExecutablePath);
        p->Verb = "runas";
        p->UseShellExecute = true;
        //Prep to Forward Arguments
        p->Arguments = String::Join(" ",
            System::Environment::GetCommandLineArgs())
            ->Replace(Application::ExecutablePath," ")->Trim();
        // Execute Process, and Clean Up
        Process::Start(p);
        delete p;
        //Exit Parent Process
        return 0;
    }
    // Else ( If Elevated )
    // Do some work
    return 0;
}

Wednesday, 2 September 2015

.Net Application with "Start with Windows" option




Adding a Start-With-Windows option can become quite a tedious task for those new to developing for windows. The solution rather, is very simple. We use a RegistryKey object (part of Microsoft.Win32 namespace) to add an entry to the Windows Registry for startup. Then we connect it to a check-box using an event trigger.

1. The program checks the state of startup at application-load.
2. The check-box updates the start-up state as you check or un-check it.

Code written in C++, but you can easily convert it.

Implementation (Application Load Event):

void Load_Event(){
    this->BootCheckBox->Checked = this->BootRun;
}

Implementation (Checkbox Check-Changed Event):

System::Void BootCheckBox_CheckedChanged(System::Object^  sender, System::EventArgs^  e) {
    this->BootRun = this->BootCheckBox->Checked;
}

Definition:

property bool BootRun{
    bool get(void){
        bool r = false;
        if( Registry::CurrentUser->OpenSubKey
            ("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true)
            ->GetValue("CSX_TrayCut") ) r = true;
        return r;
    }
    void set(bool i){
        if( i ) Registry::CurrentUser->OpenSubKey
            ("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true)
            ->SetValue("CSX_TrayCut",
            Application::ExecutablePath->ToString());
        else Registry::CurrentUser->OpenSubKey
            ("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true)
            ->DeleteValue("CSX_TrayCut", false );
    }
}


Thursday, 27 August 2015

Problem with opening SSL based Webpages (HTTPS) in Windows XP and 2003

HTTPS uses SSL based certificates that contain various informations such as authorized domain URLs, date, time, location, encryption keys etc to validate the connection and secure the data transactions between the client and the server.

If you get the above problem while you're browsing in XP or 2003:

1. Check your date and time settings if they're correct.
2. Your OS has not been updated for using the newest encryption, announced for Certificate Authorities in Windows Server 2008. (Hotfix for XP and 2003 is available here
3. The owners of the websites haven't updated the SSL certificates used on their websites, usually meaning that the site isn't managed by them anymore.
4. Your internet connection is insecure, tapped and your computer is receiving phished or modified data. Avoid using the link at all costs especially if this link belongs to a bank, social site, mail or such other services, because your identity and credentials(such as passwords) may be compromised by the perpetrators responsible for this.