Archive for the ‘Windows’ Category

How to create a unicode window in a non-unicode application?

December 22, 2009 Leave a comment

Recently a user asked this question in MSDN forums. He had MBCS enabled for his application and also wanted to enable unicode characters in a particular edit control. Note MFC only create unicode controls if UNICODE is defined in project settings.

So in order to explicitly create controls that support UNICODE characters we use the ‘W’ functions. For example: for our user he should use “CreateWindowW” function. Note the last ‘W’ in the function. ANSI windows are created by calling “CreateWindowA” function (this also means that we can explicitly create a non-unicode control).

Also make sure you use only UNICODE API’s for a control created in this manner. For e.g. always call GetWindowTextW instead of GetWindowTextA. Never use MFC on such a control if it’s not created by MFC framework, I mean don’t call CWnd::Attach on this control. Mainly because MFC is operating in non-unicode environment but our control is a UNICODE one.

Now once such a control is created you can paste characters of different languages, for e.g. Chinese, Japanese, etc even though the application is not a UNICODE supporting one.

Associate a path with a drive letter

March 16, 2009 4 comments

Use SUBST command…

SUBST [drive1: [drive2:]path]
SUBST drive1: /D

drive1:        Specifies a virtual drive to which you want to assign a path.
[drive2:]path  Specifies a physical drive and path you want to assign to
a virtual drive.
/D             Deletes a substituted (virtual) drive.

Type SUBST with no parameters to display a list of current virtual drives. An e.g. usage, create a  “p:” virtual drive for documents and settings folder path…

subst p: "C:\Documents and Settings"

To delete an existing virtual drive use…

subst /D p:

To see all existing virtual drives created…

C:\Documents and Settings>subst
N:\: => C:\
P:\: => C:\Documents and Settings

Also note that on machine restart these drives are not restored, for restoration after startup, create a batch file and put it into “Startup” folder in explorer. After a virtual drive is created, explorer shows this as an independent drive.

How to create a choice like command in a batch file?

November 30, 2008 1 comment

There used to be a windows command called choice, which takes yes or no for an answer. Recently I was making a batch file and tried the same command again but to my surprise found that it was not available.

So I didn’t implement that part. But later I found out another way and probably a better way to implement this behavior using the ‘set’ command.

For eg: if you wanna ask a question like, please enter your name, this is how we would do it…

set /p NameVar=”Please enter your name? “

When you press enter you will be asked for your name and on command completion your name will be stored in NameVar.

You can print out the value using…

echo %NameVar%

Categories: Windows Tags: ,

How to generate random numbers in a batch file?

November 22, 2008 7 comments

Ever wondered how we can generate random numbers in a batch file. Yes, I am talking about our good old faithful friend ‘.bat’ file. Why do I need one? You may ask! I had to generate a unique filename everytime because I had to backup a folder before I updated it.

So tell us is there one? Yes use ‘random’, it’s a built in command which generates a random number when you invoke it. Every time a unique one! So if you type in ‘random’ at a command line and then press enter, you’ll get an error because it’s not an independent executable, but a feature that is built into command prompt to help generate random numbers. So how will I get a random number? This is how I do it…

echo %random%

Make a random directory….

mkdir C:\Nibu_%random%

If Command Extensions are enabled, then there are several dynamic environment variables that can be expanded but which don’t show up in the list of variables displayed by SET.  These variable values are
computed dynamically each time the value of the variable is expanded. If the user explicitly defines a variable with one of these names, then that definition will override dynamic ones like %random%.

For more information type “set /?”

To enable command extensions open up regedit and then navigate to

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Command Processor\EnableExtensions
HKEY_CURRENT_USER\Software\Microsoft\Command Processor\EnableExtensions

and set value to 1. If not present then add one REG_DWORD value with an identical name and value set to 1.

DLL Best Practices

March 27, 2008 Leave a comment

Following information taken from this document maintained by microsoft!

An important excerpt from this document!

DllMain is called while the loader-lock is held. Therefore, significant restrictions are imposed on the functions that can be called within DllMain. As such, DllMain is designed to perform minimal initialization tasks, by using a small subset of the Microsoft® Windows® API. You cannot call any function in DllMain that directly or indirectly tries to acquire the loader lock. Otherwise, you will introduce the possibility that your application deadlocks or crashes. An error in a DllMain implementation can jeopardize the entire process and all of its threads.

