473,324 Members | 2,246 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

Using TsUserEx in C#

I need to use TsUserEx in C#. I found this code from here:
http://msdn.microsoft.com/library/de...dstsuserex.asp

I've never coded in C++ and I can't read it. Can anyone translate what
it's doing at each step? I know there are comments but they leave out
bits and don't make sense. For example, there are two comments which
read "Get the IADsTSUser interface from the user object." and yet they
describe different code. I think that's the key bit that I need to
understand. Can anyone explain it in more detail?

IADsContainer *pContainer = NULL;
IDispatch *pNewObject = NULL;
IADsTSUserEx *pTSUser = NULL;
IADsUser *pUser = NULL;
HRESULT hr = ERROR_SUCCESS;

CoInitialize(NULL);
//
// Bind to the known container.
//
hr = ADsGetObject(L"LDAP://CN=Users,DC=Server1,DC=Domain,DC=com",
IID_IADsContainer,
(void**)&pContainer);

if( !SUCCEEDED(hr)) {
wprintf(L"ADsGetObject ret failed with 0x%x\n", hr);
return FALSE;
}
//
// Create the new Active Directory Service Interfaces User object.
//
hr = pContainer->Create(L"user",
L"cn=test3",
&pNewObject);
pContainer->Release();

if( !SUCCEEDED(hr)) {
wprintf(L"Create ret failed with 0x%x\n", hr);
return FALSE;
}
//
// Get the IADsTSUser interface from the user object.
//
hr = pNewObject->QueryInterface(IID_IADsTSUserEx, (void**)&pTSUser);

if( !SUCCEEDED(hr)) {
wprintf(L"QueryInterface for IADsTSUser failed with ret 0x%x\n", hr);
return FALSE;
}
//
// Get the IADsTSUser interface from the user object.
//
hr = pNewObject->QueryInterface(IID_IADsUser, (void**)&pUser);

if( !SUCCEEDED(hr)) {
wprintf(L"QueryInterface for IAsUser failed with 0x%x\n", hr);
return FALSE;
}
pNewObject->Release();

//
// Set TerminalServicesProfilePath
//
pTSUser->put_TerminalServicesProfilePath(L"c:\\windows") ;
pTSUser->Release();

//
// Commit the object data to the directory.
//
pUser->SetInfo();
pUser->Release();
CoUninitialize();

Nov 17 '05 #1
2 4742
ssg31415926 wrote:
I've never coded in C++ and I can't read it. Can anyone translate what
it's doing at each step? I know there are comments but they leave out
bits and don't make sense. For example, there are two comments which
read "Get the IADsTSUser interface from the user object." and yet they
describe different code. I think that's the key bit that I need to
understand. Can anyone explain it in more detail?
I'll try, but I haven't done any Active Directory stuff in C++, so I can
only guess at the actual meaning of the API functions.
IADsContainer *pContainer = NULL;
IDispatch *pNewObject = NULL;
IADsTSUserEx *pTSUser = NULL;
IADsUser *pUser = NULL;
HRESULT hr = ERROR_SUCCESS;

CoInitialize(NULL);
Standard COM initialization.
//
// Bind to the known container.
//
hr = ADsGetObject(L"LDAP://CN=Users,DC=Server1,DC=Domain,DC=com",
IID_IADsContainer,
(void**)&pContainer);

if( !SUCCEEDED(hr)) {
wprintf(L"ADsGetObject ret failed with 0x%x\n", hr);
return FALSE;
}
Okay, now we got an interface (or rather a pointer to one) of an Active
Directory container in pContainer.

//
// Create the new Active Directory Service Interfaces User object.
//
hr = pContainer->Create(L"user",
L"cn=test3",
&pNewObject);
pContainer->Release();

if( !SUCCEEDED(hr)) {
wprintf(L"Create ret failed with 0x%x\n", hr);
return FALSE;
}
A new user object has been created and a pointer to it has been stored in
pNewObject, which is just an IDispatch interface.

