如何从EnvDTE Commands.Raise方法获取输出值(customOut)?

时间:2013-10-27 16:18:53

标签: visual-studio visual-studio-2012 visual-studio-extensions

我正在编写一个Visual Studio 2012加载项,需要向内置的T-SQL编辑器发送命令。有问题的命令sqlEditorSqlDatabaseCommand可用于在编辑器中设置或获取当前数据库的名称。以下是一些设置数据库名称的(工作)代码:

const string guidSqlEditorCommandSet = "b371c497-6d81-4b13-9db8-8e3e6abad0c3";
const int sqlEditorSqlDatabaseCommand = 0x312;

object customIn = "myDatabaseName";
object customOut = null;
m_applicationObject.Commands.Raise(guidSqlEditorCommandSet, sqlEditorSqlDatabaseCommand, ref customIn, ref customOut);

问题是,我需要获取当前数据库名称,这需要使用customOut参数,而我无法弄清楚如何使customOut工作。

sqlEditorSqlDatabaseCommand的实现如下(通过Reflector):

protected override int HandleExec(uint nCmdexecopt, IntPtr pvaIn, IntPtr pvaOut)
{
    AuxiliaryDocData auxiliaryDocDataForEditor = base.GetAuxiliaryDocDataForEditor();
    if (auxiliaryDocDataForEditor != null)
    {
        QueryExecutor queryExecutor = auxiliaryDocDataForEditor.QueryExecutor;
        if (queryExecutor != null)
        {
            if (pvaIn != IntPtr.Zero)
            {
                string objectForNativeVariant = (string) Marshal.GetObjectForNativeVariant(pvaIn);
                this.SetDatabase(auxiliaryDocDataForEditor, objectForNativeVariant);
            }
            else if (pvaOut != IntPtr.Zero)
            {
                object database = string.Empty;
                IDbConnection connection = queryExecutor.ConnectionStrategy.Connection;
                if (((connection != null) && (connection.State == ConnectionState.Open)) && !string.IsNullOrEmpty(connection.Database))
                {
                    database = connection.Database;
                }
                else
                {
                    database = string.Empty;
                }
                Marshal.GetNativeVariantForObject(database, pvaOut);
            }
        }
    }
    return 0;
}

根据我见过的每个编组示例,我应该能够为customIn和customOut传递null,并且数据库名称应该放在customOut中。当我这样做时,我从Commands.Raise得到一个E_INVALIDARG异常:

System.ArgumentException: The parameter is incorrect. (Exception from HRESULT: 0x80070057 (E_INVALIDARG))
    at EnvDTE.Commands.Raise(String Guid, Int32 ID, Object& CustomIn, Object& CustomOut)
    at RDSVisualStudioAddIn.Exec(String commandName, vsCommandExecOption executeOption, Object& varIn, Object& varOut, Boolean& handled) in c:\RDS\RDSVisualStudioAddIn\Connect.cs:line 193

有没有人将Commands.Raise与customOut参数一起使用?

0 个答案:

没有答案