Understanding of Chapter 3.3.4 Terminal Service namespace in Windows core programming 5th

Source: Internet
Author: User

A long time ago, when I saw this section, I felt confused and semi-informed, because there were too many unfamiliar concepts. At that time, I also found some materials and blogs on the Internet. I felt that the help was not great, so I skipped it. Today, the tolerance shoes on the Forum suddenly asked me how to understand this place, so I decided to read and verify my ideas.


The main original words in the book:

On a computer that is running Terminal Services, there are multiple namespaces used for kernel objects. One of them is the global namespace. All kernel objects accessible by clients should be placed in this namespace. When two or more sessions are running the same application, this arrangement can avoid mutual interference between sessions-one session will not access the objects of another session, even if the object name is the same (key point). (The concept of session is abstract. I have not found a suitable description. In my personal opinion, session is similar to a domain, and there are a bunch of processes in it .)

Service developers must run in a session different from the client application, which affects the naming conventions of shared kernel objects. Any object that you want to share with your application must be created in the global namespace. Fast User Switching also causes similar problems. We know that different users can log on to different sessions and start their own user applications by using the quick user switching function. If a service we write needs to communicate with these applications, it cannot be assumed that it runs in the same session as the user application.


For the above two paragraphs, I wrote several lines of code in the Win7 environment and made a simple test.

FirstAdmin account (one session)In the program:

HANDLE hMutex = CreateMutex(NULL,FALSE,TEXT("Global\\Handle")) ; 

Create a mutex object in the global namespace and call the Sleep (INFINITE) suspension program to keep the kernel objects unchanged.

Copy my application to the folder of the guest account and switchGuest Account (another session)And run this program. If the mutex creation fails, CreateMutex returns NULL and ErrorCode is ERROR_ALREADY_EXISTS.

Then, I changed the above Code:

HANDLE hMutex = CreateMutex(NULL,FALSE,TEXT("Local\\Handle")) ;

Then, execute the same operation as above and find that in the guest account, this program can also successfully create this kernel object with the same name.


That is to say, my idea is actually a namespace problem. Well, I don't know what I want to express, I feel like talking nonsense .......

Test code:

# Include <windows. h >#include <cstdio >#include <iostream> int main (void) {// create a kernel object in the global namespace and run the program with different accounts, you cannot create a kernel object with the same name. // HANDLE hMutex = CreateMutex (NULL, FALSE, TEXT ("Global \ Handle"); // create a kernel object in a local namespace and run the program with different accounts, you can create a kernel object with the same name. // HANDLE hMutex = CreateMutex (NULL, FALSE, TEXT ("Local \ Handle"); HANDLE hMutex = CreateMutex (NULL, FALSE, TEXT ("Handle ")); // same as above // GetLastError should be used in the local namespace test example. In the global namespace test example, determine whether hMutex is NULLif (NULL = hMutex | GetLastError () = ERROR_ALREADY_EXISTS) {printf ("mutex object \ n"); return 0;} printf ("mutex object created successfully \ n"); Sleep (INFINITE ); return 0 ;}





Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.