Don’t do’s in DllMain of a Dll!

  1. Call LoadLibrary or LoadLibraryEx (either directly or indirectly). This can cause a deadlock or a crash.
  2. Synchronize with other threads. This can cause a deadlock.
  3. Acquire a synchronization object that is owned by code that is waiting to acquire the loader lock. This can cause a deadlock.
  4. COM threads by using CoInitializeEx. Under certain conditions, this function can call LoadLibraryEx.
  5. Call the registry functions. These functions are implemented in Advapi32.dll. If Advapi32.dll is not initialized before your DLL, the DLL can access uninitialized memory and cause the process to crash.
  6. Call CreateProces. Creating a process can load another DLL.
  7. Call ExitThread. Exiting a thread during DLL detach can cause the loader lock to be acquired again, causing a deadlock or a crash.
  8. Call CreateThread. Creating a thread can work if you do not synchronize with other threads, but it is risky.
  9. Create a named pipe or other named object (Windows 2000 only). In Windows 2000, named objects are provided by the Terminal Services DLL. If this DLL is not initialized, calls to the DLL can cause the process to crash.
  10. Use the memory management function from the dynamic C Run-Time (CRT). If the CRT DLL is not initialized, calls to these functions can cause the process to crash.
  11. Call functions in User32.dll or Gdi32.dll. Some functions load another DLL, which may not be initialized.
  12. Use managed code.

Safe things to do from DllMain of Dll!

  1. Initialize static data structures and members at compile time.
  2. Create and initialize synchronization objects.
  3. Allocate memory and initialize dynamic data structures (avoiding the functions listed above.)
  4. Set up thread local storage (TLS).
  5. Open, read from, and write to files.
  6. Call functions in Kernel32.dll (except the functions that are listed above).
  7. Set global pointers to NULL, putting off the initialization of dynamic members. In Microsoft Windows Vista™, you can use the one-time initialization functions to ensure that a block of code is executed only once in a multithreaded environment

Programmatically selecting a file in windows explorer!

December 19, 2007 10 comments

Well while coding for process viewer I felt the need for programmatically selecting a file in windows explorer and after a short search bumped into which had a code snippet in Delphi which does this.

Modified a “bit” for VC++…

// Select a file in explorer
 void SelectFileInExplorer( LPCTSTR lpctszFileToSelect_i )
    // This is the command line for explorer which tells it to select the given file
    CString csCommandLine = _T( "/Select," );
    csCommandLine +=  lpctszFileToSelect_i;

    // Prepare shell execution params
    SHELLEXECUTEINFO shExecInfo   = { 0 };
    shExecInfo.cbSize             = sizeof( shExecInfo );
    shExecInfo.lpFile             = _T( "Explorer.exe" );
    shExecInfo.lpParameters       = csCommandLine;
    shExecInfo.nShow              = SW_SHOWNORMAL;
    shExecInfo.lpVerb             = _T( "Open" ); // Context menu item    

    // Just have a look in MSDN to see the relevance of these flags

    // Select file in explorer
    VERIFY( ShellExecuteEx( &shExecInfo ));
 }// End SelectFileInExplorer

See command line arguments for explorer.exe on XP

My Windows tricks

June 8, 2007 3 comments

Shift + Click — on any hyperlink to open it in a new window.

Ctrl + + — Resize any list control to it’s contents.

*  — Expand any tree node and all it’s sub nodes in windows.
+ — Expand any tree node
–  — Collapse any tree node

Shift + Click — on any folder inside explorer to open it in a new window with explorer bar at it’s side showing the path leading to that folder.

Shift + Enter – Does the same as above

Ctrl + Enter will open any folder in a new window

Ctrl + Double Click will open any folder in a new window

Ctrl + Double click on any explorer title bar including internal explorer to see that window in full screen mode.

Alt + Drag to select in columns.

Ctrl + Shift + F8 to do the above using keyboard.

WinKey + L to lock the desktop

Alt + Enter on any file to view it’s property pages.

Alt + Double Click on any folder to open it’s property pages.

Alt + Spacebar to open System menu of any window

Alt + – to open system menu of main child windows, for eg: Word document windows

Shift + Close button ( On caption bar ) to close all parent windows of a child explorer window at once.

Winkey + D — Show desktop
Winkey + M — Minimize all
Winkey + Shift + M — Undo Minimize All
WinKey + R — Opens run dialog
Winkey + E — Opens explorer.
Winkey + U — Opens Utility manager.
Winkey — Opens start menu

Internet explorer version number

%d bloggers like this: