| CodeGuru Home | VC++ / MFC / C++ | .NET / C# | Visual Basic | Newsletters | VB Forums | Developer.com |
|
|||||||
| C++ and WinAPI Discuss Windows API related issues using C++ (and Visual C++). This is a non-MFC forum. |
![]() |
|
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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. |
|
#2
|
|||
|
|||
|
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 |
|
#3
|
|||
|
|||
|
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; } |
|
#4
|
|||
|
|||
|
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) you should give the server name in the fisrt parameter Code:
::Netsessiondel(L"my server name",pTmpBuf->sesi10_cname,NULL) if i helped dont forget to rate :-) Cheers |
|
#5
|
|||
|
|||
|
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. |
|
#6
|
||||
|
||||
|
Re: SOS! About API: Netsessiondel()
Doesn't...
Code:
NetSessionDel(S"\\\\.\\", S"\\\\robot", NULL);
__________________
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! |
|
#7
|
|||
|
|||
|
Re: SOS! About API: Netsessiondel()
Quote:
return error 53~~~ Gloomy~~~~~~~~~ |
|
#8
|
||||
|
||||
|
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! |
|
#9
|
|||
|
|||
|
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~~~~~~ |
|
#10
|
||||
|
||||
|
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! |
|
#11
|
||||
|
||||
|
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;
}
/"mike" is the name of my second PC I used for testing
__________________
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. |
|
#12
|
|||
|
|||
|
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~~~~~ |
|
#13
|
||||
|
||||
|
Re: SOS! About API: Netsessiondel()
Quote:
Quote:
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 |
|
#14
|
|||
|
|||
|
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 |
|
#15
|
||||
|
||||
|
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! |
![]() |
| Bookmarks |
|
||||||
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|