"Fortunate are those who take the first steps.” ― Paulo Coelho

What is GDI Object leaks and tips to detect..

Recently I worked for an office issue where Excel 2013 goes to frozen state after executing some time consuming VBA code/macro. It did some operations like – copy ranges and pasted over another sheet within the same workbook. The copy and paste operation iterates over some 100 thousand times depends on the data row etc. It took nearly 3-4 hours to complete the whole copy operation due to data density spreads to x columns with x number of rows.

Problem identification: We saw the application was very much alive but could not able to click or respond to our mouse events like maximize/minimize. Moreover it was slowing the system performance as well. We could not able to figure out the cause in the initial stage. But we tried tools like Procmon, windbg dumps, VMMAP etc could not give that heads up. But after checking the task manager GDI count, we come to know that this is sort of object leak – GDI Leaks creating this hang state/lock situation.

How to identify the GDI object leaks? It is so simple to check such leaks from the task manager itself. Launch the taskmanager > details tab > right click any of the existing column > then ‘Select Columns” enable GDI Object to get added to the details process grid. From there you can keep a note of the count to conclude whether its a GDI leak or not. Typically, you would find this count in hundreds, but in case if you notice them in thousands and also incremented, then something sure to do with GDI leak fix.

What is GDI Objects? According to this MSDN article – GDI Objects are resources that are managed by GDI32.DLL on behalf of an application. Some of the common GDI Objects we consume directly/indirectly through code – Device Contexts (DCs), Bitmaps, Brushes, Fonts, Metafiles, Pens, and Regions etc. These objects gets created using API call but when never gets destructed after usage – this would lead to this leak situation. As like in .NET, it is recommended to dispose of when not interested with that ready to cleaned objects. Of-course, we do this very judiciously, but at times when our code path not cleaned after some exception or some condition branching stops us to do so, then this would be a show stopper for sure :).

What is the limit? It is limited to 64,536 (64k) GDI handles per user session- across all process. But for any individual process, the upper limit is 10000. System allows us to create these many handles and then halts after reaching this limit. You could also try tweaking this limit from registry, but generally not advised to do so due to various reasons like -affects other application performance etc.

What happens after reaching this 10,000 limit? The application would be alive as I said earlier but of no use. It is starving to create further GDI Objects to render it but indefinite halt after that due to no more Create handles permitted. When an application goes out of resources, then the create API call to functions like CreateFont, CreateDC etc would fail with this error : ERROR_INVALID_HANDLE.

There are some tools and guidance to research more on this, but I see very limited materials around this in net. I suggest the below links.

Very old cached MSDN article(thanks to google cache)

Debugging a GDI Resource Leak

Suggested to try these Office June updates in case of such leaks (enhances copy paste operation speed) (some fixes around object leaks)

This slideshow requires JavaScript.

Update : 19-July
From scott blog, I found this nice tool to see the GDI count under types –


July 2, 2015 Posted by | .NET General, Memory, windbg | , , , | 1 Comment

Various ways to manage Azure resources explored

I just saw scott post on Azure Resource API lib intro. I thought let me think over all possible ways(recollect) today used to manage Azure Resources like VM, Websites, jobs etc from ones desktop.

1) – famously called azure preview portal.

2) – old portal or manage azure portal.

3) Azure PowerShell PowerShell is a friendly tool for infra guys/DevOps/Admin who manages and automate stuffs using PS commandlets, or advanced stuff like DSC -Desired State configuration, puppet,chef etc.

> There are lot of infra tools available in market place.

4) Azure Resource Manager API –  over REST API – fire commands like PUT,GET,POST from your browser. You could also use ARM client here. It is really nice cmd utility with simple commands written by David E

5) Microsoft Azure Cross Platform Command Line (separate installer available for Mac and windows) – azure-xplat-cli

6) From your managed .NET code too – Microsoft Azure Management Libraries 2.0.0

> Scott H explained how one could do this coding here

7) Using Visual Studio Server Explorer – explained here.

P.s: Let me know if you have any comments regarding this and any other ways left out. 


