Thursday, November 15, 2007

Add Specific Folders to Open/Save Dialog boxes

When you use certain Windows applications (such as Notepad) to open a file, on the

left side of the Open dialog box you may have noticed a group of icons and folders

(such as My Documents, My Recent Documents, Desktop, My Computer etc) to which

you can navigate to open files. A registry hack will let you put just the folders of your

choice on the left side of the Open dialog box. Note that when you do this,it will be applied

to XP applications such as Notepad and Paint that use the Open and Save common dialog

boxes. However, it won’t affect Microsoft Office applications and other applications that

don’t use the common dialog boxes.

To start, create the following key in registry:

HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Policies\comdlg32\Placesbar

( Upto Policies might be there. You may have to create the other 2 ).

Now create a String value for it named Place0. Give Place0 a value of the topmost folder that

you want to appear on the Open dialog box, for example, C:\Projects.Next, create another

String value for Placesbar called Place1. Give it a value of the second folder that you want to

appear on the Open dialog box. You can put up to five icons on the Open dialog box, so create

new String values up to Place4 and give them values as outlined in the previous steps.Now

you may see the result of what you have just done by taking Open/Save dialog of applications

such as Notepad/Paint.

If you want to disable this feature, you can do that as well. In HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\ Policies\

comdlg32, create a new DWORD value called NoPlacesBar and give it a value of 1.If you want

the folders back, either delete NoPlacesBar or give it a value of 0.

Wednesday, November 14, 2007

Extracting Numbers from Strings using strol()

There are many applications that store numbers in strings that also contain text. For example, some databases store dates as strings in the form of "01Jan".


You can extract the number from a string by using the standard function strtol() ("string to long").

long strtol( const char *nptr, char **endptr, int base );

nptr

Null-terminated string to convert

endptr

Pointer to character that stops scan

base

Number base to use

The strtol function converts nptr to a long. strtol stops reading the string nptr at the first character it cannot recognize as part of a number.

In the following example, strtol() extracts the number 14 from the string "14-Feb" and assigns that number to n:

#include "stdafx.h"

#include

int main(int argc, char* argv[])

{

char s[] = "14-Feb";

long n = strtol(s, NULL, 0); //n = 14

return 0;

}

In this example, the radix is decimal. However, you can specify any radix between 2 through 36 as the third argument of strtol(). Alternatively, you can let strtol() deduce the radix automatically according to the numeric characters it reads from the scanned string by supplying 0 as the radix value.

Also check functions strtod and strtoul.

How to share data between different instances of DLLs

Setting up a shared data segment in a DLL. A shared data segment is used when you want to share variable values among several exe's. This is not as clever as a semaphore and not as powerful as a COM Event_Sink_MAP. However, it has very little overhead and it is very simple to implement. It is worth while knowing things can be done this easy.

One advantage of using a DLL is that several processes (exe's) can share a code segment. They share the code segment but each process gets it's own data segment. Therefore if five processes use a DLL there is only one code segment and five data segments.
What is a shared data segment?
A data segment is a chunk of main memory that holds the programs variables. It is the thing that gets swapped to the swap file.

A shared data segment in a DLL is simply one or more variables that are not unique to a specific process. In many respects this example code is very similar to a semaphore. If you really think that more than one process at a time is going to be updating the shared variables, then maybe a semaphore is the right choice for you. However we will control the update of the shared variables via critical sections, therefore this technique is just as safe as a true semaphore. What has more overhead, the ramp up cost of using CSemaphore all the time or the infrequent spin locking this approach might incur? The answer depends of what you are doing.
What makes a data segment shared

These three pragma statements override the default data segment behavior and creates a shared data segment. See MyClass.cpp in the DLL.

// Global and static member variables that are not shared.
...

#pragma data_seg("SHARED") // Begin the shared data segment.

// Define simple variables

// Integers, char[] arrays and pointers

// Do not define classes that require 'deep' copy constructors.
#pragma data_seg() // End the shared data segment and default back to
// the normal data segment behavior.


// This is the most important statement of all

// Ideally you can set this in the projects linker tab, but I never could get

// that to work. I stumbled across this in a discussion board response from

// Todd Jeffreys. This tells the linker to generate the shared data segment.

// It does not tell it what variables are shared, the other statements do that,

// but it does direct the linker to make provisions for the shared data segment.

#pragma comment(linker, "/section:SHARED,RWS")



That's it.
The code is written in VC 5. First build the DLL TestMemorySpace and then the driver executable Testexe.exe. Start two instances of the test exe, keep them both maximized, and click the button that says 'Read Me First'.