CodeGuru Forums -
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic Newsletters VB Forums Developer.com


Newest CodeGuru.com Articles:

  • Installing SQL Server 2008
  • Writing UDFs for Firebird Embedded SQL Server
  • [Updated] Shutdown Manager
  • Building Windows Azure Cloud Service Applications with Azure Storage and the Azure SDK

  • Search CodeGuru:
     



    Go Back   CodeGuru Forums > Visual C++ & C++ Programming > C++ and WinAPI
    FAQ Members List Calendar Search Today's Posts Mark Forums Read

    C++ and WinAPI Discuss Windows API related issues using C++ (and Visual C++). This is a non-MFC forum.

    Reply
     
    Thread Tools Search this Thread Rate Thread Display Modes
      #1    
    Old May 31st, 2005, 06:25 AM
    Himao Himao is offline
    Junior Member
     
    Join Date: May 2005
    Posts: 22
    Himao is an unknown quantity at this point (<10)
    Question SOS! About API: Netsessiondel()

    NET_API_STATUS NetSessionDel(
    LPWSTR servername,
    LPWSTR UncClientName,
    LPWSTR username
    );

    How to set the second parameter when I want to delete a session with the Client name?

    It seems it just could delete sessions with the username.
    Reply With Quote
      #2    
    Old May 31st, 2005, 07:05 AM
    golanshahar golanshahar is offline
    Elite Member
    Power Poster
     
    Join Date: May 2005
    Posts: 4,936
    golanshahar has a reputation beyond repute (3000+)golanshahar has a reputation beyond repute (3000+)golanshahar has a reputation beyond repute (3000+)golanshahar has a reputation beyond repute (3000+)golanshahar has a reputation beyond repute (3000+)golanshahar has a reputation beyond repute (3000+)golanshahar has a reputation beyond repute (3000+)golanshahar has a reputation beyond repute (3000+)golanshahar has a reputation beyond repute (3000+)golanshahar has a reputation beyond repute (3000+)golanshahar has a reputation beyond repute (3000+)
    Re: SOS! About API: Netsessiondel()

    if you want to delete all session of the current user ( parameter 3) then the
    UncClientName parameter can be null.

    Cheers
    Reply With Quote
      #3    
    Old May 31st, 2005, 08:52 AM
    Himao Himao is offline
    Junior Member
     
    Join Date: May 2005
    Posts: 22
    Himao is an unknown quantity at this point (<10)
    Re: SOS! About API: Netsessiondel()

    Many thanks to you at first~

    But I want to delete the client I choose.

    I have a try with Netsessiondel(NULL,pTmpBuf->sesi10_cname,NULL)

    And it returns error(NERR_ClientNameNotFound A session does not exist with that computer name )


    I get value of pTmpBuf->sesi10_cname in example from MSDN

    ////////////////////////////////////////////////////////////////////////////////////////////////
    // NetSession.cpp : 定义控制台应用程序的入口点。
    //



    #ifndef UNICODE
    #define UNICODE
    #endif
    #include "stdafx.h"
    #include <stdio.h>
    #include <assert.h>
    #include <windows.h>
    #include <lm.h>

    int wmain(int argc, wchar_t *argv[])
    {
    LPSESSION_INFO_10 pBuf = NULL;
    LPSESSION_INFO_10 pTmpBuf;
    DWORD dwLevel = 10;
    DWORD dwPrefMaxLen = MAX_PREFERRED_LENGTH;
    DWORD dwEntriesRead = 0;
    DWORD dwTotalEntries = 0;
    DWORD dwResumeHandle = 0;
    DWORD i;
    DWORD dwTotalCount = 0;
    LPTSTR pszServerName = NULL;
    LPTSTR pszClientName = NULL;
    LPTSTR pszUserName = NULL;
    NET_API_STATUS nStatus ,nStatu;;
    //
    // Check command line arguments.
    //
    if (argc > 4)
    {
    wprintf(L"Usage: %s [\\\\ServerName] [\\\\ClientName] [UserName]\n", argv[0]);
    exit(1);
    }

    if (argc >= 2)
    pszServerName = (LPTSTR)argv[1];

    if (argc >= 3)
    pszClientName = (LPTSTR)argv[2];

    if (argc == 4)
    pszUserName = (LPTSTR)argv[3];
    //
    // Call the NetSessionEnum function, specifying level 10.
    //
    do // begin do
    {
    nStatus = NetSessionEnum(pszServerName,
    pszClientName,
    pszUserName,
    dwLevel,
    (LPBYTE*)&pBuf,
    dwPrefMaxLen,
    &dwEntriesRead,
    &dwTotalEntries,
    &dwResumeHandle);
    //
    // If the call succeeds,
    //
    if ((nStatus == NERR_Success) || (nStatus == ERROR_MORE_DATA))
    {
    if ((pTmpBuf = pBuf) != NULL)
    {
    //
    // Loop through the entries.
    //
    for (i = 0; (i < dwEntriesRead); i++)
    {
    assert(pTmpBuf != NULL);

    if (pTmpBuf == NULL)
    {
    fprintf(stderr, "An access violation has occurred\n");
    break;
    }
    //
    // Print the retrieved data.
    //
    wprintf(L"\n\tClient: %s\n", pTmpBuf->sesi10_cname);
    wprintf(L"\tUser: %s\n", pTmpBuf->sesi10_username);
    printf("\tActive: %d\n", pTmpBuf->sesi10_time);
    printf("\tIdle: %d\n", pTmpBuf->sesi10_idle_time);

    nStatu = NetSessionDel(NULL,
    pTmpBuf->sesi10_cname,
    NULL);

    if (nStatu == NERR_Success)
    fprintf(stderr, "The specified session(s) has been successfully deleted\n");
    else
    fprintf(stderr, "A system error has occurred: %d\n", nStatu);

    pTmpBuf++;
    dwTotalCount++;
    }
    }
    }
    //
    // Otherwise, indicate a system error.
    //
    else
    fprintf(stderr, "A system error has occurred: %d\n", nStatus);
    //
    // Free the allocated memory.
    //
    if (pBuf != NULL)
    {
    NetApiBufferFree(pBuf);
    pBuf = NULL;
    }
    }
    //
    // Continue to call NetSessionEnum while
    // there are more entries.
    //
    while (nStatus == ERROR_MORE_DATA); // end do

    // Check again for an allocated buffer.
    //
    if (pBuf != NULL)
    NetApiBufferFree(pBuf);
    //
    // Print the final count of sessions enumerated.
    //
    fprintf(stderr, "\nTotal of %d entries enumerated\n", dwTotalCount);

    return 0;
    }
    Reply With Quote
      #4    
    Old May 31st, 2005, 09:04 AM
    golanshahar golanshahar is offline
    Elite Member
    Power Poster
     
    Join Date: May 2005
    Posts: 4,936
    golanshahar has a reputation beyond repute (3000+)golanshahar has a reputation beyond repute (3000+)golanshahar has a reputation beyond repute (3000+)golanshahar has a reputation beyond repute (3000+)golanshahar has a reputation beyond repute (3000+)golanshahar has a reputation beyond repute (3000+)golanshahar has a reputation beyond repute (3000+)golanshahar has a reputation beyond repute (3000+)golanshahar has a reputation beyond repute (3000+)golanshahar has a reputation beyond repute (3000+)golanshahar has a reputation beyond repute (3000+)
    Re: SOS! About API: Netsessiondel()

    the fuction ::NetSessionDel(..) close the session between the server and the workstation.

    in your code you called
    Code:
    Netsessiondel(NULL,pTmpBuf->sesi10_cname,NULL)
    now that is wrong casue if provide NULL on the first paramter that mean u working on the local machine....i dont think this is what you want!

    you should give the server name in the fisrt parameter

    Code:
    ::Netsessiondel(L"my server name",pTmpBuf->sesi10_cname,NULL)
    one more thing you must have an administartor rights for deleting a session

    if i helped dont forget to rate :-)
    Cheers
    Reply With Quote
      #5    
    Old May 31st, 2005, 01:30 PM
    Himao Himao is offline
    Junior Member
     
    Join Date: May 2005
    Posts: 22
    Himao is an unknown quantity at this point (<10)
    Re: SOS! About API: Netsessiondel()

    Thanks for your help~Code Master!

    I want to disconnect a client that connect to my PC ~

    when I delete the sessions with username~

    All the other clients that connects to my pc with the username also are deleted~

    I have been headache for several days ~555555555

    For example:
    there are two sessions to my pc~
    1>
    client : robot
    user: administrator
    ...
    2>
    client : robot2
    user: administrator
    ....

    and now I want to delete the first session(robot1)~Not all sessions.

    My server name is "Himao".

    so what I should do?

    PS:
    I am a chinese student in middle school~

    I will try to perfect my bad English~~~

    Last edited by Himao; May 31st, 2005 at 02:00 PM.
    Reply With Quote
      #6    
    Old May 31st, 2005, 02:06 PM
    NoHero's Avatar
    NoHero NoHero is offline
    Moderator
     
    Join Date: Mar 2004
    Location: (Upper-) Austria
    Posts: 2,899
    NoHero has much to be proud of (1500+)NoHero has much to be proud of (1500+)NoHero has much to be proud of (1500+)NoHero has much to be proud of (1500+)NoHero has much to be proud of (1500+)NoHero has much to be proud of (1500+)NoHero has much to be proud of (1500+)NoHero has much to be proud of (1500+)NoHero has much to be proud of (1500+)NoHero has much to be proud of (1500+)NoHero has much to be proud of (1500+)
    Re: SOS! About API: Netsessiondel()

    Doesn't...

    Code:
    NetSessionDel(S"\\\\.\\", S"\\\\robot", NULL);
    ... work? Try this without any other code... Because it would to be to strange if not...
    __________________
    I am not offering technical guidiance via email or IM
    Come on share your photo with us! CG members photo album!
    Use the Code Tags!
    Reply With Quote
      #7    
    Old May 31st, 2005, 02:18 PM
    Himao Himao is offline
    Junior Member
     
    Join Date: May 2005
    Posts: 22
    Himao is an unknown quantity at this point (<10)
    Re: SOS! About API: Netsessiondel()

    Quote:
    Originally Posted by NoHero
    Code:
    NetSessionDel(S"\\\\.\\", S"\\\\robot", NULL);
    Thank you ~

    return error 53~~~

    Gloomy~~~~~~~~~
    Reply With Quote
      #8    
    Old May 31st, 2005, 02:28 PM
    NoHero's Avatar
    NoHero NoHero is offline
    Moderator
     
    Join Date: Mar 2004
    Location: (Upper-) Austria
    Posts: 2,899
    NoHero has much to be proud of (1500+)NoHero has much to be proud of (1500+)NoHero has much to be proud of (1500+)NoHero has much to be proud of (1500+)NoHero has much to be proud of (1500+)NoHero has much to be proud of (1500+)NoHero has much to be proud of (1500+)NoHero has much to be proud of (1500+)NoHero has much to be proud of (1500+)NoHero has much to be proud of (1500+)NoHero has much to be proud of (1500+)
    Re: SOS! About API: Netsessiondel()

    Error 53 means "Bad network path" So maybe one or both network path don't exist or are wrong.
    __________________
    I am not offering technical guidiance via email or IM
    Come on share your photo with us! CG members photo album!
    Use the Code Tags!
    Reply With Quote
      #9    
    Old May 31st, 2005, 02:35 PM
    Himao Himao is offline
    Junior Member
     
    Join Date: May 2005
    Posts: 22
    Himao is an unknown quantity at this point (<10)
    Re: SOS! About API: Netsessiondel()

    I use NetSessionEnum() to get the sessions~

    The console shell display the sessions which I used~

    So the session must be exist~~

    Where is wrong????

    I should finish the work today~

    Gloomy again~~~~~~
    Reply With Quote
      #10    
    Old May 31st, 2005, 02:57 PM
    NoHero's Avatar
    NoHero NoHero is offline
    Moderator
     
    Join Date: Mar 2004
    Location: (Upper-) Austria
    Posts: 2,899
    NoHero has much to be proud of (1500+)NoHero has much to be proud of (1500+)NoHero has much to be proud of (1500+)NoHero has much to be proud of (1500+)NoHero has much to be proud of (1500+)NoHero has much to be proud of (1500+)NoHero has much to be proud of (1500+)NoHero has much to be proud of (1500+)NoHero has much to be proud of (1500+)NoHero has much to be proud of (1500+)NoHero has much to be proud of (1500+)
    Re: SOS! About API: Netsessiondel()

    I will test the code on my computers I will inform you on the results...
    __________________
    I am not offering technical guidiance via email or IM
    Come on share your photo with us! CG members photo album!
    Use the Code Tags!
    Reply With Quote
      #11    
    Old May 31st, 2005, 03:21 PM
    NoHero's Avatar
    NoHero NoHero is offline
    Moderator
     
    Join Date: Mar 2004
    Location: (Upper-) Austria
    Posts: 2,899
    NoHero has much to be proud of (1500+)NoHero has much to be proud of (1500+)NoHero has much to be proud of (1500+)NoHero has much to be proud of (1500+)NoHero has much to be proud of (1500+)NoHero has much to be proud of (1500+)NoHero has much to be proud of (1500+)NoHero has much to be proud of (1500+)NoHero has much to be proud of (1500+)NoHero has much to be proud of (1500+)NoHero has much to be proud of (1500+)
    Re: SOS! About API: Netsessiondel()

    Well ... this code worked perfect for me until I added the code which adds \\ before the client name (the code I speak of is highlighted). So without the \\ before the username it won't work.

    Code:
    #ifndef UNICODE
    #define UNICODE
    #endif
    
    #include <stdio.h>
    #include <assert.h>
    #include <windows.h> 
    #include <lm.h>
    
    #pragma comment(lib, "Netapi32.lib")
    
    int wmain(int argc, wchar_t *argv[])
    {
       LPSESSION_INFO_10 pBuf = NULL;
       LPSESSION_INFO_10 pTmpBuf;
       DWORD dwLevel = 10;
       DWORD dwPrefMaxLen = -1;
       DWORD dwEntriesRead = 0;
       DWORD dwTotalEntries = 0;
       DWORD dwResumeHandle = 0;
       DWORD i;
       DWORD dwTotalCount = 0;
       LPTSTR pszServerName = NULL;
       LPTSTR pszClientName = NULL;
       LPTSTR pszUserName = NULL;
       NET_API_STATUS nStatus;
       //
       // Check command line arguments.
       //
       if (argc > 4)
       {
          wprintf(L"Usage: %s [\\\\ServerName] [\\\\ClientName] [UserName]\n", argv[0]);
          exit(1);
       }
    
       if (argc >= 2)
          pszServerName = argv[1];
    
       if (argc >= 3)
          pszClientName = argv[2];
    
       if (argc == 4)
          pszUserName = argv[3];
       //
       // Call the NetSessionEnum function, specifying level 10.
       //
       do // begin do
       { 
          nStatus = NetSessionEnum(pszServerName,
                                   pszClientName,
                                   pszUserName,
                                   dwLevel,
                                   (LPBYTE*)&pBuf,
                                   dwPrefMaxLen,
                                   &dwEntriesRead,
                                   &dwTotalEntries,
                                   &dwResumeHandle);
          //
          // If the call succeeds,
          //
          if ((nStatus == NERR_Success) || (nStatus == ERROR_MORE_DATA))
          {
             if ((pTmpBuf = pBuf) != NULL)
             {
                //
                // Loop through the entries.
                //
                for (i = 0; (i < dwEntriesRead); i++)
                {
    			   NET_API_STATUS nRetVal = 0;
    			   TCHAR sz[100] = L"";
                   assert(pTmpBuf != NULL);
    
                   if (pTmpBuf == NULL)
                   {
                      fprintf(stderr, "An access violation has occurred\n");
                      break;
                   }
                   //
                   // Print the retrieved data. 
                   //
                   wprintf(L"\n\tClient: %s\n", pTmpBuf->sesi10_cname);
                   wprintf(L"\tUser:   %s\n", pTmpBuf->sesi10_username);
                   wprintf(L"\tActive: %d\n", pTmpBuf->sesi10_time);
                   wprintf(L"\tIdle:   %d\n", pTmpBuf->sesi10_idle_time);
    				
    			   
    			   wsprintf(sz, L"\\\\%s", pTmpBuf->sesi10_cname);
    			   nRetVal = NetSessionDel(NULL, sz, NULL);
    			   if ( nRetVal != NERR_Success )
    			   {
    				   wprintf(L"\tKicking him..: Error #%d\n", GetLastError());
    			   }
    			   else
    			   {
    				   wprintf(L"\tI kicked his a**! :-)\n");
    			   }
    
                   pTmpBuf++;
                   dwTotalCount++;
                }
             }
          }
          //
          // Otherwise, indicate a system error.
          //
          else
             fprintf(stderr, "A system error has occurred: %d\n", nStatus);
          //
          // Free the allocated memory.
          //
          if (pBuf != NULL)
          {
             NetApiBufferFree(pBuf);
             pBuf = NULL;
          }
       }
       // 
       // Continue to call NetSessionEnum while 
       //  there are more entries. 
       // 
       while (nStatus == ERROR_MORE_DATA); // end do
    
       // Check again for an allocated buffer.
       //
       if (pBuf != NULL)
          NetApiBufferFree(pBuf);
       //
       // Print the final count of sessions enumerated.
       //
       fprintf(stderr, "\nTotal of %d entries enumerated\n", dwTotalCount);
    
       return 0;
    }
    If I pass only the username via the third parameter it also worked like a charm.

    /"mike" is the name of my second PC I used for testing
    Attached Images
     
    __________________
    I am not offering technical guidiance via email or IM
    Come on share your photo with us! CG members photo album!
    Use the Code Tags!

    Last edited by NoHero; May 31st, 2005 at 03:28 PM.
    Reply With Quote
      #12    
    Old May 31st, 2005, 03:24 PM
    Himao Himao is offline
    Junior Member
     
    Join Date: May 2005
    Posts: 22
    Himao is an unknown quantity at this point (<10)
    Re: SOS! About API: Netsessiondel()

    You are a "good" man~~

    Thank you for your helping me~

    I am online waitting for you result~~~~~~~~~~~~

    I think it is the problem of parameter~

    I test the function [netsessiondel] on control shell~

    It deletes sessions successfully~

    But now the new problem is How to translate the value "\\\\himao" to LPWSTR type~~~~~
    Reply With Quote
      #13    
    Old May 31st, 2005, 03:27 PM
    NoHero's Avatar
    NoHero NoHero is offline
    Moderator
     
    Join Date: Mar 2004
    Location: (Upper-) Austria
    Posts: 2,899
    NoHero has much to be proud of (1500+)NoHero has much to be proud of (1500+)NoHero has much to be proud of (1500+)NoHero has much to be proud of (1500+)NoHero has much to be proud of (1500+)NoHero has much to be proud of (1500+)NoHero has much to be proud of (1500+)NoHero has much to be proud of (1500+)NoHero has much to be proud of (1500+)NoHero has much to be proud of (1500+)NoHero has much to be proud of (1500+)
    Re: SOS! About API: Netsessiondel()

    Quote:
    Originally Posted by Himao
    I am online waitting for you result~~~~~~~~~~~~
    Check my previous post

    Quote:
    Originally Posted by Himao
    But now the new problem is How to translate the value "\\\\himao" to LPWSTR type~~~~~
    The string literal? Well just put an L before it. Orif you have "himao" somewhere in a variable just use wsprintf().:

    Code:
    TCHAR himao[] = S"himao";
    TCHAR buff[100];
    
    wsprintf(buff, S"\\\\%s", himao);
    wprintf(buff);
    __________________
    I am not offering technical guidiance via email or IM
    Come on share your photo with us! CG members photo album!
    Use the Code Tags!

    Last edited by NoHero; May 31st, 2005 at 03:27 PM. Reason: typo
    Reply With Quote
      #14    
    Old May 31st, 2005, 03:47 PM
    Himao Himao is offline
    Junior Member
     
    Join Date: May 2005
    Posts: 22
    Himao is an unknown quantity at this point (<10)
    Re: SOS! About API: Netsessiondel()

    OMG~~~~~~~~~~~~~~

    I want to use it in MFC~~~

    when I used the following code

    TCHAR sz[100] = L"";
    wsprintf(sz, L"\\\\%s", pTmpBuf->sesi10_cname);

    There are two complier error~~~

    1> couldn't convert const wchar_t[1] to TCHAR [100]
    2> wsprintfA : couldn't convert the seccond parameter const wchar_t[5] to LPCSTR
    Reply With Quote
      #15    
    Old May 31st, 2005, 03:51 PM
    NoHero's Avatar
    NoHero NoHero is offline
    Moderator
     
    Join Date: Mar 2004
    Location: (Upper-) Austria
    Posts: 2,899
    NoHero has much to be proud of (1500+)NoHero has much to be proud of (1500+)NoHero has much to be proud of (1500+)NoHero has much to be proud of (1500+)NoHero has much to be proud of (1500+)NoHero has much to be proud of (1500+)NoHero has much to be proud of (1500+)NoHero has much to be proud of (1500+)NoHero has much to be proud of (1500+)NoHero has much to be proud of (1500+)NoHero has much to be proud of (1500+)
    Re: SOS! About API: Netsessiondel()

    Have you changed the character set to use in your project properties section to Unicode? Or defined tha macro _UNICODE before including tchar header file?
    __________________
    I am not offering technical guidiance via email or IM
    Come on share your photo with us! CG members photo album!
    Use the Code Tags!
    Reply With Quote
    Reply

    Bookmarks
    Go Back   CodeGuru Forums > Visual C++ & C++ Programming > C++ and WinAPI


    Thread Tools Search this Thread
    Search this Thread:

    Advanced Search
    Display Modes Rate This Thread
    Rate This Thread:

    Posting Rules
    You may not post new threads
    You may not post replies
    You may not post attachments
    You may not edit your posts

    BB code is On
    Smilies are On
    [IMG] code is Off
    HTML code is Off
    Forum Jump


    All times are GMT -5. The time now is 05:42 PM.



    Acceptable Use Policy

    internet.comMediabistrojusttechjobs.comGraphics.com

    WebMediaBrands Corporate Info


    Advertise | Newsletters | Feedback | Submit News

    Legal Notices | Licensing | Permissions | Privacy Policy


    Powered by vBulletin® Version 3.7.3
    Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
    Copyright WebMediaBrands Inc. 2002-2009