8) Blob Transfer Utility for Windows Azure Blob Storage  – read via – 

April 12, 2015 Posted by | .NET General, Azure, Azure Tools | | 1 Comment

Certified for 70-487 !! [ recently cleared 70-532 too ]

Last week I have cleared my 70-487 Developing Microsoft Azure and Web Services certification. I have started preparing for 45+ days on and off, mostly weekend reading and able to clear through second shot offer. There are plenty of materials there to prepare, but I urge to stick with official curriculum + MVA (microsoft virtual academy videos) for this exam. I had internal employee measure up account with some sample questions, with deep understanding of all those concepts + small hands on helped me crack it. Here is what you will learn through this certification,

9780735677241f1256.70-532_3E18159EChapter 1) Accessing Data using – EF, WCF DataServices, Azure Data Storage, Transaction, Caching, XML ds (data). ..

Chapter 2) Querying and Manipulating data by using EF – LINQ to Objects, entities, ADO.NET, EF, Lazy loading,data model…

Chapter 3) Designing and implementing WCF Services – SOA concepts, WCF API obj and config, Securing, consuming, version, Azure Service bus, one way-two way, hosting..

Chapter 4) Create and consume Web API based services – Design a web api, implement, secure, host and consume web api web services

Chapter 5): Deploying web application and Services- Design, deployment strategy, Configure, manage packages using Nuget, create, configure and publish a web package, share assemblies.

Overall, we  will learn back-end programming/db programming + infra projecs like publishing, nuget, etc also helps us to uinderstand end -to end.

Study notes from Chris –

[Update: May first week  – I have cleared 70-532 🙂 ]

April 8, 2015 Posted by | .NET General, Certification | , , , , | Leave a comment

Debugview.exe as rescue tool

DebugView is a developer friendly tool helps you to monitor debug output messages from local or nw system – from managed/unmanaged code. Recently I was debugging managed COM dll created in C# consumed in office application. I wanted a tool to debug end to end, Let say, your Office files VBA macro calls .NET COM dll for processing some complex logic and then returns the value to your macro.

In dev environment, we usually attach VS to the application and debug them, in VBA using debug.print statements. Assume that, you don’t have VS at your production server or customer machine where you wanted to debug them badly. Here is where debugview comes for rescue.

To write, debug statements from managed code, use Diagnostic having Debug.writeline. For VBA, use OutputDebugString. For other languages – use as here.

C#:- System.Diagnostics.Debug.WriteLine("This is from .NET before word instance.");

VBA:- Private Declare Sub OutputDebugString Lib "kernel32" Alias "OutputDebugStringA" (ByVal lpOutputString As String)
Private Sub DebugPrint(dbgOutput As String)
OutputDebugString dbgOutput
End Sub
Sub test()
DebugPrint ("Test message for the debugger")
End Sub

Note: Make sue you run as “Admin”.



March 22, 2015 Posted by | .NET General, Office | , , , | Leave a comment

Memory explained by Mark Russinovich

Pushing the Limits of Windows: Physical Memory

Pushing the Limits of Windows: Virtual Memory

Pushing the Limits of Windows: Paged and Nonpaged Pool

March 4, 2015 Posted by | .NET General | | Leave a comment

Troubleshooting masterpiece by Mark Russinovich – Weekend Read. (from TechEd NA-2014)

It is always funny, geeky, informative and casual when listening to Mark Russinovich Tech-ed session. It made my weekend 🙂

Be sure to watch this – especially blue screen one.

Mark Russinovich.jpg

December 8, 2014 Posted by | .NET General, 100 % Fun...[really Worth] | | Leave a comment

How to create your first Office Com Addin

1) Create a C# class library project

2) Add Extensibility reference to the project
 namespace ClassLibrary1
 public class Class1 : Extensibility.IDTExtensibility2

3) Implement the interface method and place a messagebox OnStartupComplete to test
4) From Project properties > Build > "Register for COM interop"
5) From Project properties > Application > Assembly information > "Make assembly COM Visible
6) Compile - VS should automatically build and register the COM to the registry
7) This would create something like this,
 Windows Registry Editor Version 5.00
