Polytope
Polytope

Reputation: 13

Unresponsive IVsSingleFileGenerator in a C# Visual Studio project

I've tried to follow an online tutorial to learn how to create and register Visual Studio file generators (aka custom tools). I believe I've been able to register the tool with at least partial success, since in a different Visual Studio project, I can assign 'VsTools' (the namespace of the file generator's class) as a .txt file's "Custom Tool" in the .txt file's properties dialog and then, upon running the tool, a distinct file is generated. However, although I expect the generated file to display a number (specifically, the number of lines in the file to which the custom tool is applied), the generated file has no content at all.

Can anyone help me to understand how I may have incorrectly registered the file generator or incorrectly implemented the custom logic used to get a source file's line count?

The IVsSingleFileGenerator implementation code follows:

namespace VsTools
{
    [Guid("A2...")]
    public class ToolBase : IVsSingleFileGenerator
    {
        public int DefaultExtension(out string InputfileRqExtension)
        {
            InputfileRqExtension = ".txt";
            return VSConstants.S_OK;
        }

        public int Generate(
            string inputFilePath,
            string inputFileContents,
            string defaultNamespace,
            IntPtr[] outputFileContents,
            out uint outputByteCount,
            IVsGeneratorProgress generateProgress)
        {
            int lineCount = inputFileContents.Split('\n').Length;
            byte[] bytes = Encoding.UTF8.GetBytes(lineCount.ToString());
            int length = bytes.Length;
            outputFileContents[0] = Marshal.AllocCoTaskMem(length);
            Marshal.Copy(bytes, 0, outputFileContents[0], length);
            outputByteCount = (uint)length;
            return VSConstants.S_OK;
        }
    }
}

The registry file used to register the "VsTools.ToolBase" file generator:

Windows Registry Editor Version 5.00

[HKEY_CURRENT_USER\SOFTWARE\Microsoft\VisualStudio\14.0_Config\CLSID\{A2..}]

"InprocServer32"="C:\\Windows\\System32\\mscoree.dll"
"ThreadingModel"="Both"
"Class"="VsTools2.ToolBase"
"Assembly"="VsTools, Version=1.0.0.0, Culture=neutral, PublicKeyToken=e9b..."

"CodeBase"=file:///C:\\VsTools\\VsTools.dll

[HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\14.0_Config\Generators\{FAE04EC1-301F-11D3-BF4B-00C04F79EFBC}\VsTools]

"CLSID"="{A2..}"
"GeneratesDesignTimeSource"=dword:00000001

The following commands were run:

regasm /codebase "C:\VsTools\VsTools.dll"
gacutil /i "C:\VsTools\VsTools.dll"

Upvotes: 0

Views: 186

Answers (0)

Related Questions