Page 1 of 2

Console commands of Windows 7 (TCP/IP connection)

Posted: Fri Jan 27, 2012 8:44 pm
by registrymechanic22
Hello
Help please, alternative to the console commands Windows 7(how to get the results of these commands on PB with winapi or in some other way):
1) net session
2) net session [computer name] / delete
3) openfiles /query /fo CSV /nh /v
4) openfiles /disconnect /id X

thank

upd:
may need to use NetFileEnum_ (...
but how, and in conjunction with what other functions?

Re: Console commands of Windows 7 (TCP/IP connection)

Posted: Sun Jan 29, 2012 8:48 am
by xorc1zt
http://msdn.microsoft.com/en-us/library ... s.85).aspx

1. list all open sessions with NetSessionEnum
2. close the session with NetSessionDel
3. list all open files with NetFileEnum
4. close the file with NetFileClose

Re: Console commands of Windows 7 (TCP/IP connection)

Posted: Sun Jan 29, 2012 6:48 pm
by registrymechanic22

Code: Select all

Structure SESSION_INFO_1
  cname.l
  username.l
  num_opens.w
  time.w
  open_time.w
  user_flags.w
  dead_time.l
EndStructure

*Buffer.Session_info_1

Num.l
Total.l

success=NetSessionEnum_(0, 0, 0, 1, @*Buffer, -1, @Num, @Total, 0)

If success = 0 And success <> #ERROR_MORE_DATA
  For  i=1 To Num

    Debug "Num - "+Str(Num)
    Debug "Total - "+Str(Total)
    Debug "cname - "+PeekS(*Buffer\cname, -1, #PB_Unicode)
    Debug "username - "+PeekS(*Buffer\username , -1, #PB_Unicode)
    Debug "num_opens - "+Str(*Buffer\num_opens)
    Debug "time - "+Str((*Buffer\time))
    Debug "open_time. - "+FormatDate("%hh:%ii:%ss",*Buffer\open_time)
    Debug "user_flags - "+Str(*Buffer\user_flags)
    Debug "dead_time - "+FormatDate("%hh:%ii:%ss",*Buffer\dead_time)
    Debug "********************"
    
  Next
EndIf

NetApiBufferFree_(*buffer)
If Num > 1, then I can not get data on these compounds... :(
help please...

PS: http://msdn.microsoft.com/en-us/library ... 85%29.aspx

Re: Console commands of Windows 7 (TCP/IP connection)

Posted: Sun Jan 29, 2012 9:42 pm
by infratec
Hi,

try this:

Add

Code: Select all

*Buffer + SizeOf(SESSION_INFO_1)
as last line inside the loop.

Bernd

Re: Console commands of Windows 7 (TCP/IP connection)

Posted: Mon Jan 30, 2012 5:40 am
by registrymechanic22
infratec wrote: try this:
Add

Code: Select all

*Buffer + SizeOf(SESSION_INFO_1)
as last line inside the loop.
Bernd
HI

on the line:

Code: Select all

Debug "cname -" + PeekS (* Buffer \ cname, -1, # PB_Unicode)
an error: "[ERROR] Spetsifid address used null"

:?:


ps: http://pinvoke.net/default.aspx/netapi3 ... nEnum.html
http://forum.sources.ru/index.php?showtopic=8537

Re: Console commands of Windows 7 (TCP/IP connection)

Posted: Mon Jan 30, 2012 9:27 am
by infratec
Hi,

I think your structure is complete wrong.
It should be:

Code: Select all

typedef struct _SESSION_INFO_1 {
  LPWSTR sesi1_cname;
  LPWSTR sesi1_username;
  DWORD  sesi1_num_opens;
  DWORD  sesi1_time;
  DWORD  sesi1_idle_time;
  DWORD  sesi1_user_flags;
} SESSION_INFO_1, *PSESSION_INFO_1, *LPSESSION_INFO_1;
This results in:

Code: Select all

Structure _SESSION_INFO_1
  sesi1_cname.i
  sesi1_username.i
  sesi1_num_opens.l
  sesi1_time.l
  sesi1_idle_time.l
  sesi1_user_flags.l
EndStructure


*Buffer._SESSION_INFO_1 = #Null

Define Num.l
Define Total.l

success = NetSessionEnum_(#Null, #Null, #Null, 1, @*Buffer, -1, @Num, @Total, #Null)

If success = 0 Or success = #ERROR_MORE_DATA
  
  Debug "Num - " + Str(Num)
  Debug "Total - " + Str(Total)
  Debug "---------------------------"
  
  *Ptr._SESSION_INFO_1 = *Buffer
  
  For i = 1 To Num
    Debug "cname - " + PeekS(*Ptr\sesi1_cname, -1, #PB_Unicode)
    Debug "username - " + PeekS(*Ptr\sesi1_username , -1, #PB_Unicode)
    Debug "num_opens - " + Str(*Ptr\sesi1_num_opens)
    Debug "time - " + Str((*Ptr\sesi1_time))
    Debug "idle_time. - " + FormatDate("%hh:%ii:%ss",*Ptr\sesi1_idle_time)
    Debug "user_flags - " + Str(*Ptr\sesi1_user_flags)
    Debug "********************"
    
    *Ptr + SizeOf(_SESSION_INFO_1)
  Next
EndIf

If *Buffer : NetApiBufferFree_(*Buffer) : EndIf
Please try it, since I have no open sessions :(

Bernd

Re: Console commands of Windows 7 (TCP/IP connection)

Posted: Mon Jan 30, 2012 10:08 am
by infratec
Hi,

I had to modify the code above :!:

I used .l for the pointers.
Since you want to use it in 64bit I had to change it to .i (which is always better for pointers)

Thanks Danilo for the hint :!:

Bernd

Re: Console commands of Windows 7 (TCP/IP connection)

Posted: Mon Jan 30, 2012 12:47 pm
by registrymechanic22
Hi.
Thank you very much!! :D

Final version of the:

Code: Select all

Structure _SESSION_INFO_2
  sesi2_cname.i
  sesi2_username.i
  sesi2_num_opens.l
  sesi2_time.l
  sesi2_idle_time.l
  sesi2_user_flags.l
  sesi2_cltype_name.i
EndStructure

*Buffer._SESSION_INFO_2 = #Null

Num.l
Total.l

success=NetSessionEnum_(#Null, #Null, #Null, 2, @*Buffer, -1, @Num, @Total, #Null)

If success = 0 And success <> #ERROR_MORE_DATA
 
  Debug "Num - "+Str(Num)
  Debug "Total - "+Str(Total)
  Debug "---------------------------"
 
  *Ptr._SESSION_INFO_2 = *Buffer
 
  For  i=1 To Num
    Debug "cname - "+PeekS(*Ptr\sesi2_cname, -1, #PB_Unicode)
    Debug "username - "+PeekS(*Ptr\sesi2_username , -1, #PB_Unicode)
    Debug "num_opens - "+Str(*Ptr\sesi2_num_opens)
    Debug "time - "+ FormatDate("%dd", *Ptr\sesi2_time-(60*60*24) ) + " day / " + FormatDate("%hh:%ii:%ss", *Ptr\sesi2_time)
    Debug "idle_time - "+ FormatDate("%dd", *Ptr\sesi2_idle_time-(60*60*24) ) + " day / " + FormatDate("%hh:%ii:%ss", *Ptr\sesi2_idle_time)
    Debug "user_flags - "+Str(*Ptr\sesi2_user_flags)
    Debug "cltype_name - "+Str(*Ptr\sesi2_cltype_name)
    Debug "********************"
   
    *Ptr + SizeOf(_SESSION_INFO_2)
  Next
EndIf

If *Buffer : NetApiBufferFree_(*Buffer) : EndIf
the result:
Num - 8
Total - 8
---------------------------
cname - 172.28.42.44
username - AAAAAAAAA
num_opens - 5
time - 00 day / 05:02:07
idle_time - 00 day / 00:00:28
user_flags - 0
cltype_name - 6474144
********************
cname - 172.28.42.84
username - BBBBBBBBB
num_opens - 2
time - 00 day / 04:59:32
idle_time - 00 day / 01:27:23
user_flags - 0
cltype_name - 6474620
********************
cname - 172.28.42.52
username - CCCCCCCCC
num_opens - 1
time - 00 day / 04:18:57
idle_time - 00 day / 00:09:41
user_flags - 0
cltype_name - 6474852
********************
cname - 172.28.42.71
username - DDDDDDDDD
num_opens - 1
time - 00 day / 04:49:50
idle_time - 00 day / 01:46:48
user_flags - 0
cltype_name - 6473668
********************

Re: Console commands of Windows 7 (TCP/IP connection)

Posted: Mon Jan 30, 2012 12:54 pm
by registrymechanic22
What is missing?:

Code: Select all

Debug NetSessionDel_ (# Null, # Null, # Null)
an error 87

a:

Code: Select all

Procedure.s GetComputerName ()
   buffer.s = Space (64)
   bufsize.l ​​= 64
   GetComputerName_ (@ buffer, @ bufsize)
   ProcedureReturn buffer
EndProcedure

Debug NetSessionDel_ ("\ \ \ \" + GetComputerName (), "\ \ \ \ 172.28.42.44", # Null)
an error 53

PS: http://msdn.microsoft.com/en-us/library ... 85%29.aspx

Re: Console commands of Windows 7 (TCP/IP connection)

Posted: Mon Jan 30, 2012 12:57 pm
by infratec
registrymechanic22 wrote:Hi.
Thank you very much!! :D
You're welcome :!:

Btw.: This makes no sense:

Code: Select all

If success = 0 And success <> #ERROR_MORE_DATA
:wink:

Bernd

Re: Console commands of Windows 7 (TCP/IP connection)

Posted: Mon Jan 30, 2012 1:01 pm
by infratec
registrymechanic22 wrote:What is missing?:

Code: Select all

Debug NetSessionDel_ (#Null, #Null, #Null)
It is not allowed to have all parameters #Null :!:
The second or third parameter has to be a valid entry:
http://msdn.microsoft.com/en-us/library ... 85%29.aspx

Code: Select all

Debug NetSessionDel_ ("\ \ \ \" + GetComputerName (), "\ \ \ \ 172.28.42.44", #Null)
I also think that you need only 2 Backslashes, since PB did not use \ as escape character.
Test:

Code: Select all

Debug NetSessionDel_ ("\\" + GetComputerName (), "\\172.28.42.44", #Null)
Maybe you need:

Code: Select all

Debug NetSessionDel_ (@"\\" + GetComputerName (), @"\\172.28.42.44", #Null)
Bernd

P.S.: Sorry, but I can not test this stuff.
I'm logged in a domain, but I have no sessions :(

Re: Console commands of Windows 7 (TCP/IP connection)

Posted: Mon Jan 30, 2012 1:14 pm
by registrymechanic22
infratec wrote: This makes no sense:
Bernd
ok, thank you.

Code: Select all

Procedure.s GetComputerName()
  buffer.s=Space(64)
  bufsize.l=64
  GetComputerName_(@buffer, @bufsize)
  ProcedureReturn buffer
EndProcedure

Debug NetSessionDel_(#Null, "\\172.28.42.44", #Null)
error 2312

Code: Select all

Procedure.s GetComputerName()
  buffer.s=Space(64)
  bufsize.l=64
  GetComputerName_(@buffer, @bufsize)
  ProcedureReturn buffer
EndProcedure

Debug NetSessionDel_("\\"+GetComputerName(), "\\172.28.42.44", #Null)
error 53

Re: Console commands of Windows 7 (TCP/IP connection)

Posted: Mon Jan 30, 2012 1:15 pm
by infratec
Hi,

Try the '@' infront of the strings (see my last post above),
because you need a
pointer to a string
Better would be

Code: Select all

Help$ = "\\172.28.42.44"
(#Null, @Help$, ...
Bernd

Re: Console commands of Windows 7 (TCP/IP connection)

Posted: Mon Jan 30, 2012 1:23 pm
by infratec

Code: Select all

#ERROR_ACCESS_DENIED = 5
#ERROR_BAD_NETPATH  =  53
#ERROR_INVALID_PARAMETER = 87

#NERR_ClientNameNotFound = 2312
#NERR_InvalidComputer = 2351 
#NERR_NoSuchServer = 2460
#NERR_NoSuchSession = 2461
Bernd

Re: Console commands of Windows 7 (TCP/IP connection)

Posted: Mon Jan 30, 2012 1:35 pm
by registrymechanic22

Code: Select all

Procedure.s GetComputerName()
  buffer.s=Space(64)
  bufsize.l=64
  GetComputerName_(@buffer, @bufsize)
  ProcedureReturn buffer
EndProcedure

Servername.s = "\\"+GetComputerName()
UncClientName.s = "\\172.28.42.44""

Debug NetSessionDel_(@Servername, #Null, #Null)
Debug NetSessionDel_(#Null, @UncClientName, #Null)
Debug NetSessionDel_(@Servername, @UncClientName, #Null)
error:
53
2312 ; 172.28.42.44 - taken from NetSessionEnum_(
53