8) Now, create the below registry file "a.reg" save and run to create this entry under HCU
For Excel, create under Excel and accordingly for other product
 Windows Registry Editor Version 5.00
Copy "{0D39F056-DF63-3860-9E79-B57F6358FD4D}" and search under HKEY_CLASS_ROOT\CLSID
Windows Registry Editor Version 5.00
 [HKEY_CLASSES_ROOT\Wow6432Node\CLSID\{0D39F056-DF63-3860-9E79-B57F6358FD4D}\Implemented Categories]
 [HKEY_CLASSES_ROOT\Wow6432Node\CLSID\{0D39F056-DF63-3860-9E79-B57F6358FD4D}\Implemented Categories\{62C8FE65-4EBB-45e7-B440-6E39B2CDBF29}]
[HKEY_CLASSES_ROOT\Wow6432Node\CLSID\{0D39F056-DF63-3860-9E79-B57F6358FD4D}\InprocServer32] @="mscoree.dll"
 "Assembly"="ClassLibrary1, Version=, Culture=neutral, PublicKeyToken=null"
 "CodeBase"="file:///c:/users/maheshk/documents/visual studio 2013/Projects/ClassLibrary1/ClassLibrary1/bin/Debug/ClassLibrary1.dll"
 "Assembly"="ClassLibrary1, Version=, Culture=neutral, PublicKeyToken=null"
 "CodeBase"="file:///c:/users/maheshk/documents/visual studio 2013/Projects/ClassLibrary1/ClassLibrary1/bin/Debug/ClassLibrary1.dll"
9) Now launch, Excel should see the message box on load - means this add-in loaded because of load behavior set as "3" - load at start. ( at the step #8 )

10)You can check this, by Excel > File > Options > Addins > Active Application Addin: ClassLibrary1.Class, location: mscoree.dll, COM Add-in

11) Register: 
"%WINDIR%\Microsoft.NET\Framework\v4.0.30319\RegAsm.exe" /codebase ComAddin\bin\Debug\ComAddin.dll
"%WINDIR%\Microsoft.NET\Framework\v4.0.30319\RegAsm.exe" /unregister ComAddin\bin\Debug\ComAddin.dll

Note: You may get access denied if you did not run as VS in Admin mode. 

Refer -
Download my sample - 
Download C++ sample -

November 8, 2014 Posted by | .NET General, Office, Outlook | , | Leave a comment

How to capture the dumps when the application crash on launching ?

Two ways you can achieve this,

#1) Using Procdump

>procdump.exe -w -e -ma SomeCrashingApp.exe
[This command used to wait for the exe to appear, crash to gather the dumps ]

If you are capturing .NET 4.0 dump, then this won’t triggered, you may have to specify -e 1 to gather the dump for later framework.
>procdump.exe -w -e 1 -ma somecrashing.exe
If it is older version of .NET then you could use this.
>procdump.exe -w -e -ma somecrashing.exe

#2) Using DebugDiag

1 2 3 4

October 28, 2014 Posted by | .NET General | Leave a comment

About DDEInitialize and sometime lockup scenario explained.

I had a chance to work with Excel DDE call lockup Issue,

> The first step in establishing a DDE conversation is that the DDE client will send a WM_DDE_INITIATE message to one or more windows running on the same desktop as the DDE client process. The WM_DDE_INITIATE message is a query to each window receiving the message to determine whether the process that owns the window supports the specified application name and topic. Typically, a DDE client will broadcast the WM_DDE_INITIATE message to all top level windows (both visible and hidden) running on the same desktop, looking for any running process that supports the specified application name and topic.

The implementation of the Excel DDEInitiate function broadcasts the WM_DDE_INITIATE message by calling the SendMessage function, specifying HWND_BROADCAST. The SendMessage call will not return until each top level window has processed the WM_DDE_INITIATE message. The hang that occurs when calling DDEInitiate is the result of one or more threads that own a top level not having an active message pump.

The ListBlockedWindow tool helps to identify each process that contains  threads that do not have an active message pump and own one or more top level windows. The applications identified by the ListBlockedWindow tool should be updated such that each thread that owns a window has an active message pump, even when a given thread owns windows that are not visible. The term “message pump” (or message loop) refers to the calls a thread makes to the GetMessage or PeekMessage functions to retrieve/process any pending window messages for windows owned by the thread. A thread that owns a window receiving a WM_DDE_INITIATE message and does not actively call GetMessage or PeekMessage will cause the thread that is sending the WM_DDE_INITIATE message with SendMessage to block until the message is processed.

Please refer to the following links for additional information about DDE and message processing in Windows.

About Dynamic Data Exchange –

Using Messages and Message Queues –

SendMessage function –

Note: this content is not mine, borrowed from my colleague-DaveAnd

October 25, 2014 Posted by | .NET General, Office | Leave a comment

Connect(); — Join this exciting virtual event for developers!







Connect(); is a cloud-first, mobile-first, code-first virtual event focused on current and future Microsoft technologies for developers. Build on your current skills, unleash your creativity, and expand what’s possible to deliver unprecedented innovations.

Want to ask and interact with Scott Guthrie, S. “Soma” Somasegar, Brian Harry, and Scott Hanselman & watch 40+ondemand vidoes?  

October 19, 2014 Posted by | .NET General | | Leave a comment

How to call C#.NET Class Library from Office VBA

1 2 3 4 5 6 7

August 23, 2014 Posted by | .NET General, Office, VBA | Leave a comment

How to create Automation COM Add in (in slideshow steps)

July 30, 2014 Posted by | .NET General | Leave a comment

Bing Code Search for C#

The Bing Code Search add-in for Visual Studio 2013 makes it easier for .NET developers to search for and reuse code samples from across the coding community, including MSDNStackOverflowDotnetperls and CSharp411.

Bing Code Search improves developer productivity and speed by bringing the experience of searching for reusable C# code into Visual Studio IDE.

Here is an excellent online portal to try this out.. 

Note: Oops this works only with VS 2013 😦

February 18, 2014 Posted by | .NET General, Uncategorized | Leave a comment

Design scenarios from – .NET Technology Guide for Business Applications.pdf

I highly recommend this pdf for anyone who is looking for design guide in .NET.

Note: It also talks about Silverlight scope, migration & suggestions. Get one from here –

.NET Technology Guide for Business Applications

January 6, 2014 Posted by | .NET General | Leave a comment

How to attach Visual studio dynamically

I never thought this would be so easy with this couple of statements. All these days, I used to put a hard coded message box thrown up and then try to attach manually VS by selecting that new process.  But this one is very useful if you want to do automatically break in VS.

if (!Debugger.IsAttached)

This would be useful if you are debugging multiple process in the Visual Studio – Ley say you HelloWorld.exe is creating a new exe LogViewer.exe and yourself want to debug end to end all in same VS.  More at MSDN-

January 3, 2014 Posted by | .NET General | 1 Comment

Robocopy download and command help

I cannot stop myself recommending ‘Robocopy’ for copying big files and folders across network. It simply does it job neatly and FASTER.

Download –  & Robocopy commands –

Handy one c:>  robocopy.exe “Z:\Comp Ver 4.11” “D:\ThirdParty_Build” “my_big_file.*”   – this to copy zip file.

November 12, 2013 Posted by | .NET General | Leave a comment

How to find missing dll in .NET? Fuslogvw.exe

Let say our app is ready for deployment for the first time or recompiled. And we want to double check whether all the dependencies were copied properly with right versioned assemblies referenced. Then here is the tool which would help you to avoid running into problem like ‘Could not load file or assembly’..

Here is the tool which gives you a list of bind failures in a log.

How to launch:
More tweaking :

   Note: Make sure you run this tool with admin privilege and also refresh to see them loading.

Save the below keys as .REG and double click to set it.
Windows Registry Editor Version 5.00


Windows Registry Editor Version 5.00


Note: Don’t forget to disable after enabling it.

November 3, 2013 Posted by | .NET General | Leave a comment

%d bloggers like this: