Console commands of Windows 7 (TCP/IP connection)

Just starting out? Need help? Post your questions and find answers here.
infratec
Always Here
Always Here
Posts: 7662
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

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

Post by infratec »

Hi,

you have to use unicode.
Try this:

Code: Select all

*Buffer = AllocateMemory(100)
PokeS(*Buffer, "\\172.28.42.44", -1, #PB_Unicode)
Debug NetSessionDel_(#Null, *Buffer, #Null)
To avoid AllocateMemory() you can use this:

Code: Select all

UncClientName = Space(100)
PokeS(@UncClientName, "\\172.28.42.44", -1, #PB_Unicode)
Debug NetSessionDel_(#Null, @UncClientName, #Null)
Bernd
jpd
Enthusiast
Enthusiast
Posts: 167
Joined: Fri May 21, 2004 3:31 pm

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

Post by jpd »

Hi registrymechanic22,

is long time ago that I have used this stuff.

here a sample to delete the opened session and one to close opened file.

Best
jpd

Code: Select all

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

Procedure.s NetName(name.s)
    name=RemoveString(name, "\" )
    name="\\"+name
    ProcedureReturn name   
EndProcedure

Procedure.l NetSessionDelete(server.s,client.s,user.s)
    Protected Success
    Protected.s Userver,UClient,Uuser
    
    CompilerIf #PB_Compiler_Unicode 
        Success=NetSessionDel_(NetName(server),NetName(client),user)
    CompilerElse
        Userver = Space(10) 
        Uclient =Space(10) 
        Uuser =Space(10) 
        PokeS(@Userver, NetName(server), -1, #PB_Unicode) 
        PokeS(@Uclient, NetName(client), -1, #PB_Unicode) 
        PokeS(@Uuser, user, -1, #PB_Unicode) 
        Success=NetSessionDel_(@Userver,@Uclient,@Uuser)
    CompilerEndIf
    
    Debug "NetSessionDelete Return: "+Str(Success)
    Debug "-------------------------------------------"
    ProcedureReturn   Success
    
EndProcedure

Procedure NetFileClose(server.s,fileid.l)
    Protected.l Success
    Protected SUnicodeLen, *SUniCode
    server=NetName(server)
    CompilerIf #PB_Compiler_Unicode 
        Success=NetFileClose_(server,fileid)
    CompilerElse 
        
        SUnicodeLen = MultiByteToWideChar_( #CP_ACP, 0, server, -1, 0,0) 
        *SUniCode = AllocateMemory(SUnicodeLen) 
        MultiByteToWideChar_( #CP_ACP, 0, server, -1, *SUniCode, SUnicodeLen) 
        Success=NetFileClose_(*SUniCode,fileid)
        FreeMemory(*SUniCode) 
    CompilerEndIf 
    
    ProcedureReturn Success
    
EndProcedure

Procedure.s GetLastError(Err.l)
    
    Protected buffer.l = 0, ferr.l
    Protected errormsg.s = ""
    
    ferr = FormatMessage_(#FORMAT_MESSAGE_ALLOCATE_BUFFER|#FORMAT_MESSAGE_FROM_SYSTEM,0,Err,GetUserDefaultLangID_(),@buffer,0,0)
    
    If buffer <> 0
        
        errormsg=PeekS(buffer)
        LocalFree_(buffer)
        errormsg=RemoveString(errormsg,Chr(13)+Chr(10))
        
    EndIf
    
    ProcedureReturn errormsg
    
EndProcedure

Define Ret
Ret = NetFileClose("jpd-pc",298)

Debug "ReturnCode: " +Str(Ret)
Debug "ErrDescription: "+ GetLastError(Ret)
 
; Ret = NetSessionDelete("jpd-pc","jpd-pc","jpd") 
; 
; Debug "ReturnCode: " +Str(Ret)
; Debug "ErrDescription: "+ GetLastError(Ret)
PB 5.10 Windows 7 x64 SP1
registrymechanic22
Enthusiast
Enthusiast
Posts: 176
Joined: Sun Jun 28, 2009 7:07 pm
Location: RUS

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

Post by registrymechanic22 »

infratec / Bernd
Thank you very much! If you have not helped, I do not know that I did ..... ;)
With respect to you!


Final version:

Code: Select all

Procedure DelSessionNet(UCN.s)
  Protected UncClientName.s
  UncClientName=Space(Len(UCN))
  PokeS(@UncClientName, "\\"+UCN, -1, #PB_Unicode)
  ProcedureReturn   NetSessionDel_(#Null, @UncClientName, #Null)
EndProcedure

If DelSessionNet("172.28.42.44")=0
  Debug "GOOD"
Else 
  Debug "BAD"
EndIf
and more:

Code: Select all

Structure _FILE_INFO_3 
  fi3_id.l
  fi3_permissions.l
  fi3_num_locks.l
  fi3_pathname.i
  fi3_username.i
EndStructure

*Buffer._FILE_INFO_3 = #Null

Num.l
Total.l

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

If success = 0

  Debug "Num - "+Str(Num)
  Debug "Total - "+Str(Total)
  Debug "---------------------------"

  *Ptr._FILE_INFO_3  = *Buffer

  For  i=1 To Num
    
    Debug "id - "+Str(*Ptr\fi3_id)
    Debug "permissions - "+Str(*Ptr\fi3_permissions)
    Debug "num_locks - "+Str(*Ptr\fi3_num_locks)
    Debug "pathname - "+PeekS(*Ptr\fi3_pathname, -1, #PB_Unicode)
    Debug "username - "+PeekS(*Ptr\fi3_username, -1, #PB_Unicode)
    Debug "********************"
    
    *Ptr + SizeOf(_FILE_INFO_3)
    
  Next
EndIf

If *Buffer : NetApiBufferFree_(*Buffer) : EndIf
and:

Code: Select all

NetFileClose_(#Null, id.l)
registrymechanic22
Enthusiast
Enthusiast
Posts: 176
Joined: Sun Jun 28, 2009 7:07 pm
Location: RUS

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

Post by registrymechanic22 »

jpd wrote:

Code: Select all

Procedure.l NetSessionDelete(server.s,client.s,user.s)
    Protected Success
    Protected.s Userver,UClient,Uuser
    CompilerIf #PB_Compiler_Unicode 
        Success=NetSessionDel_(NetName(server),NetName(client),user)
    CompilerElse
        Userver = Space(10) 
        Uclient =Space(10) 
        Uuser =Space(10) 
        PokeS(@Userver, NetName(server), -1, #PB_Unicode) 
        PokeS(@Uclient, NetName(client), -1, #PB_Unicode) 
        PokeS(@Uuser, user, -1, #PB_Unicode) 
        Success=NetSessionDel_(@Userver,@Uclient,@Uuser)
    CompilerEndIf
    Debug "NetSessionDelete Return: "+Str(Success)
    Debug "-------------------------------------------"
    ProcedureReturn   Success
EndProcedure

Procedure NetFileClose(server.s,fileid.l)
    Protected.l Success
    Protected SUnicodeLen, *SUniCode
    server=NetName(server)
    CompilerIf #PB_Compiler_Unicode 
        Success=NetFileClose_(server,fileid)
    CompilerElse 
        SUnicodeLen = MultiByteToWideChar_( #CP_ACP, 0, server, -1, 0,0) 
        *SUniCode = AllocateMemory(SUnicodeLen) 
        MultiByteToWideChar_( #CP_ACP, 0, server, -1, *SUniCode, SUnicodeLen) 
        Success=NetFileClose_(*SUniCode,fileid)
        FreeMemory(*SUniCode) 
    CompilerEndIf 
    ProcedureReturn Success
EndProcedure
works well, thanks.
infratec
Always Here
Always Here
Posts: 7662
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

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

Post by infratec »

registrymechanic22 wrote:infratec / Bernd
Thank you very much! If you have not helped, I do not know that I did ..... ;)
With respect to you!
:oops: :oops: :oops:

But I have to show you a memory fault in your code.
Try the following:

Code: Select all

Debug StringByteLength("1234567890", #PB_Ascii)
Debug StringByteLength("1234567890", #PB_Unicode)
So please replace your

Code: Select all

UncClientName=Space(Len(UCN))
with

Code: Select all

UncClientName=Space(StringByteLength("\\" + UCN, #PB_Unicode) + 2)
I don't know exactly how a unicode string is terminated :oops:
maybe it is a double 0 (+ 2)
But better to reserve more memory than less :wink:

Bernd
registrymechanic22
Enthusiast
Enthusiast
Posts: 176
Joined: Sun Jun 28, 2009 7:07 pm
Location: RUS

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

Post by registrymechanic22 »

infratec wrote:So please replace your

Code: Select all

UncClientName=Space(Len(UCN))
with

Code: Select all

UncClientName=Space(StringByteLength("\\" + UCN, #PB_Unicode) + 2)
Yes, exactly, I forgot about the "\\"

infratec wrote:I don't know exactly how a unicode string is terminated :oops:
maybe it is a double 0 (+ 2)
But better to reserve more memory than less :wink:
ok

Thanks!
Post Reply