8

Solution->Properties の下で、複数のスタートアップ プロジェクトを設定できます。 ソリューションのプロパティ

( EnvDTE: を使用してsolution.SolutionBuild.StartupProjects)「開始」とマークされたプロジェクトのリストを取得できることはわかっていますが、アクションが「デバッグなしで開始」であるプロジェクトのリストを取得するにはどうすればよいですか? それらはリストに表示されません。

4

1 に答える 1

7

これが文書化されていて公式に入手できるとは思いませんが、ここにいくつかの情報があります:

  • これは、Visual Studio 組み込みパッケージによってソリューションの .SUO ファイルに保存されます。SUO ファイルには、OLE 複合ストレージ形式があります。OpenMCDFなどのツールを使用して参照できます(エクスプローラーのサンプルがあります)。このファイルには、' SolutionConfiguration' という名前のストリームがあり、dwStartupOpt トークンの後に探している情報が続きます。ストリーム自体にはカスタム バイナリ形式があります。

  • IVsPersistSolutionProps Interfaceを介して VS 内から同じ情報を入手できます。読み込まれたパッケージの 1 つからそれへのポインターを取得する必要があります (たとえば、IVsShell.GetPackageEnum メソッドSolutionConfigurationを使用してパッケージのリストを列挙します。1 つのパッケージは、' ' ストリームで IVsPersistSolutionProps インターフェイスをサポートします。

ただし、どの方法を選択しても、「SolutionConfiguration」ストリームを手動で解析することになります。ここでは、SUO ファイルを開き、「手動で」ビットをハッキングするだけで、VS の外部で機能する方法を紹介します。

「SolutionConfiguration」ストリームを解析するユーティリティ クラスは次のとおりです。

public sealed class StartupOptions
{
    private StartupOptions()
    {
    }

    public static IDictionary<Guid, int> ReadStartupOptions(string filePath)
    {
        if (filePath == null)
            throw new ArgumentNullException("filePath");

        // look for this token in the file
        const string token = "dwStartupOpt\0=";
        byte[] tokenBytes = Encoding.Unicode.GetBytes(token);
        Dictionary<Guid, int> dic = new Dictionary<Guid, int>();
        byte[] bytes;
        using (MemoryStream stream = new MemoryStream())
        {
            CompoundFileUtilities.ExtractStream(filePath, "SolutionConfiguration", stream);
            bytes = stream.ToArray();
        }

        int i = 0;
        do
        {
            bool found = true;
            for (int j = 0; j < tokenBytes.Length; j++)
            {
                if (bytes[i + j] != tokenBytes[j])
                {
                    found = false;
                    break;
                }
            }
            if (found)
            {
                // back read the corresponding project guid
                // guid is formatted as {guid}
                // len to read is Guid length* 2 and there are two offset bytes between guid and startup options token
                byte[] guidBytes = new byte[38 * 2];
                Array.Copy(bytes, i - guidBytes.Length - 2, guidBytes, 0, guidBytes.Length);
                Guid guid = new Guid(Encoding.Unicode.GetString(guidBytes));

                // skip VT_I4
                int options = BitConverter.ToInt32(bytes, i + tokenBytes.Length + 2);
                dic[guid] = options;
            }
            i++;
        }
        while (i < bytes.Length);
        return dic;
    }
}

小さな複合ストリーム読み取りユーティリティが続きます (外部ライブラリは必要ありません):

public static class CompoundFileUtilities
{
    public static void ExtractStream(string filePath, string streamName, string streamPath)
    {
        if (filePath == null)
            throw new ArgumentNullException("filePath");

        if (streamName == null)
            throw new ArgumentNullException("streamName");

        if (streamPath == null)
            throw new ArgumentNullException("streamPath");

        using (FileStream output = new FileStream(streamPath, FileMode.Create))
        {
            ExtractStream(filePath, streamName, output);
        }
    }

    public static void ExtractStream(string filePath, string streamName, Stream output)
    {
        if (filePath == null)
            throw new ArgumentNullException("filePath");

        if (streamName == null)
            throw new ArgumentNullException("streamName");

        if (output == null)
            throw new ArgumentNullException("output");

        IStorage storage;
        int hr = StgOpenStorage(filePath, null, STGM.READ | STGM.SHARE_DENY_WRITE, IntPtr.Zero, 0, out storage);
        if (hr != 0)
            throw new Win32Exception(hr);

        try
        {
            IStream stream;
            hr = storage.OpenStream(streamName, IntPtr.Zero, STGM.READ | STGM.SHARE_EXCLUSIVE, 0, out stream);
            if (hr != 0)
                throw new Win32Exception(hr);

            int read = 0;
            IntPtr readPtr = Marshal.AllocHGlobal(Marshal.SizeOf(read));
            try
            {
                byte[] bytes = new byte[0x1000];
                do
                {
                    stream.Read(bytes, bytes.Length, readPtr);
                    read = Marshal.ReadInt32(readPtr);
                    if (read == 0)
                        break;

                    output.Write(bytes, 0, read);
                }
                while(true);
            }
            finally
            {
                Marshal.FreeHGlobal(readPtr);
                Marshal.ReleaseComObject(stream);
            }
        }
        finally
        {
            Marshal.ReleaseComObject(storage);
        }
    }

    [ComImport, Guid("0000000b-0000-0000-C000-000000000046"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    private interface IStorage
    {
        void Unimplemented0();

        [PreserveSig]
        int OpenStream([MarshalAs(UnmanagedType.LPWStr)] string pwcsName, IntPtr reserved1, STGM grfMode, uint reserved2, out IStream ppstm);

        // other methods not declared for simplicity
    }

    [Flags]
    private enum STGM
    {
        READ = 0x00000000,
        SHARE_DENY_WRITE = 0x00000020,
        SHARE_EXCLUSIVE = 0x00000010,
        // other values not declared for simplicity
    }

    [DllImport("ole32.dll")]
    private static extern int StgOpenStorage([MarshalAs(UnmanagedType.LPWStr)] string pwcsName, IStorage pstgPriority, STGM grfMode, IntPtr snbExclude, uint reserved, out IStorage ppstgOpen);
}

また、スタートアップ オプションに関連付けられたプロジェクト GUID を表示するサンプル:

static void SafeMain(string[] args)
{
    foreach (var kvp in StartupOptions.ReadStartupOptions("mySample.suo"))
    {
        if ((kvp.Value & 1) != 0)
        {
            Console.WriteLine("Project " + kvp.Key + " has option Start");
        }
        if ((kvp.Value & 2) != 0)
        {
            Console.WriteLine("Project " + kvp.Key + " has option Start with debugging");
        }
    }
}
于 2012-02-12T19:33:09.817 に答える