Partager via


VsDebugTargetInfo2.bstrEnv Field

BSTR containing the environment settings (DLO_CreateProcess).

Namespace:  Microsoft.VisualStudio.Shell.Interop
Assembly:  Microsoft.VisualStudio.Shell.Interop.8.0 (in Microsoft.VisualStudio.Shell.Interop.8.0.dll)

Syntax

'Declaration
Public bstrEnv As String
public string bstrEnv
public:
String^ bstrEnv
val mutable bstrEnv: string
public var bstrEnv : String

Remarks

This field is used to set custom environment variables. Note that bstrEnv should be a null-terminated block of null-terminated strings. You must also pass the DBGLAUNCH_MergeEnv in the LaunchFlags to specify that you want the default system environment variables to be merged with what you are specifying. For more information, see the section about lpEnvironment in the topic CreateProcess.

The following is an example of setting this field correctly.

void LaunchMyProcessesUnderDebugger()
{
    processes = new Process[numberOfHostInstances];
    VsDebugTargetInfo3[] debugTargetInfos = new VsDebugTargetInfo3[numberOfHostInstances];
    string argumentTemplate = "-arg1 \"{0}\" -arg {1} -arg3 {2}";
    for (int i = 0; i < count; i++)
    {
        string workingDirectory = ...;
        string arguments = string.Format(argumentTemplate, val1, val2, val3);

        debugTargetInfos[i] = new VsDebugTargetInfo3();....// create process; we don't already have a process to attach to
        // create process; we don't already have a process to attach to

        debugTargetInfos[i].dlo = (uint)DEBUG_LAUNCH_OPERATION.DLO_CreateProcess;
        // attach managed debugger
        debugTargetInfos[i].guidLaunchDebugEngine = VSConstants.CLSID_ComPlusOnlyDebugEngine;
        // full path to an exe
        debugTargetInfos[i].bstrExe = debugTargetInfos[i].bstrCurDir = workingDirectory;
        debugTargetInfos[i].bstrArg = arguments;
        debugTargetInfos[i].pStartupInfo = IntPtr.Zero;

        Dictionary<string, string> environmentVariables = new Dictionary<string, string>();
        environmentVariables.Add(CustomEnvVar, EnvVarValue);
        // custom environment variables
        vdebugTargetInfos[i].bstrEnv = GetEnvironmentString(environmentVariables);
    }
    // Merge default environment variables with custom ones above.
    debugTargetInfos[i].LaunchFlags = (uint)__VSDBGLAUNCHFLAGS2.DBGLAUNCH_MergeEnv;....}
    VsDebugTargetProcessInfo[] tpi = new VsDebugTargetProcessInfo[numberOfHostInstances];
    int hr = debugger.LaunchDebugTargets3((uint)numberOfHostInstances, debugTargetInfos, tpi);
    Marshal.ThrowExceptionForHR(hr);

    for (int i = 0; i < count; i++)
    {
        processes[i] = Process.GetProcessById((int)tpi[i].dwProcessId);
    }
}

private static string GetEnvironmentString(IDictionary<string, string> environment)
{
    if (environment == null || environment.Count == 0)
    {
        return null;
    }
// Collect all the variables as a null delimited list of key=value pairs.
    StringBuilder result = new StringBuilder();
    foreach (var pair in environment)
    {
        result.Append(pair.Key);
        result.Append('=');
        result.Append(pair.Value);
        result.Append('\0');
    }
    // Add a final list-terminating null character. This is sent to native code as a BSTR and no null is added automatically. But the format of the data requires that this be a null-delimited, null-terminated list.
    result.Append('\0');
    return result.ToString();
} 

.NET Framework Security

See Also

Reference

VsDebugTargetInfo2 Structure

Microsoft.VisualStudio.Shell.Interop Namespace