Monday, 6 August 2012

Creating your own Folder Locker in Windows Operating systems.

Wow..today I came to know how to write my own folder protector and safe guard my files with out using any software. I started my career in IT as a Software Instructor for teaching people as Computer fundamentals, DOS, MS-Office etc.

At that time I knew that DOS is Powerful and has the capacity to do many low level works but not had the idea that it can create a utility file for me when I am searching for a software to protect it from the guys who stoles the data.

here is the Bat file content.

MyFolderLocker.bat

cls
@ECHO OFF
title Folder Technade
if EXIST "Control Panel.{21EC2020-3AEA-1069-A2DD-08002B30309D}" goto UNLOCK
if NOT EXIST Technade goto MDLOCKER
:CONFIRM
echo Are you sure u want to Lock the folder(Y/N)
set/p "cho=>"
if %cho%==Y goto LOCK
if %cho%==y goto LOCK
if %cho%==n goto END
if %cho%==N goto END
echo Invalid choice.
goto CONFIRM
:LOCK
ren Sudarsan "Control Panel.{21EC2020-3AEA-1069-A2DD-08002B30309D}"
attrib +h +s "Control Panel.{21EC2020-3AEA-1069-A2DD-08002B30309D}"
echo Folder locked
goto End
:UNLOCK
echo Enter password to Unlock folder
set/p "pass=>"
if NOT %pass% = = sudarsan123$ HERE goto FAIL
attrib -h -s "Control Panel.{21EC2020-3AEA-1069-A2DD-08002B30309D}"
ren "Control Panel.{21EC2020-3AEA-1069-A2DD-08002B30309D}" Sudarsan
echo Folder Unlocked successfully
goto End
:FAIL
echo Invalid password
goto end
:MDLOCKER
md Sudarsan
echo Sudarsan created successfully
goto End
:End

Here the password is sudarsan123$, you can change the password as per your wish.

Lets go for a test:

Copy the bat file which you had created from the above content, now take it to the place where you want to hide the files.

Here create a folder name Sudarsan and store your files you want to protect . Now run the batch file by double clicking it.

For unlocking again , run the file it will ask for password and here i had used  sudarsan123$, once you provide the password correctly it will unlock the folder.


Happy Coding......

How to protect your data to be taken through usb in Windows 7 or Win XP

I came to notice few days back that some one is using my system and I found data loss as windows system are prone to data theft easily. so, followed a technique to stop it with out using any software.

There are three ways are there,

1. Make you Registry to protect in such a way that it should not write data into the USB drives

2. Use a .bat file that protect your files and folder with a password.

3. Use a folder locker/ PC Locker for this purpose.

Write Protect to the USB Drives:

Go to the Start menu --> Run---> type Regedit,
You will get the Windows Registry Editor.

Now search for the Following key:
    HKEY_LOCAL_MACHINE\System\ControlSet001\Control\StorageDevicePolicies

if the "StorageDevicePolicies" key is not found you can create a new one in HKEY_LOCAL_MACHINE\System\ControlSet001\Control


Now set the Key value to 1 in order to protect it from writing data to the USB Devices.

If you you want to copy data from you computer to usb devices it will not allow.

In order to write the data again to the USB devices you can chane this value to 0 again.



for Windows Xp Machines follow the above same procedure but you the registry key is:
HKEY_LOCAL_MACHINE\System\ControlSet001\Control\StorageDevicePolicies


Note: Just back up the registry before changing any thing so that if u do any thing wrong or the system goes unstable it can be restored.

Happy coding...

Friday, 20 July 2012

How to Show a Pop-Up Message Window in Asp.net (Like the Message Window in Windows Forms)

You all might aware of that in win forms you might have used Message.Show("Some Text")  to show some information. However in Asp.net forms you might have used Alert( ) or used a Literal or Label to Show some Message when you want to show some thing to the user like  1)  Your Resume has been updated Successfully. or 2) User Created Successfully or 3) A mail has been sent to your Mail ID and so on. In above all case the best one is to be go for a Literal or a Label and show the message there. How ever you want to show a message like alert using Literal, then go for the code below:


public static void ShowMessage(Page page,string message)
{
     Literal literalMessage=new Literal( );
     literalMessage.Text="<script>alert('" + message + " ')</script>";
     page.Controls.Add(literalMessage);
}

How we can use the above code :-

Just think that we want some message on a button click event,then we can use it as:

protected void btnSave_Click(Object sender, EventArgs e)
{
    ShowMessage(this, "Data updated Successfully!");
}

Enjoy it...........


Thursday, 24 May 2012

Why int is 16-bit in 16-bit compilers but even if long, float are 32 bits?

