Code Template add-in for Visual C++ 5.0

The CodeTmpl add-in provides a simple mechanism for inserting commonly used text blocks into your source code.

Install

Build the CodeTmpl project using the Win32 MinDependency build configuration, and copy the CodeTmpl.txt file into your DevStudio\SharedIDE directory. Install the CodeTmpl add-in by selecting Customize from the Tools Menu and browsing for the DLL on the 'Add-ins and Macro files' tab. The add-in should appear as 'CTAddIn Class' in the list of active add-ins. Once installed a new toolbar with a single button should appear for the CodeTmpl add-in.

Overview

So what does this thing do for me you're wondering. Well the CodeTmpl.txt that you copied into your SharedIDE directory contains chunks of source code delimited into named blocks. When you click on the CodeTmpl toolbar button you'll see those same names appear on a popup menu, and should you select one, then the appropriate chunk of source code will be glued into your current source code document at the insertion point.

Configuration

Clearly my idea of useful bits of code is probably not yours, so the CodeTmpl add-in is completely configurable, by simply editting the CodeTmpl.txt file.

The format of the CodeTmpl.txt file is extremely simple. Basically you paste your favourite blocks of source code into the file and put a pair of 'tags' at the beginning and end of each block. The tag '#{' goes at the beginning of the block and the tag '#}' goes at the end. The '#{' tag should be followed by a name for that block that will appear on the popup menu. The tags must appear at the beginning of a new line. Any text outside of the tags is ignored, with the exception of a string of 2 or more hashes which cause a separator to appear on the pop up menu.

For example :


#{Hello World - Console
#include 

int main()
{
    puts("Hello, World");
}
#}

##########################

#{Hello World - GUI
#include 

int PASCAL WinMain(HANDLE hInstance,
                   HANDLE hPrevInstance,
                   LPSTR lpszCommandLine,
                   int cmdShow)
{
    MessageBox(NULL, "Hello, World", "Example", MB_OK);
}
#}

If your CodeTmpl.txt contained the above text then clicking the CodeTmpl toolbar button would display a popup menu containing the options 'Hello World - Console' and 'Hello World - GUI', with a separator between them. Selecting 'Hello World - Console' would paste the text shown between the '#{' and '#}' tags into your source.

Tips

If you click the CodeTmpl toolbar button with the 'Control' key down, then the CodeTmpl.txt is re-read by the add-in before the popup menu is displayed. This is to allow for on the fly configuration changes.

When editting the CodeTmpl.txt file in DevStudio, right hand mouse click the window in which it is open, select Properties from the context menu and set Language to C/C++ from the property dialog. This will give you color coding and make the tab key do what is ought to.

Contact me

I hope you will find this add-in useful either for its intended purpose or just as a source code example. Either way feel free to e-mail me with any comments, suggestions etc. Darren Richards.


