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.

Saturday, March 20, 2010

CoCreateInstance returns CO_E_SERVER_EXEC_FAILURE (0x80080005) for ATL service created using Microsoft Visual Studio 2008

Create an ATL service project using Microsoft Visual Studio 2008. If you try to create instance of an object which is hosted by the service, CoCreateInstance returns error 0x80080005. The error means server execution failed.


It took lot of time to resolve this error and was quite frustrating as the error message is generic and does not give any clue about what could have gone wrong.
I tried creating a service which has one COM object and has a foo() method which does nothing and just returns. The CoCreateInstance was failing for this object as well with same error code.


While googgling for the error code, I came across an article which talks about a defect in Visual Studio 2008 wizard generated registry files. http://blogs.msdn.com/jigarme/archive/2008/05/08/cocreateinstance-returns-0x80080005-for-visual-studio-2008-based-atl-service.aspx

The APPID entry is missing from the wizard generated .rgs file. The error got fixed after adding the APPID entry in the .rgs file

ForceRemove {9ACB97C6-4492-40E8-A1BD-51DFE8962E92} = s 'CMyObject Class'
{
ProgID = s 'ATLComService.CMyObject.1'
VersionIndependentProgID = s 'ATLComService.CMyObject'
ForceRemove 'Programmable'
LocalServer32 = s '%MODULE%'
val AppID = s '%APPID%'
'TypeLib' = s '{E423A7DE-58C7-4535-A864-395DCEDE941F}'
}

Saturday, March 6, 2010

GetRecordInfoFromTypeInfo and GetRecordInfoFromGuids returns E_INVALIDARG if COM structure contains a GUID member field

Recently I was working on developing a COM service which returns a SAFERRAY of structure as out parameter. This structure has member of type GUID.

To create SAFEARRAY of custom data types, you need to get the IRecordInfo pointer to the structure and pass it to SafeArrayCreateEx. You can get IRecordInfo pointer to the custom structure by calling either GetRecordInfoFromTypeInfo or GetRecordInfoFromGuids API.

Both of these API returns E_INVALIDARG if the sturcture contains member of type GUID. One way to workaround this problem is to convert GUID as BSTR.

This is a defect in Microsoft Visual Studio 2008 and hopefully will get fixed in next versions. Link to the microsoft page for same. https://connect.microsoft.com/VisualStudio/feedback/details/380266/getrecordinfofromtypeinfo-returns-e-invalidarg-if-a-com-structure-contains-a-guid-field