对在远程桌面上运行的程序使用调试模式
我有一个专门与远程桌面连接一起使用的简短程序,该连接被设置为仅运行该程序,并且不允许任何其他访问远程计算机。 以前,程序只是退出并让连接终止,但速度非常慢,因此我编写了以下代码,以在程序运行完毕时终止远程会话。
[DllImport("wtsapi32.dll", SetLastError = true)]
static extern bool WTSLogoffSession(IntPtr hServer, int SessionId, bool bWait);
private IntPtr WTS_CURRENT_SERVER_HANDLE;
private const int WTS_CURRENT_SESSION = -1;
...
private void HardTerminalExit()
{
WTSLogoffSession(WTS_CURRENT_SERVER_HANDLE, WTS_CURRENT_SESSION, false);
}
当该程序处于其生产环境中(由使用特定 RDP 连接文件进行远程处理的人员使用)时,此功能可以正常工作。 程序运行后连接退出。 但是,在测试和调试该程序时,我的计算机每次运行后都会重新启动。
我正在寻找一种区分这些情况的好方法。 我应该设置某种远程访问并运行程序的调试脚本吗? 或者是否有某种方法可以以编程方式检测程序是否在调试模式下运行,并在这种情况下禁用退出过程?
I have a short program that is used exclusively with a remote desktop connection that's been set to only run that program and not allow any other access into the remote machine. Previously, the program just exited and let the connection terminate, but it was very slow, so I wrote the following code to terminate the remote session when the program is done running.
[DllImport("wtsapi32.dll", SetLastError = true)]
static extern bool WTSLogoffSession(IntPtr hServer, int SessionId, bool bWait);
private IntPtr WTS_CURRENT_SERVER_HANDLE;
private const int WTS_CURRENT_SESSION = -1;
...
private void HardTerminalExit()
{
WTSLogoffSession(WTS_CURRENT_SERVER_HANDLE, WTS_CURRENT_SESSION, false);
}
This works fine when this program is in its production environment, used by the people who are remoting in using a specific RDP connection file. The connection exits after the program is run. However, when testing and debugging this program, my computer restarts after every run.
I'm looking for a good way to distinguish between these cases. Should I set up some kind of debug script that remotes in and runs the program remotely? Or is there some way to programmatically detect whether the program is running in debug mode and just disable the exit procedure in that case?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用 pragma 指令:
我相信使用较新的 ConditionalAttribute 是更好的做法,但我认为您不能否定这一点,因此您需要自己为“RELEASE”设置一个环境变量:
这会告诉编译器仅当在某处声明“RELEASE”环境变量时才调用此方法 - 这可以在编译期间声明(Build 属性窗格上的条件编译符号)或作为操作系统 shell 的一部分,请参阅 ConditionalAttribute 了解更多信息。
You can use the pragma directives:
I believe it's better practice to use the newer ConditionalAttribute, but I don't think you can negate that, so you'd need to set an environment variable for "RELEASE" yourself:
This will tell the compiler to only call this method when the "RELEASE" environment variable is declared somewhere - this can declared in during compilation (Conditional compilation symbols on the Build property pane) or as part of the operating system shell, see ConditionalAttribute for more inforamation on this.