Comments

  • Help Cannot compile CodeTemplate - causes Computer restart.

    Posted by Legacy on 02/11/2003 12:00am

    Originally posted by: Gargoyle

    Hi 
    
    I am trying to taech myself how to write addins to cover my needs in Visual C++. So when I saw this code I thought great a peice of code to examing and to try my modifications on to see what I could do.
    So downloaded project and tried to build project (MinDependency Build) as said in project read me.
    However this causes my system to blank out and restart!
    What's going on can anyone help.

    Thanks

    Gargoyle.

    Reply
  • Has anyone got this add-in work in VC 6?

    Posted by Legacy on 07/21/2002 12:00am

    Originally posted by: vcHelpBoy

    Hi, I just started to learn programming a couple of weeks ago. I tried many times to apply this add-in WITHOUT success. Could anybody tell me how to get it work?

    Here are my steps for doing this (please correct me if I am doing wrong):
    1) Build under mode "Win32 Release MinDependency";
    2) Move the "CodeTmpl.dll" and "CodeTmpl.txt" to the folder
    "C:\Program Files\Microsoft Visual Studio\Common\MSDev98\AddIns";
    3) Install the add-in: Tools -> Customize -> Addins and Macro Files (Tab) -> CTAddIn Class;
    4) Then it puts a Toolbar in the editor.

    However,

    when I opened a .cpp file and Ctrl + click the toolbar, nothing happened.

    Please help me if you have a couple of minutes.

    Thanks!

    vcHelpBoy

    Reply
  • Has anyone ported this to VS.NET yet, as the V5/6 version doesn't seem to work on VS.NET

    Posted by Legacy on 05/09/2002 12:00am

    Originally posted by: Richard Blundell

    The dll addin won't load in VS.NET

    Reply
  • It doesn't work, when both VC5 & VC6 installed

    Posted by Legacy on 01/31/2001 12:00am

    Originally posted by: Loki

    %subj% because MSDevDir in that case looks like this:
    MSDevDir=C:\Program Files\Microsoft Visual Studio\Common\MSDev98;C:\Program Files\DevStudio\SharedIDE

    I think, it very easy to fix =)

    Reply
  • very good

    Posted by Legacy on 11/08/2000 12:00am

    Originally posted by: eom, hee jung

    Thanks^^

    Reply
  • Merge all, please...

    Posted by Legacy on 08/22/1999 12:00am

    Originally posted by: Paul Selormey

    Hello Darren Richards,
    It is rather unfortunate for the good people who extended this addin to submit them separately. But please can you take the pains to merge most of these new features for us?

    Best regards,
    Paul.

    Reply
  • A change which allows the configuration file to be in the same dir as the DLL (version 6.0)

    Posted by Legacy on 06/29/1999 12:00am

    Originally posted by: Mark Johnston

    This change works for Visual C++ 6.0.  Something like it 
    
    might also work for previous versions, depending upon how
    Add-Ins are registered.

    My goal in making this change was to be able to keep the
    CodeTmpl.txt file in the same directory as the Add-In DLL.
    In Visual C++ 6.0, the path and file name of the Add-In DLL
    is stored in the registry when the Add-In is encountered by
    the system. This code looks for that entry in the registry
    and replaces the DLL file name with the configuration file
    name.

    Also, Visual Studio 6.0 has a default directory to find
    Add-In DLLs (<Visual Studio Install Dir>\Common\MSDev98\AddIns).
    If you copy both files to that directory the Add-In will
    appear automagically when you open the Tools->Customize
    Add-Ins and Macros tab.

    Thank you for sharing this great idea.

    Mark Johnston

    void CCTAddIn::ReadTmplFile()
    {
    TemplateData td; // buffer for current template

    enum { PS_Tag, PS_Template } eState = PS_Tag; // parsing state

    //
    // compose the path for the code template file, which
    // lives in the directory where the Add-In DLL file is
    // installed.
    //

    // Get DLL name from registry entry for this Add-In
    HKEY hKey;
    LONG result;
    string strTmplFile = "";

    // open Add-In registry key for read access
    result = RegOpenKeyEx(HKEY_CURRENT_USER, // Key handle at root level.
    "Software\\Microsoft\\DevStudio\\6.0\\AddIns\\CTAddin.CTAddin.1", // Path name of child key.
    0, // Reserved.
    KEY_EXECUTE, // Requesting read access.
    &hKey); // Address of key to be returned.

    if (result == ERROR_SUCCESS)
    { char szBuf[MAX_PATH];
    DWORD dwLen=MAX_PATH;

    // read registry value called "Filename" (this is the DLL name)
    result = RegQueryValueEx(hKey, // Handle to open registry key
    "Filename", // Name of value to query
    NULL, // Reserved
    NULL, // Address of buffer which holds value type
    (unsigned char*) szBuf, // Address of buffer which holds returned value
    &dwLen); // Length of buffer

    if (result == ERROR_SUCCESS)
    // replace DLL name with template file name in file name string
    { strTmplFile = szBuf;
    strTmplFile = strTmplFile.substr(0, strTmplFile.find_last_of("\\")) + "\\CodeTmpl.txt";
    }

    // close registry key
    RegCloseKey(hKey);
    hKey = NULL;
    }

    if (result != ERROR_SUCCESS)
    // display an error message
    { LPVOID lpMsgBuf;

    FormatMessage(
    FORMAT_MESSAGE_ALLOCATE_BUFFER |
    FORMAT_MESSAGE_FROM_SYSTEM |
    FORMAT_MESSAGE_IGNORE_INSERTS,
    NULL,
    result,
    MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
    (LPTSTR) &lpMsgBuf,
    0,
    NULL );
    // Display the string.
    MessageBox( NULL, (LPCTSTR)lpMsgBuf, "CTAddIn: Error", MB_OK | MB_ICONINFORMATION );
    // Free the buffer.
    LocalFree( lpMsgBuf );
    }


    // Suggestion #1:
    // -------------
    // GetSystemDirectory(szBuf, dwLen);
    // strcat(szBuf, "\\CodeTmpl.txt");

    // Original:
    // --------
    // string strMSDevDir = getenv("MSDevDir");
    // string strTmplFile = strMSDevDir + "\\CodeTmpl.txt";

    Reply
  • Great work!

    Posted by Legacy on 04/01/1999 12:00am

    Originally posted by: Tom

    A great way to standardize development. Thanks!

    Reply
  • A good to for Autoduck

    Posted by Legacy on 11/11/1998 12:00am

    Originally posted by: Rosa Bernardez


    I find this code template tool extremely useful in using
    the Microsoft Autoduck autodocumentation utility.

    thanks.

    This is my new CodeTmpl.txt >>

    CodeTmpl.txt
    ------------

    #############################################################################

    AutoDuck elements
    -----------------------

    #{AutoDuck m�dulo
    /* @doc
    *
    * @module <file.CPP> - <Title |
    *
    *
    * <Description>
    *
    */

    #}

    #{AutoDuck global variable
    //@globalv <Description>
    #}

    #{AutoDuck internal doc
    /* @doc INTERNAL
    */
    #}

    #{AutoDuck function
    /*
    * @func <Function description>
    * @parm <type> | <name> | <Description>
    *
    * @rdesc <return type Description>
    * @rvalue <return value> | <Description>
    */
    #}

    #############################################################################

    #{AutoDuck member funtion
    /*
    * @mfunc <return_type> | <class_name> | <function_name> | <description>
    * @parm <type> | <name> | <Description>
    *
    * @rdesc <type> | <Description>
    * @rvalue <return value> | <Description>
    */
    #}


    #{AutoDuck data member
    //@cmember <Description>
    #}

    #############################################################################

    #{AutoDuck constant
    //@CONST <constant>
    #}

    #{AutoDuck enum type
    //@enum <enumeration_name> | <description>
    #}

    #{AutoDuck enum value
    //@emem <name> | <description>
    #}

    #############################################################################

    #{AutoDuck consume interfaz
    //@CONSUMES <consumed interfaz>
    #}

    #{AutoDuck To do
    //@todo <describe what to do>
    #}

    #{AutoDuck development note
    //@devnote <write your development note>
    #}

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

  • In support of their business continuity and disaster recovery plans, many midsized companies endeavor to avoid putting all their eggs in one basket. Understanding the critical role of last-mile connectivity and always available Internet access for their enterprises, savvy firms utilize redundant connections from multiple service providers. Despite the good intentions, their Internet connectivity risk may still be in a single basket. That is because internet service providers (ISPs) and competitive local …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds