0

How can you detect whether the current session is a Terminal Services (Remote Desktop) session in a VB6 app?

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
CJ7
  • 22,579
  • 65
  • 193
  • 321
  • You could try forcing the window to a restored (not maximised) state on load to see if that helps. – Deanna Mar 12 '12 at 12:07
  • What do you mean exactly? `Form.WindowState` or some API call? – CJ7 Mar 12 '12 at 12:20
  • Yes, `Form.WindowState = vbNormal`. – Deanna Mar 12 '12 at 12:29
  • @Deanna: You're right. It worked. I added that line to both Form.Load and Form.Activate and the behaviour has disappeared. Put it in an answer and I'll award it. – CJ7 Mar 12 '12 at 16:15
  • But it doesn't answer your stated question about terminal services :) Cody's answer does and so should be accepted IMO. If you'd asked "My app starts up funny in TS" then maybe I would have. If anyone reads into the details to see what your actual problem was, they'll also see my comment. – Deanna Mar 13 '12 at 10:58
  • Ok, question changed. Sorry Cody! ;) – CJ7 Mar 13 '12 at 12:32
  • Yes, I could already tell the question was morphing into something else in the comment thread. I was hoping you'd take the hint and **post a new question** with your repro project, but apparently that didn't happen. Editing a question so drastically that it invalidates existing answers is considered bad practice around here; you should accept the answer you received and post a new question. I would have been happy to answer that new question, too. I don't get any rep for posting comments. – Cody Gray - on strike Mar 13 '12 at 23:56
  • OK, question has been split into two (see other: http://stackoverflow.com/q/9713285/327528). I'm awarding Cody's answer here. If Deanna transfers her answer over to the second question I'll award it there. – CJ7 Mar 15 '12 at 02:43

2 Answers2

3

Calling the GetSystemMetrics function with the SM_REMOTESESSION flag will tell you whether the app is running inside a Terminal Services session.

To call that from VB 6, you need to declare it in a module like so:

Const SM_REMOTESESSION As Long = &H1000
Declare Function GetSystemMetrics Lib "user32" (ByVal nIndex As Long) As Long

If you are running inside of a Terminal Services environment, the return value will be non-zero.

...But you should really just fix your centering code, rather than trying to work around it with different behavior depending on whether you're running inside a Terminal Services session. That's just going to make more work for you and introduce more bugs. Unfortunately, I can't tell you what's wrong with the centering code you're using without seeing it.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
  • @Craig: That's not what the answers to the linked question say. The problem is that your code to center the window is incorrect. It's making some assumptions that normally work fine, but fall apart when running under Terminal Services. – Cody Gray - on strike Mar 12 '12 at 07:07
  • @Craig: Ah, I see what you're saying. You didn't write the form centering code, you're setting the property and letting VB handle it. So the bug is in the assumptions that VB makes about screen coordinates, not in something that you can fix. Yes, I understand what it was saying about the TS Config Tool, but that doesn't change the fact that the centering code is wrong, it just allows you to work around it if you don't have access to the source code. – Cody Gray - on strike Mar 12 '12 at 09:41
  • Best would be to use `SPI_GETWORKAREA` to center your forms manually. VB ignores taskbar docking position and does not reload screen size on resolution change. Your bug is less common and not that irksome. – wqw Mar 12 '12 at 15:07
0

I had the same problem. As far as I understood, your form placed what you want where you wanted it (e.g. in the center of the screen). Your application works fine on a normal desktop, but it will be maximized in terminal environment. If so, I found a little trick. Put a timer on that form, make its interval=1 and write in timer1_timer event

Me.WindowState = 0
'then put the movement code like this
formname.Top = (Screen.Height / 2) - (formname.Height / 2) -400 '(400 for form title bar)
formname.Left = (Screen.Width / 2) - (formname.Width / 2)
timer1.interval=0
timer1.enabled=false

That's it. capanogli@hotmail.com

John Watts
  • 8,717
  • 1
  • 31
  • 35
MuraT
  • 1
  • 1
    I solved the problem by adding `Form.WindowState = vbNormal` to both the Form Load and Activate events. – CJ7 Jun 03 '12 at 03:01