Wednesday, January 26, 2011

String To IntPtr

Often we required to copy our managed string into unmanaged memory and gets it pointer to pass string's integer pointer (IntPtr) to the unmanaged api. To create a copy of managed string into unmanaged memory call one of the below given static method of Marshal class.

1. Marshal.StringToHGlobalAnsi : Creates the ANSI encoded unmanaged copy of the string.

public static IntPtr StringToHGlobalAnsi (string s)

E.g.
string str = "my string";
IntPtr ptrStr Marshal.StringToHGlobalAnsi(str);

2. Marshal.StringToHGlobalUni : Creates the Unicoded encoded unmanaged copy of the string.

public static IntPtr StringToHGlobalUni (string s)

E.g.
string str = "my string";
IntPtr ptrStr = Marshal.StringToHGlobalUni(str);


Releasing the unmanaged resources
The unamanaged resources are not disposed or released by the garbage collector so we must release the unmanaged resource after its use and to release our unmanaged string from memory we uses another static method Marshal class which is Marshal.FreeHGlobal

public static void FreeHGlobal (IntPtr hglobal)

E.g.
FreeHGlobal(ptrStr);

Tuesday, January 25, 2011

C#.NET Combinational mouse and Keyboard shortcuts

Often it is required to implement combinational keyboard shortcut like shift+left Click or Shift+Right Click but we cannot implement them so easily in provided mouse and keyboard events like (mousedown, click, keydown, keypress or keyup) because in all these events one of the event information either Key events or mouse events are missing. So to implement such shortcut we can use Microsoft.VisualBasic.Device.KeyBoard class.

The same is discussed with example by me at eggheadcafe's FAQ section
http://www.eggheadcafe.com/sample-code/csharp.NET/d5c0ae86-2bd8-4abf-9c88-335779499169/net-keydown-events-like-shiftclick-or-altclick.aspx

Friday, January 21, 2011

Registry Trick: Keeping the Outlook 2002 in system tray instead of taskbar

If you are using outlook 2002 and you want to keep the outlook in system tray like th outlook 2003 and above you have to addd registry value. Which will put the outlook in system tray when minimized.

Step1: Goto start>run type regedit and press enter

Step2: Browse to the below registry key
HKEY_CURRENT_USER\Software\Microsoft\Office\10.0\Outlook\Preferences

Step3: Modify or create the value MinToTray of DWORD datatype and set its value to 0 = Disabled or 1 = Enabled

Step4: Exit regedit and restart outlook.

Now on minimizing outlook it will move to system tray.

Registry Trick: Hide and lock any drive(s) using regedit

You can hide any of drive(s) by just adding two keys in windows registry

goto start>run

Type regedit and press enter. This will bring you to the registry editor.To hide any drive you have to follow these 2 steps

STEP1: Hidding Drive
goto HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer now create a new DWORD value NoDrives and set its value as

2^(Alpha Number of Drive Letter-1)
where Alpha number are simple counting of alphabets from A to Z as 1 - 26

for example: to hide C drive
Alpha number of C is 3 so 2^(3-1) = 4 (decimal value)

If you want to hide more than one drive than calculate the value of each drive as explained and then set sum of those numbers as value

Step2: Prevent Access to the drive
Now as we have make our drive invissible but it is still accesible so to lock the drive we will create another DWORD value at

HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer
create a new DWORD value NoViewOnDrive and set its value same as you have calculated for hidding the drive(s)

Now your drive is locked. If you want to unlock and unhidden the drive then just delete these two keys and your drive is again accessible. You can also create two .reg files one for hidding and locking and another one for unlocking and unhidding