Sunday, May 1, 2011

Constant pointer and pointer to a constant

Declaring pointer to a constant OR constant pointer is always confusing. Following rules will help removing the confusion
1. Constant gets applied to the symbol that appears to its immediate left. If there is no symbol in the left then it gets applied to the symbol which to its immediate right.
2. While reading the variable declaration, always start at the identifier and read backwards.
e.g.
1. const int *I; // No symbol on the left of const so it gets applied to the right symbol i.e. integer.
I is a pointer to a constant integer.
2. int const *I // const gets applied to int (immediate left)
I is a pointer to constant integer.
3. int * const I // const gets applied to pointer (immediate left).
I is a constant pointer to integer.
4. const int* const I ;
I is a constant pointer to constant integer.
5. int const* const I;
I is a constant pointer to constant integer.

Some other useful tips about constants
1. Always use const instead of #define for better type safety. Constants always use implicit linking so its scope is file level. To access const in another file, declare it as extern.
2. Constant can be declared compile time or run time.
e.g. const int i = 10; // compile time constant
const int i = ReadI() // runtime constant.
But once assigned a constant it cannot be changed through out the lifetime of the application.
3. Constants can be declared in class. Instance level constants can be declared as follows
class A
{
const int i; // I is a constant but only for an instance and not at the class level
};
4. Declare class level constant using static const
class A
{
static const int i; // i is constant at class level i.e. across instances of class A.
};
5. Apply const to a function if function cannot change any data members of a class.
6. Dont use const_cast to remove constness of a member variable inside constant funtion since there is no way for a caller to know if function can change class member variable. Instead declare the member variable which can be changed in a constant function as 'mutable'. This gives clear clue to the caller that the constant member function can change mutable member variable.
7. const and volatile takes part in function overloading. So following two are acceptable function declarations.
void foo(int);
void foo(int) const;
void foo(int) volatile;

Wednesday, November 24, 2010

Configuration files for .net assembly

.Net windows application or web application have a configuration file where application specific configuration information is stored e.g. windows application stores configuration information in appName.config file and web applications in web.config file.

.Net does not allow having assembly specific configuration file.

The application configuration file is stored in application default app domain. All the binding rules are then followed as per the application configuration file.

It’s possible to specify custom configuration file while creating the app domain. So load your assembly in different app domain and specify the custom configuration file while creating app domain. Read AppDomainSetup class for more information on how to set configuration file.

Directing assembly loader to look for an assembly in specific folder

.Net assembly loader loads assemblies from GACUtil or from private path given in the application configuration file. It cannot load assemblies from any other folder than what is specified in application configuration file. In some cases, it’s not possible to modify application configuration file e.g. plug-in (add-on) architecture. Plug-in vendor writes a plug-in (or add-on) which is nothing but a library implementing certain well known interfaces. Generally, the plug-in binaries are deployed in the dedicated folder as specified given by the application vendor.

If the plug-in binary requires certain assemblies to be loaded from a well known path then it cannot just change the application configuration file as the configuration file is not owned by plug-in and it’s inappropriate to change loading application configuration file. Another way to modify private path is to change it using AppDomain class. The plug-in library gets loaded in some appdomain (either dedicated or default). AppendPrivatePath method of the appdomain class can be used to append your own path in the private path of the application. This way the plug-in dependant binaries will get loaded in the application. As per Microsoft AppendPrivatePath method is obsolete and may not be available in future versions of .net. It’s available in .net version 2.0, 3.0, 3.5 and 4.0. Microsoft recommends AppDomainSetup class instead. The object of this class can be passed to the AppDomain::Create method. The problem with this approach is you will have to create separate appdomain otherwise it will not work. Sometimes it’s not possible to create separate AppDomain e.g. if you are created UI controls then it’s not possible to pass UI control objects across appdomain.

Another way to achieve this is to register the dependant assemblies in GAC. The loader first searches GAC for any dependant assembly and then private path. But deploying assemblies in GAC is discouraged if it’s not shared between multiple applications as it violates .net principle of xcopy install.

