Search for Symbols in Visual Studio more Easily

15 11 2009


Ever tried VisualAssist? Yes man, Its a killer product. The feature that I like most is its Symbol Search. You can specify words and it will list symbols that contain those words. Its very useful if you have a vague idea about the function name that you’re searching for. Have a look at the following screenshot.

Search3FindInVisualStudio

But is there any PoorMan’s SearchSymbols without VisualAssist?


Did you forget about the hidden gem in VisualStudio find window? Yes, the Regular Expressions. You can use .* to do the same search done by VisualAssist. For instance, to do the above search, search for Show.*Msg. Have a look at following screenshots.

Search4

Now look at the results. Cool! isn’t it?

Search5


Quite easy. nah? BTW, did you try to kill that bug. 😉


Targeted Audience – Beginners.

Advertisements




How to import dialogs or resources from one project to another?

9 09 2008


Designing dialogs – that could grab a good amount of time! But often we can find dialogs that can be reused from our previous projects. But the copy paste trick – the Ctrl+C & Ctrl+V won’t work on dialog resources. So how to copy/paste dialogs or any other resources from one project to another?

importdialog


Assume you have two projectsProject1 and project2 and you want to copy one dialog from Project1 to Project2. Just follow the steps –

For Visual Studio 6.0
1) Load project2 workspace in visual studio.
2) Now Browse and load Project1’s rc file. While opening resource file, select “Open As” as “Resources”.

3) Now drag your required dialog from project1’s resource tree and drop it to your resource tree.

4) You’ve successfully copied dialog from one project to another!

For Visual Studio 2005 and siblings
1) Open your IDE without any solutions loaded.
2) open both RC files into the IDE.
3) Now take Project2 RC file, right click on the dialog you want to export and copy it.

importdialog1

4) Now take Project1 RC file, right click and paste.

importdialog2

5) You see, now the dialog is imported from project1 to Project2.

importdialog3

Well, special thanks to Mike and Alan for their contribution for the trick in Visual Studio 2005.


You could also edit the rc file in some text editor and can copy paste the dialog if you’re a geek. 😉


Targeted Audience – Beginners.





Interesting bug in VisualStudio!

15 07 2008


Once in technical forum, a guy asked how to add ampersand( & ) symbol to control captions? Because the ampersand( & ) symbol is used to specify the keyboard accelerator for the control. Have a look at my previous post –
http://weseetips.com/2008/06/15/how-to-add-ampersand-symbol-to-dialog-control-captions/

Visual Studio, the ultimate creator – recommends to add && to make ampersand visible in control captions. But, does the creator himself suffer due to ampersand problem? 😉 Read on…


Have a look at the properties of Auto variable item in Visual Studio window. It suffers the same bug. The variable name is “&lResult”. But in the properties window, its displayed as lResult”. See the screenshot.


Pretty funny, nah? BTW, Was that guy from Microsoft? 😉 (Just kidding)

But I always bow my head in front this legendary compiler suite. Its a classic. I tried a lot, but my heart won’t allow me to switch to the new generation Visual Studio IDEs. Still 6.0 is my favorite. What about you guys? Comment on!


Targeted Audience – Beginners.





How to enable/disable all breakpoints in single keystroke?

9 06 2008


During heavy debugging, we usually have dozens of breakpoints dropped in various locations of source. Some of these breakpoints can also be in the application startup route. While stopping and starting programs during debugging, these breakpoints are really irritating, because they stop always. I’ve to press F5 a couple to times to start the application and run it. If there’s some shortcuts to enable and disable all break points at one single keystroke, it will be nice. isn’t it? But how?


You can use the VisualStudio’s powerful object model to do so. See the macro code for enabling and disabling the breakpoints. In later steps, i’ll explain how to map it to keystroke. The macro code is as follows,

' Macro to Enable all Breakpoints.
Sub EnableBreakPoints()
    Dim myBreakpoint
    For Each myBreakpoint in Debugger.Breakpoints
        ' Enable the break points.
        myBreakpoint.Enabled=True
    Next
End Sub

' Macro to Disable all Breakpoints.
Sub DisableBreakPoints()
    Dim myBreakpoint
    For Each myBreakpoint in Debugger.Breakpoints
        ' Disable the break points.
        myBreakpoint.Enabled=False
    Next
End Sub

How to map the macros to Keystroke
1) Take Tools > Macro.
2) Now you’ll see your macro window and i assume no macros are defined in your macro file. For getting the macro editor, enter some dummy macro name and click “edit” button.

