Sending NT-Messages, retrieve all local LAN-Users

Share your advanced PureBasic knowledge/code with the community.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Sending NT-Messages, retrieve all local LAN-Users

Post by BackupUser »

Code updated For 5.20+

Restored from previous forum. Originally posted by Rings.

As Subject say, Send Messages through NT (DID not Work on Win9x, neighter on Linux :( ) and retrieve all local (Network)users.

Code: Select all

    ;NetSend; Another Snippet by Siegfried Rings (C) 2002 CodeGuru
    ; This one retrieves all usernames from local Network
    ; and can send a message to them(Works only NT,2000 or XP , Nor Win9x, ME tested)

    #Button1=1
    #Button2=2
    #ListViewGadget=3
    #String1=4
    #Text1=5
    #Text2=6

    Structure SERVER_INFO_101
          dwPlatformId.l
          lpszServerName.l
          dwVersionMajor.l
          dwVersionMinor.l
          dwType.l
          lpszComment.l
    EndStructure

    Structure WKSTA_INFO_100
        wki100_platform_id.l  ;Indicates level to use for get platform-specific info.
        wki100_computername.l;Contains name of local computer in Unicode
        wki100_langroup.l;Contains domain computer belongs to in Unicode
        wki100_ver_major.l;Holds Major version number of OS on local computer
        wki100_ver_minor.l;Holds Minor version number of OS on local computer
    EndStructure

    #MAX_PREFERRED_LENGTH              = -1
    #SV_TYPE_ALL =$FFFFFFFF
    #NERR_SUCCESS                      = 0
    #ERROR_MORE_DATA                   = 234

    Procedure GetServerList()

    se101.SERVER_INFO_101
    nStructSize = SizeOf(SERVER_INFO_101)
    RetCode = NetServerEnum_(0, 101, @bufptr, #MAX_PREFERRED_LENGTH, @dwEntriesread, @dwTotalentries, #SV_TYPE_ALL, 0, @dwResumehandle)
    If RetCode = #NERR_SUCCESS And RetCode <> #ERROR_MORE_DATA
     ;Loop through And the Data in the memory
     For i = 0 To dwEntriesread - 1
      CopyMemory( bufptr + (nStructSize * i),@se101, nStructSize)
      Buffer.s=Space(512)
      Result=WideCharToMultiByte_(#CP_ACP ,0,se101\lpszServerName,255,@Buffer.s,512,0,0)
      AddGadgetItem(#ListviewGadget, -1,Buffer)
     Next
    EndIf
    NetApiBufferFree_(bufptr)
    EndProcedure

    Procedure.s GetLocalSystemName()
    twkstaInfo100.WKSTA_INFO_100
    lwkstaInfo100.l
    nStructSize=SizeOf(WKSTA_INFO_100)
    Result= NetWkstaGetInfo_(0, 100, @lwkstaInfo100)
    If Result=0
     CMResult=CopyMemory( lwkstaInfo100,@twkstaInfo100, nStructSize)
     Buffer.s=Space(512)
     Result=WideCharToMultiByte_(#CP_ACP ,0,twkstaInfo100\wki100_computername,-1,@Buffer.s,512,0,0)
     ProcedureReturn Trim(Buffer)
    Else
     ProcedureReturn "DAMM :("
    EndIf
    EndProcedure

    Procedure NTSendMessage(NTFrom.s,NTTo.s,NTMessage.s)
    Buffer1 = AllocateMemory( Len(NTTo)*2, 0)
    Result=MultiByteToWideChar_(#CP_ACP ,0,NTTo,-1,Buffer1,Len(NTTo)*2)

    Buffer2 = AllocateMemory( Len(NTFrom)*2, 0)
    Result=MultiByteToWideChar_(#CP_ACP ,0,NTFrom,-1,Buffer2,Len(NTFrom)*2)

    buf.s="MeineNachricht"
    buflen.l=Len(NTMessage)

    Buffer3 = AllocateMemory( Len(NTMessage)*2, 0)
    Result=MultiByteToWideChar_(#CP_ACP ,0,NTMessage.s,-1,Buffer3,buflen*2)

    Result=NetMessageBufferSend_(0,Buffer1,Buffer2,buffer3,buflen*2)
    FreeMemory(Buffer1)
    FreeMemory(Buffer2)
    FreeMemory(Buffer3)

    EndProcedure

    Localname.s=GetLocalSystemName()
    ;-Main
    WindowID = OpenWindow(0, 200, 200, 400,120  , "Send NT-Message  Local:"+Localname.s, #PB_Window_SystemMenu   );+GetLocalSystemName())
    If WindowID

      StringGadget(#String1,130,20,265,40,"This is a Testmessage",#PB_Text_Center)
      ButtonGadget(#Button1,130,100,100,19,"SEND")
      ButtonGadget(#Button2,230,100,100,19,"Refresh")
      ListViewGadget(#ListViewGadget,2,20,120,100)
      TextGadget(#Text1,2,1,100,16,"Users")
      TextGadget(#Text2,120,1,100,16,"Message")
      GetServerlist()

      Repeat
        EventID = WaitWindowEvent()
        If EventID = #PB_Event_Gadget
          GadgetID=EventGadget()
          Select GadgetID
           Case #Button1
            Pos=GetGadgetState(#Listviewgadget)
            If Pos>-1
             NTTo.s=GetGadgetItemText(#Listviewgadget,pos,0)
             
             NTSendMessage(Localname,NTTo.s,GetGadgetText(#String1))
            Else
             Beep_(100,50)
            EndIf
           Case #Button2
            GetServerlist()
           EndSelect
        EndIf
      Until EventID = #PB_Event_CloseWindow
 
    EndIf
Its a long way to the top if you wanna .....CodeGuru
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by tranquil.

Hi Rings!

Crashes after compiling on Win 2000 Server.

Mike

Tranquilizer/ Secretly!
http://www.secretly.de
Registred PureBasic User
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Rings.

remove GetLocalname in Titlebar for testing

Its a long way to the top if you wanna .....CodeGuru
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Rings.

This is an updated version
A small (but heavy) bug in an Apicall is changed.
So this snippet should work also as an exe :)

Code: Select all

;NetSend; Another Snippet by Siegfried Rings (C) 2002 CodeGuru
; This one retrieves all usernames from local Network
; and can send a message to them(Works only NT,2000 or XP , Nor Win9x, ME tested)

#Button1=1
#Button2=2
#ListViewGadget=3
#String1=4
#Text1=5
#Text2=6

Structure SERVER_INFO_101
      dwPlatformId.l
      lpszServerName.l
      dwVersionMajor.l
      dwVersionMinor.l
      dwType.l
      lpszComment.l
EndStructure

Structure WKSTA_INFO_100
    wki100_platform_id.l  ;Indicates level to use for get platform-specific info.
    wki100_computername.l;Contains name of local computer in Unicode
    wki100_langroup.l;Contains domain computer belongs to in Unicode
    wki100_ver_major.l;Holds Major version number of OS on local computer
    wki100_ver_minor.l;Holds Minor version number of OS on local computer
EndStructure

#MAX_PREFERRED_LENGTH              = -1
#SV_TYPE_ALL =$FFFFFFFF
#NERR_SUCCESS                      = 0
#ERROR_MORE_DATA                   = 234

Procedure GetServerList() 
ClearGadgetItemList(#ListViewGadget) 
se101.SERVER_INFO_101
nStructSize = SizeOf(SERVER_INFO_101)
RetCode = NetServerEnum_(0, 101, @bufptr, #MAX_PREFERRED_LENGTH, @dwEntriesread, @dwTotalentries, #SV_TYPE_ALL, 0, @dwResumehandle)
If RetCode = #NERR_SUCCESS And RetCode  #ERROR_MORE_DATA 
 ;Loop through And the Data in the memory
 For i = 0 To dwEntriesread - 1
  CopyMemory( bufptr + (nStructSize * i),@se101, nStructSize)
  Buffer.s=Space(512)
  Result=WideCharToMultiByte_(#CP_ACP ,0,se101\[url]mailto:lpszServerName,255,@Buffer.s,512,0,0[/url])
  AddGadgetItem(#ListviewGadget, -1,Buffer)
 Next
EndIf
NetApiBufferFree_(bufptr)
EndProcedure

Procedure.s GetLocalSystemName()
twkstaInfo100.WKSTA_INFO_100
lwkstaInfo100.l
nStructSize=SizeOf(WKSTA_INFO_100)
Result= NetWkstaGetInfo_(0, 100, @lwkstaInfo100)
If Result=0
 CMResult=CopyMemory( lwkstaInfo100,@twkstaInfo100, nStructSize)
 Buffer.s=Space(512)
 Result=WideCharToMultiByte_(#CP_ACP ,0,twkstaInfo100\[url]mailto:wki100_computername,-1,@Buffer.s,512,0,0[/url])
 ProcedureReturn Trim(Buffer)
Else
 ProcedureReturn "DAMM :("
EndIf 
EndProcedure

Procedure NTSendMessage(NTFrom.s,NTTo.s,NTMessage.s)
Buffer1 = AllocateMemory(1, Len(NTTo)*2, 0) 
Result=MultiByteToWideChar_(#CP_ACP ,0,NTTo,-1,Buffer1,Len(NTTo)*2)

Buffer2 = AllocateMemory(2, Len(NTFrom)*2, 0) 
Result=MultiByteToWideChar_(#CP_ACP ,0,NTFrom,-1,Buffer2,Len(NTFrom)*2)

buf.s="MeineNachricht"
buflen.l=Len(NTMessage)

Buffer3 = AllocateMemory(3, Len(NTMessage)*2, 0) 
Result=MultiByteToWideChar_(#CP_ACP ,0,NTMessage.s,-1,Buffer3,buflen*2)

Result=NetMessageBufferSend_(0,Buffer1,Buffer2,buffer3,buflen*2)
FreeMemory(1) 
FreeMemory(2) 
FreeMemory(3) 

EndProcedure

Localname.s=GetLocalSystemName()
;-Main
WindowID = OpenWindow(0, 200, 200, 400,120, #PB_Window_SystemMenu     , "Send NT-Message  Local:"+Localname.s);+GetLocalSystemName()) 
If WindowID
 If CreateGadgetList(WindowID)
  StringGadget(#String1,130,20,265,40,"This is a Testmessage",#PB_Text_Center)
  ButtonGadget(#Button1,130,100,100,19,"SEND")
  ButtonGadget(#Button2,230,100,100,19,"Refresh")
  ListViewGadget(#ListViewGadget,2,20,120,100)
  TextGadget(#Text1,2,1,100,16,"Users")
  TextGadget(#Text2,120,1,100,16,"Message")
  GetServerlist()

  Repeat
    EventID = WaitWindowEvent()
    If EventID = #PB_EventGadget
      GadgetID=EventGadgetID()
      Select GadgetID
       Case #Button1
        Pos=GetGadgetState(#Listviewgadget)
        If Pos>-1
         NTTo.s=GetGadgetItemText(#Listviewgadget,pos,0)
         
         NTSendMessage(Localname,NTTo.s,GetGadgetText(#String1))
        Else
         Beep_(100,50)
        EndIf
       Case #Button2
        GetServerlist()
       EndSelect 
    EndIf
  Until EventID = #PB_EventCloseWindow
 EndIf
EndIf
Its a long way to the top if you wanna .....CodeGuru
Lucifer
User
User
Posts: 26
Joined: Wed Apr 30, 2003 6:11 pm
Location: UK

Post by Lucifer »

Can this code be modified to retrieve not only the Computer Name but also the Username of the currently logged on user and the OS and service pack running on that PC. Can we also find out what admin group they are in?
User avatar
Rings
Moderator
Moderator
Posts: 1435
Joined: Sat Apr 26, 2003 1:11 am

Post by Rings »

Lucifer wrote:Can this code be modified to retrieve not only the Computer Name but also the Username of the currently logged on user and the OS and service pack running on that PC. Can we also find out what admin group they are in?
sure, learn and study the Network-API from M$ and implement yourself,
or wait until i release the MakeApp() version. ;)
And another hint, most of your question's are already answered here (or tips on purearea.net) .
use your brain :)
SPAMINATOR NR.1
thefool
Always Here
Always Here
Posts: 5875
Joined: Sat Aug 30, 2003 5:58 pm
Location: Denmark

Post by thefool »

Use whaT??
Lucifer
User
User
Posts: 26
Joined: Wed Apr 30, 2003 6:11 pm
Location: UK

Post by Lucifer »

So I should be able to get the user logged on by using NetWkstaUserEnum and query the workstation in question. If I use the WKSTA_USER_INFO_1 structure I can even find out the authenticating server.
Post Reply