Wednesday, July 21, 2010

Windows tools

MSInfo32.Exe - Shows system information. Lists device drivers and running processes. The data collected can be exported in a text file.

TaskList.exe - Displays a list of currently running processes on either a local or remote system.

pstat.exe - Gets installed as part of windows 2000 support tools. It shows list of all the processes running on the system and Shows following process specific information - time spent by the process in user mode, time spent by the process in kernel mode, maximum working set size, # of page faults occured, commited memory size, # of open handles, # of running threads and process id.

pview.exe - Gets installed as part of windows 2000 support tools. Lists all the running process on local or remote machine. It shows following process specifc information - working set, heap usage,all the running threads in a process. It shows detailed information about running thread like - thread prority, processor time used by the thread, kernel time, user time, context switeches done by the thread, start address of thread and dynamic priority.

msizap - Msizap.exe is a command line utility that removes either all Windows Installer information for a product or all products installed on a computer.

Thursday, May 27, 2010

RpcServerUseProtseqEp returns error code RPC_S_PROTSEQ_NOT_SUPPORTED (1703)

RpcServerUseProtseqEp takes protocol sequence and endpoint paramters as type of string. If your program is compiled with UNICODE and if any of these parameters are passed non-unicode value then the function RpcServerUseProtseqEp returns error code 1703 i.e. RPC_S_PROTSEQ_NOT_SUPPORTED. Message string for RPC_S_PROTSEQ_NOT_SUPPORTED error code is “The RPC protocol sequence is not supported”.

Pass the string parameters as unicode (by prepending L to the strings) to fix this error

Tuesday, April 6, 2010

.Net tools and utilities

CLRVer.exe - Lists installed CLR versions on a system. Note that it does not list the .net frameworks installed on a system. e.g. .net framework 3.0 uses CLR version 2.0.XXX.
CorFlags.exe - Look at header information emitted in a managed module
DumpBin.exe - Look at header information emitted in a managed module
AL.exe - Assembly linker. Command line utility to create assembly using modules and data (resource) files.
ILASM.Exe - IL assembler
ILDASM.Exe - IL Disassembler
PEVerify.exe - Examines all of assemblies methods and notifiies of any method that contain unsafe code.
NGen.exe - Native code generator. Generate native code during application installation and stores it in a file on the machine.
Obfuscator - scramble names of all private symbols in assembly metadata
TlbImp.exe - TLB (Type library) importer
TlbExp.exe - TLB (Type library) exporter
RegASM.exe - register assembly as a COM component.
SN.exe -Generate public/private key pair.
GACUtil.exe - install/uninstall strongly named assemblies into GAC (Global Assembly Cache)
FxCop.exe - Application that analyzes .net assemblies and report information about the assemblies, such as possible design, localization, performance and security improvements.
FulLogvw.exe - Assembly binding log viewer. It displays details for failed assembly binds. This information helps you diagnose why the .NET Framework cannot locate an assembly at run time.
wsdl.exe - Command line tool to generate proxy class for XML web services.
BinDiff.Exe - Comapare two windows executable files. This tool is very useful to find out files changed between two builds of release/product.

Thursday, April 1, 2010

Concurrent connections to SQL server or SQL express

SQL server or SQL express supports unlimited (maxint) number of concurrent connections. SQL server installation wizard asks for number of connections to limit to. If the value given is 0 then it means unlimited number of connections. User can specify any number of connections that he wants.

It is a myth that SQL server or SQL express supports only 5 concurrent connections. There is some history behind it. Microsoft came up with MSDE engine to overcome limitations of popular Jet database engine. The initial version of MSDE engine could allow maximum 5 concurrent connections as the performance detoriorates if more than 5 concurrent connections are made to the MSDE database engine. Governor component in MSDE engine used to control the maximum connections.

The latest versions of SQL server (I guess SQL 2000 onwards) and SQL express does not have governor component. So now what limits the concurrent connections is now how the connection is made, for how much time and how it is used.