Thread ID

How do i get the threadID for an other running window?
LVL 2
telebasicsAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jkrCommented:
To enumerate all users, use

#include <windows.h>
#include <lm.h>
#include <stdio.h>
#pragma hdrstop



// 28jul97 felixk   fixed a nasty bug in do_NetQueryDisplayInformation
//                  (next_index was set even if read == 0: accvio)


void print_user( const wchar_t *name, DWORD flags )
{
      // NOTE: capital "S" in a printf format string means "Unicode",
      // if we are an ANSI program

      printf( "%-40.40S ", name ); // display Unicode user name
      if ( flags & UF_NORMAL_ACCOUNT )
            puts( "(domain user)" );
      else if ( flags & UF_TEMP_DUPLICATE_ACCOUNT )
            puts( "(local user)" );
      else if ( flags & UF_WORKSTATION_TRUST_ACCOUNT )
            puts( "(computer account)" );
      else if ( flags & UF_SERVER_TRUST_ACCOUNT )
            puts( "(PDC/BDC computer account)" );
      else if ( flags & UF_INTERDOMAIN_TRUST_ACCOUNT )
            puts( "(trusted domain account)" );
      else
            puts( "(funny account type)" );
}



void do_NetUserEnum( const wchar_t *server )
{
      USER_INFO_1 *buf, *cur;
      DWORD read, totalremaining, resumeh, rc, i;

      resumeh = 0;
      do
      {
            buf = NULL;
            rc = NetUserEnum( server, 1, 0, (BYTE **) &buf, 1024, &read, &totalremaining, &resumeh );
            if ( rc != ERROR_MORE_DATA && rc != ERROR_SUCCESS )
                  break;

            printf( "\ngot %lu entries out of %lu\n", read, totalremaining );
            for ( i = 0, cur = buf; i < read; ++ i, ++ cur )
                  print_user( cur->usri1_name, cur->usri1_flags );

            if ( buf != NULL )
                  NetApiBufferFree( buf );

      } while ( rc == ERROR_MORE_DATA );

      if ( rc != ERROR_SUCCESS )
            printf( "NUE() returned %lu\n", rc );
}



void do_NetQueryDisplayInformation( const wchar_t *server, int level )
{
      void *buf;
      NET_DISPLAY_USER *ndu;
      NET_DISPLAY_MACHINE *ndm;
      NET_DISPLAY_GROUP *ndg;
      DWORD read, next_index, rc, i;

      next_index = 0;
      do
      {
            buf = NULL;
            rc = NetQueryDisplayInformation( server, level, next_index, 10, 1024, &read, &buf );
            if ( rc != ERROR_MORE_DATA && rc != ERROR_SUCCESS )
                  break;

            printf( "\ngot %lu entries\n", read );

            switch ( level )
            {
                  case 1: // users
                        for ( i = 0, ndu = (NET_DISPLAY_USER *) buf; i < read; ++ i, ++ ndu )
                              print_user( ndu->usri1_name, ndu->usri1_flags );
                        // take the last element's next_index
                        if ( read > 0 )
                              next_index = ((NET_DISPLAY_USER *)buf)[read - 1].usri1_next_index;
                        break;
                  case 2: // machines
                        for ( i = 0, ndm = (NET_DISPLAY_MACHINE *) buf; i < read; ++ i, ++ ndm )
                              print_user( ndm->usri2_name, ndm->usri2_flags );
                        // take the last element's next_index
                        if ( read > 0 )
                              next_index = ((NET_DISPLAY_MACHINE *)buf)[read - 1].usri2_next_index;
                        break;
                  case 3: // groups
                        for ( i = 0, ndg = (NET_DISPLAY_GROUP *) buf; i < read; ++ i, ++ ndg )
                              printf( "%-40.40S (group)\n", ndg->grpi3_name );
                        // take the last element's next_index
                        if ( read > 0 )
                              next_index = ((NET_DISPLAY_GROUP *)buf)[read - 1].grpi3_next_index;
                        break;
            }

            if ( buf != NULL )
                  NetApiBufferFree( buf );

      } while ( rc == ERROR_MORE_DATA );

      if ( rc != ERROR_SUCCESS )
            printf( "NQDI() returned %lu\n", rc );
}