This is because of processor size. As the default data type is int so, if this matches then it optimizes the operations. another important thing is that int depends on OS, Processor, etc. so it is 16-bit in 16 bit compiler.

IEnumerable.Count() is a bad style of Coding, Why?

IEnumerable<Participant> participants=GetParticipants();

if(products.count()==0)
{
       SomeControls.Visible=false;
}
else
{
     SomeControls.Visible=true;
}
The wrong thing with the above code is that Count() is a Linq extension Method that literally iterates through every item in an  enumerable( here it is participants). In other words it won't short circuit after it finds more than zero items and will continue iterating through every single item. And while, that one may be fast operation in Dev environment but it will be a slow process in production. This is because of Linq's deferred Execution.

How we can refine the above Code

The above Code can be refined by changing it into as:
IEnumerable<Participant> participants=GetParticipants();

if(products.Any())
{
       SomeControls.Visible=false;
}
else
{
     SomeControls.Visible=true;
}

In the above code once the expression finds more than one item, it simply stops iterating and just increasing the Optimization time.

Happy Coding.........!

Thursday, 17 May 2012

Function to find GCD using Euclidean Algorithm

int GCD(int a, int b)
{
     while (a != 0 && b != 0)
     {
         if (a > b)
            a %= b;
         else
            b %= a;
     }

     if (a == 0)
         return b;
     else
         return a;
}

Using "Bitflags" in C

Bitflags are a method of storing multiple values, which are not mutucally exclusive, in one variable. You've probably seen them before. Each flag is a bit position which can be set on or off. You then have a bunch of bitmasks #defined for each bit position so you can easily manipulate it:
 
#define LOG_ERRORS            1  // 2^0, bit 0
#define LOG_WARNINGS          2  // 2^1, bit 1
#define LOG_NOTICES           4  // 2^2, bit 2
#define LOG_INCOMING          8  // 2^3, bit 3
#define LOG_OUTGOING         16  // 2^4, bit 4
#define LOG_LOOPBACK         32  // and so on...

// Only 6 flags/bits used, so a char is fine
unsigned char flags;

// initialising the flags
// note that assignming a value will clobber any other flags, so you
// should generally only use the = operator when initialising vars.
flags = LOG_ERRORS;
// sets to 1 i.e. bit 0

//initialising to multiple values with OR (|)
flags = LOG_ERRORS | LOG_WARNINGS | LOG_INCOMING;
// sets to 1 + 2 + 8 i.e. bits 0, 1 and 3

// setting one flag on, leaving the rest untouched
// OR bitmask with the current value
flags |= LOG_INCOMING;

// testing for a flag
// AND with the bitmask before testing with ==
if ((flags & LOG_WARNINGS) == LOG_WARNINGS)
   ...

// testing for multiple flags
// as above, OR the bitmasks
if ((flags & (LOG_INCOMING | LOG_OUTGOING))
         == (LOG_INCOMING | LOG_OUTGOING))
   ...

// removing a flag, leaving the rest untouched
// AND with the inverse (NOT) of the bitmask
flags &= ~LOG_OUTGOING;

// toggling a flag, leaving the rest untouched
flags ^= LOG_LOOPBACK;

 
WARNING: DO NOT use the equality operator (i.e. bitflags == bitmask) for testing if a flag is set - that expression will only be true if that flag is set and al others are unset. To test for a single flag
 
if (flags == LOG_WARNINGS) //DON'T DO THIS
   ...
 

Using Enumerations in C and C++

Whenever you need to define a set of mutually exclusive states, always use an enumeration. Don't use an int and a bunch of #defines. If space is really a premium and you only need a few things, you could settle for a char and a few letters as a code. If you need to represent some states that aren't mutually exclusive, use bitflags (see the next section).
An enumeration is basically an integer type associated with a bunch of special tokens. These tokens can be used for assignment and is-equal or not-equal checks - you can think of them as a sort of special #define.
 
//declare the enumeration _type_
enum BirdState {FLYING, LANDING, STANDING, TAKEOFF, EATING, SLEEPING};

//create a variable of it (in c or c++)
enum BirdState bs1 = SLEEPING;
//create a variable of it (in c++ only)
BirdState bs2 = SLEEPING;

//compare state
if (bs1 == EATING)
   bird1.hungry--;

//set and compare state
if (bs1 == TAKEOFF && bird1.velocity > 0.3)
   bs1 = FLYING;
 
There are some differences between enums in C and C++. First, in C++ you do not need the enum keyword when declaring a variable. Second, in C++ you cannot use general integer operators (such as arithmetic and bitwise operators) on enumerations
 
enum BirdState bs2;  // legal in C and C++
BirdState bs3;       // legal in C++ only