//
// Get the IADsTSUser interface from the user object.
//
hr = pNewObject->QueryInterface(IID_IADsTSUserEx, (void**)&pTSUser);

if( !SUCCEEDED(hr)) {
wprintf(L"QueryInterface for IADsTSUser failed with ret 0x%x\n", hr);
return FALSE;
}
A pointer to an interface of type IADsTSUserEx has been queried from the
newly created user object and has been stored in pTSUser.

//
// Get the IADsTSUser interface from the user object.
//
hr = pNewObject->QueryInterface(IID_IADsUser, (void**)&pUser);

if( !SUCCEEDED(hr)) {
wprintf(L"QueryInterface for IAsUser failed with 0x%x\n", hr);
return FALSE;
}
A pointer to an interface of type IADsUser has been queried from the newly
created user object and has been stored in pUser.
pNewObject->Release();
Now we don't need the original IDispatch interface of the new user any more.
//
// Set TerminalServicesProfilePath
//
pTSUser->put_TerminalServicesProfilePath(L"c:\\windows") ;
pTSUser->Release();
The IADsTSUserEx interface reference is used to set the user's profile
path and the interface is then released.
//
// Commit the object data to the directory.
//
pUser->SetInfo();
pUser->Release();
The IADsUser interface is used to save the information and is then released.
CoUninitialize();


Standard COM uninitilization.

Hope this helps!
Oliver Sturm
--
omnibus ex nihilo ducendis sufficit unum
Spaces inserted to prevent google email destruction:
MSN oliver @ sturmnet.org Jabber sturm @ amessage.de
ICQ 27142619 http://www.sturmnet.org/blog
Nov 17 '05 #2
Thanks for your help. I've translated what you've written into my
basic understanding of C# and managed to sort it out by casting the
DirectoryEntry.NativeObject first to an IADsUser and then casting that
to an IADsTSUserEx. I've posted my solution in
microsoft.public.adsi.general under the subject "Configuring Terminal
Services attributes using .NET" in case it's of use to anyone else.

Thanks again.

SSG

Nov 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: rawCoder | last post by:
Hi All, I have a *.cer file, a public key of some one and I want to encrypt some thing using this public key. Can someone point me to a sample code for Encrypting some file using...
1
by: Mike | last post by:
When trying to compile (using Visual Web Developer 2005 Express Beta; frameworkv2.0.50215 ) the source code below I get errors (listed below due to the use of ICallBackEventHandler. Ultimately I...
10
by: Christopher Benson-Manica | last post by:
Why can't I use a class destructor in a using declaration: using MyClass::~MyClass; ? -- Christopher Benson-Manica | I *should* know what I'm talking about - if I ataru(at)cyberspace.org ...
17
by: beliavsky | last post by:
Many of my C++ programs have the line using namespace std; but the "Accelerated C++" book of Koenig and Moo has many examples where the library names are included one at a time, for example ...
8
by: Petter Reinholdtsen | last post by:
I ran into a problem on HP-UX 11.00 the other day, where it refused to compile a program using 'using namespace std;' at the top. The reason seem to be that the compiler refuses to accept 'using...
14
by: john.burton.email | last post by:
I've done some extensive searching and can't seem to find an answer to this - Is it correct to using "using" with templates, for example: using std::vector; Or do I need to specify the type...
5
by: Enos Meroka | last post by:
Hallo, I am a student doing my project in the university.. I have been trying to compile the program using HP -UX aCC compiler, however I keep on getting the following errors. ...
12
by: Calum Grant | last post by:
In older C++ computer books, you'll often see using namespace std; even in my 1996 copy of Stroustrup. Nowadays, it seems to be considered better to qualify names to make it clearer what...
3
by: JDeats | last post by:
I have some .NET 1.1 code that utilizes this technique for encrypting and decrypting a file. http://support.microsoft.com/kb/307010 In .NET 2.0 this approach is not fully supported (a .NET 2.0...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.