int main( void )
{
      // set this to \\your_domain_controller
      wchar_t *servername = L"\\\\BABYA";

      // get all accounts
      puts( "\n*** Enumerating accounts: NetUserEnum()" );
      do_NetUserEnum( servername );

      // get users
      puts( "\n*** Enumerating users: NetQueryDisplayInformation()" );
      do_NetQueryDisplayInformation( servername, 1 );

      // get machine accounts
      puts( "\n*** Enumerating machine accounts: NetQueryDisplayInformation()" );
      do_NetQueryDisplayInformation( servername, 2 );

      // get groups
      puts( "\n*** Enumerating groups: NetQueryDisplayInformation()" );
      do_NetQueryDisplayInformation( servername, 3 );

      return 0;
}




Display the logon sessions, use:


VOID DisplaySessionLogons( LPTSTR ServerName )
{
   LPSESSION_INFO_10 pBuf = NULL;
   LPSESSION_INFO_10 pTmpBuf;
   DWORD            dwLevel = 10;
   DWORD            dwPrefMaxLen = 0xFFFFFFFF;
   DWORD            dwEntriesRead = 0;
   DWORD            dwTotalEntries = 0;
   DWORD            dwResumeHandle = 0;
   DWORD            i;
   DWORD            dwTotalCount = 0;
   LPTSTR            pszClientName = NULL;
   LPTSTR            pszUserName = NULL;
   NET_API_STATUS nStatus;
   PSID                  sid;
   DWORD            sidSize, domainNameSize;
   BYTE                  sidBuffer[MAX_SID_SIZE];
   TCHAR            domainName[MAX_NAME_STRING];  
   SID_NAME_USE sidType;
   BOOLEAN            first = TRUE;

   //
   // Now display session logons
   //
   do
   {
      nStatus = NetSessionEnum(ServerName,
                               pszClientName,
                               pszUserName,
                               dwLevel,
                               (LPBYTE*)&pBuf,
                               dwPrefMaxLen,
                               &dwEntriesRead,
                               &dwTotalEntries,
                               &dwResumeHandle);

      if ((nStatus == NERR_Success) || (nStatus == ERROR_MORE_DATA))
      {
         if ((pTmpBuf = pBuf) != NULL)
         {
            for (i = 0; (i < dwEntriesRead); i++)
            {
               assert(pTmpBuf != NULL);

               if (pTmpBuf == NULL)
               {
                  fprintf(stderr, "An access violation has occurred\n");
                  break;
               }

                     //
                     // Take the name and look up a SID so that we can get full domain/user
                     // information
                     //
                     sid = (PSID) sidBuffer;
                     sidSize = sizeof( sidBuffer );
                     domainNameSize = sizeof( domainName );

                     if( pTmpBuf->sesi10_username[0] ) {

                           if( first ) {
                                
                                    wprintf(L"\nUsers logged on via resource shares:\n");
                                    first = FALSE;
                           }
                           if( LookupAccountName( pTmpBuf->sesi10_cname ,
                                                            pTmpBuf->sesi10_username,
                                                            sid,
                                                            &sidSize,
                                                            domainName,
                                                            &domainNameSize,
                                                            &sidType )) {

                                 wprintf(L"     %s\\%s\n", domainName, pTmpBuf->sesi10_username );

                           } else {
                                    
                                wprintf(L"      %s\\%s\n", ServerName, pTmpBuf->sesi10_username );
                           }
                     }

               pTmpBuf++;
               dwTotalCount++;
            }
         }
      } else {

              wprintf(L"Unable to query resource logons\n");
              first = FALSE;
        }

      if (pBuf != NULL)
      {
         NetApiBufferFree(pBuf);
         pBuf = NULL;
      }
   }
   while (nStatus == ERROR_MORE_DATA);

   if (pBuf != NULL)
      NetApiBufferFree(pBuf);
      if( first ) wprintf(L"\nNo one is logged on via resource shares.\n");
}



The name of the logged on user can be found by calling 'GetUserName()'
0
jkrCommented:
Ooops, sorry, wrong browser window - please reject this...
0
jkrCommented:
jkr has changed their proposed answer to a comment
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

MadshiCommented:
Simple:

  threadID = GetWindowThreadProcessID(windowHandle, NULL);

Regards, Madshi.
0
telebasicsAuthor Commented:
Sounds good but i get an invalid use of NULL when i trie to get the treadID for sometrhing like the taskbar
0
MadshiCommented:
Well, I'm no C++ programmer. In Delphi it looks like "...(windowHandle, nil);". Which language are you using?
0
MadshiCommented:
Perhaps you have to do something like this?

  threadID = GetWindowThreadProcessID(windowHandle, (LPDWORD) NULL);

?
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
telebasicsAuthor Commented:
OK Using an other Null operater availible in basic it works
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.