3) Now Macro editor will be opened and copy-paste the Enable/Disable macro code in your macro editor and save it.
4) Now take Tools > Macro again and VisualStudio will ask, whether to reload the macros. Press Yes.
5) Now you’ll see the macro dialog, and click “Options” and “Keystrokes“.

6) Now select the macro and press a key combination, then click “Assign”.

7) The greatest difficult that you’ll face is to find a free slot of key combination. Almost all are reserved by visual studio itself. 🙂 After some R&D I’ve found two working key combinationAlt+z and Alt+x. Map your enable/disable macros to those shortcuts.

8 ) Now take visual studio and press the key combination to disable the breakpoints. Note that the breakpoints will not be visually disabled but if you take breakpoint window by Ctrl+B you can see all are disabled. This happens because we’ve modified the internal structure of VisualStudio, but GUI is not refreshed.


The only drawback is the breakpoints will not be visually disabled on keystroke. I’m searching for some solution and hope i’ll be back soon. Till then enjoy this one. 😉


Targeted Audience – Intermediate.





Let Visual Studio talk to you – Events in Visual Studio Object Model.

10 04 2008


Did you ever wish to see a “Bye Bye!” from Visual Studio when you exit? Did you ever wish to see a message, when the build is completed, instead of that boring chime sound? The answer is – Visual Studio Object Model Events.


Visual Studio Object model Events are the ones, which Visual Studio triggers on specified events. They can be easly accessed by writing simple VBScript macros. See the following step by step instructions.

1) Start Visual Studio.
2) Take Tools -> Macro
3) Click Options -> New File and save a new macro file by specifying a name such as “VSEvents” or something like that.
4) Click Edit button and give macroname as “HelloWorld” or something like that.
5) Now you can see a VBScript editor window for editing macros. Actually we’ve added “HelloWorld” for just starting the editor. If there are no macro inside a file, it cannot be edited.
6) Now in the editor add the following lines to the bottom

Sub Application_BeforeApplicationShutDown
	' Say ByeBye to master when he leaves!
	MsgBox "Bye Bye!"
End Sub

sub Application_BuildFinish(nNumErrors, nNumWarnings)
   ' Build Finished.
   	MsgBox "Master, Wakeup! the build is finished."
end sub

7) Save everything and exit Visual Studio.
8 ) Now Start VisualStudio and try to exit. The Visual studio says “Bye Bye!”.
9) Try to compile a project, Visual Studio will shows a message when compilation is finished!

There is a couple of other events too. They are,

Application_BeforeApplicationShutDown
Application_BeforeBuildStart
Application_BeforeDocumentClose
Debugger_BreakpointHit
Application_DocumentOpen
Application_DocumentSave
Application_NewDocument
Application_WindowActivate
Application_WindowDeactivate
Application_WorkspaceClose
Application_WorkspaceOpen

Have a look at them too. See their description in MSDN.


Since its intended for beginners, i’ve described step by step. If you are advanced, then just write a .dsm file with these macro as contents and put to <VisualStudioDir>\Common\MSDev98\Macros. Then load that macro file in Visual Studio.


Targeted Audience – Beginners.





Watch heap objects eventhough the symbol went out of scope.

25 03 2008

Icon Description
While debugging we want to watch some objects through out. An object can be watched only with its symbol and it should be valid in the current stack frame. For instance,

CJobManager* CJobManager::GetJobManager
{
    return m_pJobManager;
}

For watching the JobManager instance, we need the symbol m_JobManager in this stack frame. When the function leaves, we can’t see the JobManager instance anymore. So how can i watch these heap objects without the symbol in stack frame?

Icon How Can I Do It?
1) Get the address of your heap object. Just add to watch window and get the location. For instance assume its 0x00034de0.
2) Now add a new entry to the watch window like this – (CJobManager*)(0x00034de0)
3) Now eventhough your function returns and you loss the symbol, still you can see your heap object.

Really helpful isn’t it? i was fed up with the global objects in my project and atlast find this method to watch them premenently.





Interlocked family of functions for simple Synchronizations.

24 03 2008

Icon Description
In multithreaded environment we usually use Mutex to synchronize access to resources. But sometimes our resources may be a single integer member variable which holds some count or like that. For such light weight resource, using mutex is not preferred.

Icon How Can I Do It?
For that you can Interlocked family of functions. Please see some of the functions below.

InterlockedIncrement()
InterlockedDecrement()
InterlockedExchange()
InterlockedCompareExchange()
InterlockedExchangePointer()
…

For example if you need to increment a member variable threadsafely, you can use InterlockedIncrement(). That function will ensure the thread synchronization without any additional kernel objects such as mutex etc… Please see the code block below.

long m_lCount = 0;
...
// Increment the value.
InterlockedIncrement( &m_lCount );