Click to See Complete Forum and Search --> : SOS! About API: Netsessiondel()


Himao
May 31st, 2005, 05:25 AM
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.

golanshahar
May 31st, 2005, 06:05 AM
if you want to delete all session of the current user ( parameter 3) then the
UncClientName parameter can be null.

Cheers

Himao
May 31st, 2005, 07:52 AM
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;
}

golanshahar
May 31st, 2005, 08:04 AM
the fuction ::NetSessionDel(..) close the session between the server and the workstation.

in your code you called

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


::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

Himao
May 31st, 2005, 12:30 PM
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~~~

NoHero
May 31st, 2005, 01:06 PM
Doesn't...

NetSessionDel(S"\\\\.\\", S"\\\\robot", NULL);

... work? Try this without any other code... Because it would to be to strange if not...

Himao
May 31st, 2005, 01:18 PM
NetSessionDel(S"\\\\.\\", S"\\\\robot", NULL);



Thank you ~

return error 53~~~

Gloomy~~~~~~~~~

NoHero
May 31st, 2005, 01:28 PM
Error 53 means "Bad network path" So maybe one or both network path don't exist or are wrong.

Himao
May 31st, 2005, 01:35 PM
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~~~~~~

NoHero
May 31st, 2005, 01:57 PM
I will test the code on my computers I will inform you on the results...

NoHero
May 31st, 2005, 02:21 PM
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.

#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

Himao
May 31st, 2005, 02:24 PM
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~~~~~

NoHero
May 31st, 2005, 02:27 PM
I am online waitting for you result~~~~~~~~~~~~

Check my previous post

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().:

TCHAR himao[] = S"himao";
TCHAR buff[100];

wsprintf(buff, S"\\\\%s", himao);
wprintf(buff);

Himao
May 31st, 2005, 02:47 PM
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

NoHero
May 31st, 2005, 02:51 PM
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?

Himao
May 31st, 2005, 02:54 PM
I have do nonthing about what you say~~~

How poorly I am when I use the knowledge~~~

NoHero
May 31st, 2005, 02:58 PM
The add

#define _UNICODE

Above the includes and add tchar to your includes...:

#ifndef UNICODE
#define UNICODE
#endif

#define _UNICODE

#include <stdio.h>
#include <assert.h>
#include <windows.h>
#include <tchar.h>
#include <lm.h>

// ...

Himao
May 31st, 2005, 03:04 PM
This is the MapRobotServerDlg.cpp
///////////////////////////////////////////////////////
#include "stdafx.h"
#include "MapSvr.h"
#include "MapSvrDlg.h"
#include ".\mapsvrdlg.h"

#include <assert.h>
#include <windows.h>
#include <lm.h>

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

class CAboutDlg : public CDialog
{
....................
};
/////////////////////////////////////
I will use the function below
and what I shoul do?

NoHero
May 31st, 2005, 03:06 PM
This is the MapRobotServerDlg.cpp
///////////////////////////////////////////////////////
#include "stdafx.h"
#include "MapSvr.h"
#include "MapSvrDlg.h"
#include ".\mapsvrdlg.h"

#include <assert.h>
#include <windows.h>
#include <lm.h>

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

class CAboutDlg : public CDialog
{
....................
};
/////////////////////////////////////
I will use the function below
and what I shoul do?


You can either tell me which IDE you are using or test to define _UNICODE and & UNICODE in stdafx.h before any includes.

Himao
May 31st, 2005, 03:09 PM
I know little about IDE
and the stdafx.h is
///////////////////////////////////////////////////////
#pragma once

#ifndef VC_EXTRALEAN
#define VC_EXTRALEAN // 从 Windows 标头中排除不常使用的资料
#endif

// 如果您必须使用下列所指定的平台之前的平台,则修改下面的定义。
// 有关不同平台的相应值的最新信息,请参考 MSDN。
#ifndef WINVER // 允许使用 Windows 95 和 Windows NT 4 或更高版本的特定功能。
#define WINVER 0x0400 //为 Windows98 和 Windows 2000 及更新版本改变为适当的值。
#endif

#ifndef _WIN32_WINNT // 允许使用 Windows NT 4 或更高版本的特定功能。
#define _WIN32_WINNT 0x0400 //为 Windows98 和 Windows 2000 及更新版本改变为适当的值。
#endif

#ifndef _WIN32_WINDOWS // 允许使用 Windows 98 或更高版本的特定功能。
#define _WIN32_WINDOWS 0x0410 //为 Windows Me 及更新版本改变为适当的值。
#endif

#ifndef _WIN32_IE // 允许使用 IE 4.0 或更高版本的特定功能。
#define _WIN32_IE 0x0400 //为 IE 5.0 及更新版本改变为适当的值。
#endif

#define _ATL_CSTRING_EXPLICIT_CONSTRUCTORS // 某些 CString 构造函数将是显式的

// 关闭 MFC 对某些常见但经常被安全忽略的警告消息的隐藏
#define _AFX_ALL_WARNINGS

#include <afxwin.h> // MFC 核心和标准组件
#include <afxext.h> // MFC 扩展
#include <afxdisp.h> // MFC 自动化类

#include <afxdtctl.h> // Internet Explorer 4 公共控件的 MFC 支持
#ifndef _AFX_NO_AFXCMN_SUPPORT
#include <afxcmn.h> // Windows 公共控件的 MFC 支持

#include <afxsock.h>
#endif
////////////////////////////////////////////////////////
You are a good teacher to me~

and How small the world is~~

NoHero
May 31st, 2005, 03:11 PM
Please use code tags when posting code. And add the #define's of UNICODE, _UNICODE on top of the file and don't include tchar here. MFC does this for you.

Himao
May 31st, 2005, 03:22 PM
.............................................

NoHero
May 31st, 2005, 03:32 PM
The problem is.: I do not have any ADO installed. And it's now half past 10 pm here in Austria.

Sorry but I cannot help you anymore... I am too tired.

/may my angel be with you

Himao
May 31st, 2005, 03:39 PM
Thank you for all time for your help~~~~~~~~~~~

I will study hard in my future life~~~~~~~~~~~~~~~

Have a good night~~~~~~~~~~~~~~~~~~~~~~

It's 4:37 AM in China now~~~~~~~~~~~~~~~~~

Hoho~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Wellcome to China,Friend, NoHero~~~~~~~~~~~

Himao
May 31st, 2005, 03:43 PM
And Have a good night~~~~~~~