Discussion:
Configuring Terminal Services attributes using .NET
(too old to reply)
ssg31415926
2005-08-29 17:52:58 UTC
Permalink
I need to configure Terminal Services settings in a C# program I'm
working on. System.DirectoryServices doesn't seem to allow this. I've
found a reference to IADsTSUserEx on MSDN but the page says I need to
"call the IADs::GetInfo method or the IADs::GetInfoEx method to load
the property values of the ADSI object from the underlying directory
store into the property cache". So far, I've managed to avoid Interop
and I'm not sure what to do.

Initially, I've created a reference in my project to the "tsexusrm 1.0
Type Library" at C:\Windows\system32\tsuserex.dll and I tried this
bare-minimum code:

public string TerminalServicesProfilePath {
get { TSUSEREXLib.ADsTSUserExClass tsUser =
(TSUSEREXLib.ADsTSUserExClass) this.NativeDSObject;
return tsUser.TerminalServicesProfilePath;
}
}

but it's an invalid cast. I'm guessing I've got to cast
this.NativeDSObject as a standard ADSI object first but I'm not sure
how ADsTSUserExClass will 'link' to it. Any ideas?

Note: The above code is in a class which derives from a class which
holds a reference to a DirectoryEntry where this.NativeDSObject is
defined as:

protected object NativeADObject {
get { return this.m_DirectoryEntry.NativeObject; }
}
ssg31415926
2005-08-30 09:47:47 UTC
Permalink
Post by ssg31415926
I need to configure Terminal Services settings in a C# program I'm
working on. System.DirectoryServices doesn't seem to allow this. I've
found a reference to IADsTSUserEx on MSDN but the page says I need to
"call the IADs::GetInfo method or the IADs::GetInfoEx method to load
the property values of the ADSI object from the underlying directory
store into the property cache". So far, I've managed to avoid Interop
and I'm not sure what to do.
Initially, I've created a reference in my project to the "tsexusrm 1.0
Type Library" at C:\Windows\system32\tsuserex.dll and I tried this
I've solved this one and so I'm posting the code because I found a lot
of people asking how in various places.

private TSUSEREXLib.IADsTSUserEx m_TsUser;

protected void InitialiseTS() {
if (this.m_TsUser == null) {
ActiveDs.IADsUser iADsUser = (ActiveDs.IADsUser) this.NativeADObject;
m_TsUser = (TSUSEREXLib.IADsTSUserEx) iADsUser;
}
}

public string TerminalServicesProfilePath {
get {
InitialiseTS();
return this.m_TsUser.TerminalServicesProfilePath;
}
set {
InitialiseTS();
this.m_TsUser.TerminalServicesProfilePath = value;
this.m_PropertyChanged = true;
}
}

public TerminalServicesFunction TerminalServicesAllowLogon {
get {
InitialiseTS();
return (TerminalServicesFunction) this.m_TsUser.AllowLogon;
}
set {
InitialiseTS();
this.m_TsUser.AllowLogon = (int) value;
this.m_PropertyChanged = true;
}
}

public TerminalServicesBrokenConnectionActions
TerminalServicesBrokenConnectionAction {
get {
InitialiseTS();
return (TerminalServicesBrokenConnectionActions)
this.m_TsUser.BrokenConnectionAction;
}
set {
InitialiseTS();
this.m_TsUser.BrokenConnectionAction = (int) value;
this.m_PropertyChanged = true;
}
}

public int TerminalServicesMaxConnectionTime {
get {
InitialiseTS();
return this.m_TsUser.MaxConnectionTime;
}
set {
InitialiseTS();
this.m_TsUser.MaxConnectionTime = value;
this.m_PropertyChanged = true;
}
}

public enum TerminalServicesFunction {
Disabled = 0,
Enabled = 1
}

public enum TerminalServicesBrokenConnectionActions {
DisconnectFromSession = 0,
EndSession = 1
}

public enum TerminalServicesReconnectionActions {
FromAnyClient = 0,
FromOriginatingClientOnly = 1
}

with similar code to TerminalServicesProfilePath for
TerminalServicesHomeDirectory, TerminalServicesHomeDrive,
TerminalServicesWorkDirectory, TerminalServicesInitialProgram;
and similar code to TerminalServicesBrokenConnectionAction for
ReconnectionAction;
and similar code to TerminalServicesAllowLogon for
ConnectClientDrivesAtLogon, ConnectClientPrintersAtLogon,
DefaultToMainPrinter and EnableRemoteControl;
and similar code to TerminalServicesMaxConnectionTime for
MaxDisconnectionTime and MaxIdleTime.
Note, for these last three: The value is stored in Minutes. The
interface contains the following standard values: Never (mapped to 0),
1 minute, 5 minutes, 10 minutes, 15 minutes, 30 minutes, 1 hour, 2
hours, 3 hours, 1 day, and 2 days. Interrim values set by this routine
are accepted and are mapped by the display into a readable format.
E.g. 14400 is mapped to 10 days, 1500 is mapped to 1 day 1 hour and
1502 is mapped to 1 day 1 hour 2 minutes. I cannot vouch for how
accepted this is by Microsoft. I have done this by working it out, not
by reading.

I would appreciate comments as to the 'correctness' of this code, any
mistakes, erroneous assumptions or simplifications that could be
applied. I'm still new to C# and OO and know I have a lot to learn.

SSG
ssg31415926
2005-08-30 12:08:19 UTC
Permalink
Whoops! Further reading shows that EnableRemoteControl is a little
more complex, so I replaced the TerminalServicesEnableRemoteControl
property with this:

public TerminalServicesEnableRemoteControlActions
TerminalServicesEnableRemoteControl {
get {
InitialiseTS();
return (TerminalServicesEnableRemoteControlActions)
this.m_TsUser.EnableRemoteControl;
}
set {
InitialiseTS();
this.m_TsUser.EnableRemoteControl = (int) value;
this.m_PropertyChanged = true;
}
}

and added this:

public enum TerminalServicesEnableRemoteControlActions {
Disable = 0,
EnableInputNotify = 1,
EnableInputNoNotify = 2,
EnableNoInputNotify = 3,
EnableNoInputNoNotify =4
}

where 0 (Disable) = Enable Remote Control UNchecked
1 (EnableInputNotify) = Enable Remote Control checked,
Require user's permission checked and
Level of control = Interact with the session.
2 (EnableInputNoNotify) = Enable Remote Control checked,
Require user's permission UNchecked and
Level of control = Interact with the session.
3 (EnableNoInputNotify) = Enable Remote Control checked,
Require user's permission checked and
Level of control = View the user's session.
4 (EnableNoInputNoNotify) = Enable Remote Control checked,
Require user's permission UNchecked and
Level of control = View the user's session.

Continue reading on narkive:
Loading...