typedef enum BirdState BirdState_t
BirdState_t bs4;     // can use typedef like this
                     // to make it legal in C as well

bs2++;               // Illegal in C++
bs3 = 2;             // Illegal in C++
bs4 = bs2 | bs3;     // Illegal in C++
 

Polymorphic Structures in C

This can be achieved with a little creative union and struct work. The basic idea is to have a structure representing a superclass, which contains a union of all the other subclasses.
You will need another entity (preferably an enumeration) in the superclass and all subclasses to determine what type of struct it is.
Example:

typedef struct{
   int type;
   float ypos;
   float xpos;
   float length;
   float height;
} Shape_Triangle

typedef struct{
   int type;
   float ypos;
   float xpos;
   float length;
} Shape_Square

typedef struct{
   int type;
   float ypos;
   float xpos;
   float radius;
} Shape_Circle

typedef union{
  int type;
  Shape_Square square;
  Shape_Triangle triangle;
  Shape_Circle circle;
} Shape;

...

Shape s = getShape();
switch(s.type)
{
   case SHAPE_SQUARE:
      s.Shape_Square.length=3;
      break;
   case SHAPE_TRIANGLE:
      s.Shape_Triangle.height=4;
      break;
   case SHAPE_CIRCLE:
      s.Shape_Circle.radius=5;
      break;
}

A drawback of this method is that you need to duplicate the members in the substructure, and you must make sure the type variables are all in the same physical position (first is best) in the struct/union. Alternatively, use a struct for the superclass and subclass:

typedef struct{
   float length;
   float height;
} Shape_Triangle

typedef struct{
   float length;
} Shape_Square

typedef struct{
   float radius;
} Shape_Circle

typedef union{
  Shape_Square square;
  Shape_Triangle triangle;
  Shape_Circle circle;
} Shape_Union;

typedef struct{
   int type;
   float xpos;
   float ypos;
   Shape_Union subshape;
}

...

Shape s = getShape();
switch(s.type)
{
   case SHAPE_SQUARE:
      s.subshape.Shape_Square.length=3;
      break;
   case SHAPE_TRIANGLE:
      s.subshape.Shape_Triangle.height=4;
      break;
   case SHAPE_CIRCLE:
      s.subshape.Shape_Circle.radius=5;
      break;
}


This requires extra typing "through" the union.

Bit Masks in C

Bit masks are where you specify the number of bits to use in a integral member of a class or struct. C has more restrictions than C++ on this. In particular, only "int", "signed int" or "unsigned int" can have bit masks in C. Many C compilers ignore bit masks altogether.
First rule of bit masks: Don't. Don't unless you have a really bad need to save a tiny amount of memory, and slowing down your code a lot isn't a problem (the compiler will have to work around things as they are not properly aligned). Bit masks are also inherently non-portable and differ on different compilers on different machines. Some compilers will ignore them totally.
If you really want to go ahead with it, all you do is put a colon after the member followed by the number of bits it should occupy. Here's an example:

struct TwelveHourTime
{
   unsigned hour     : 4;     //4 bits (16), enough for 1-12
   unsigned minute   : 6;     //6 bits (64), enough for 0-59
   unsigned am       : 1;     //1 bit, on for am off for pm
};

//   0 1 2 3 4 5 6 7 8 9 A
//  |_ _ _ _|_ _ _ _ _ _|_|
//    hour     minute    am 

You can use bit masks with a blank identifier to add some padding:

struct TwelveHourTime
{
   unsigned hour     : 4;     //4 bits (16), enough for 1-12
   unsigned minute   : 6;     //6 bits (64), enough for 0-59
   unsigned          : 5;     //5 unused bits
   unsigned am       : 1;     //1 bit, on for am off for pm
};

//   0 1 2 3 4 5 6 7 8 9 A B C D E F
//  |_ _ _ _|_ _ _ _ _ _|_ _ _ _ _|_|
//    hour     minute    (unused)  am 

You can a blank identifier with bit mask of 0 to force the next element to align to the boundary of the given type:

struct TwelveHourTime
{
   unsigned hour     : 4;     //4 bits (16), enough for 1-12
   char              : 0;     //push minute to the next byte boundary
   unsigned minute   : 6;     //6 bits (64), enough for 0-59
   unsigned am       : 1;     //1 bit, on for am off for pm
};

//   0 1 2 3 4 5 6 7 8 9 A B C D E
//  |_ _ _ _|_ _ _ _|_ _ _ _ _ _|_|
//    hour  (unused)  minute     am

Again, bit masks are very non-portable and the exact layout depends on the compiler and platform. The diagrams are a guide only.