VsDebugTargetInfo2.bstrEnv 字段
包含环境设置 (DLO_CreateProcess) 的 BSTR。
命名空间: Microsoft.VisualStudio.Shell.Interop
程序集: Microsoft.VisualStudio.Shell.Interop.8.0(在 Microsoft.VisualStudio.Shell.Interop.8.0.dll 中)
语法
声明
Public bstrEnv As String
public string bstrEnv
备注
此字段用于设置自定义环境变量。请注意应为 null 终止的 bstrEnv 块 null 终止的字符串。您还必须通过在 LaunchFlags 的 DBGLAUNCH_MergeEnv 指定要默认系统环境变量什么将您指定。有关更多信息,请参见有关 lpEnvironment 的部分在主题 CreateProcess。
下面是正确设置此字段的示例。
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 安全性
- 对直接调用方的完全信任。此成员不能由部分信任的代码使用。有关更多信息,请参见通过部分受信任的代码使用库。