Join Unjoin Domain or Workgroup

Share your advanced PureBasic knowledge/code with the community.
TeddyLM
Enthusiast
Enthusiast
Posts: 133
Joined: Wed Apr 30, 2003 2:04 pm
Location: Germany (French expat)

Join Unjoin Domain or Workgroup

Post by TeddyLM »

Hello

Maybe somebody is interrested in this:

Code: Select all

Prototype.l NetJoinDomain(Server.p-unicode, Domain.p-unicode, AccountOU.p-unicode, Account.p-unicode, Password.p-unicode, JoinOptions)
Prototype.l NetUnjoinDomain(Server.p-unicode, Account.p-unicode, Password.p-unicode, UnjoinOptions)
Prototype.l NetJoinWorkgroup(Server.p-unicode, Domain.p-unicode, AccountOU.p-unicode, Account.p-unicode, Password.p-unicode, JoinOptions)
Prototype.l NetGetJoinInformation(Host.p-unicode, bufptr, dwBufferType)

;===========================
Procedure.s _GetSystemMessage(ErrorCode.l)
    Buffer.STRING\s = Space(255)
    tchar.l = FormatMessage_(#FORMAT_MESSAGE_FROM_SYSTEM, #Null, ErrorCode, 0, @Buffer\s, 255, #Null)
    If tchar > 0
        Buffer\s = Trim(Left(Buffer\s, tchar))
    Else
        Buffer\s = ""
    EndIf
    ProcedureReturn Buffer\s
EndProcedure
;==========================
Procedure.s NET_JoinDomain(Hostname$, Domain$, AccountOU$, Account$, Password$, JoinOptions.l) 
    Hostname$ = "\\" + RemoveString(Hostname$, "\")
    Result$ = "Error - Library could not be opened"
    LibNr.l = OpenLibrary(#PB_Any, "NetApi32.dll") 
    If LibNr
        JoinDomain.NetJoinDomain = GetFunction(LibNr, "NetJoinDomain")        
        If JoinDomain <> 0
            ErrorCode.l = JoinDomain(Hostname$, Domain$, AccountOU$, Account$, Password$, JoinOptions)
            If ErrorCode = 0
                Result$ = UCase(Hostname$) + " has joined the domain " + UCase(Domain$)
            Else
                Result$ = "Error - (" + Str(ErrorCode) + ") " + _GetSystemMessage(ErrorCode) 
            EndIf      
        EndIf   
        CloseLibrary(LibNr)        
    EndIf    
    ProcedureReturn Result$
EndProcedure
;Debug NET_JoinDomain("Computername", "domain.com", "OU=XXXXX,DC=domain,DC=com", "Admin@domain.com", "AdminPassword", $21)
;==========================
Procedure.s NET_UnjoinDomain(Hostname$, Account$, Password$, UnjoinOptions.l) 
    Hostname$ = "\\" + RemoveString(Hostname$, "\")
    Result$ = "Error - Library could not be opened"
    LibNr.l = OpenLibrary(#PB_Any, "NetApi32.dll") 
    If LibNr
        UnjoinDomain.NetUnjoinDomain = GetFunction(LibNr, "NetUnjoinDomain")        
        If UnjoinDomain <> 0
            ErrorCode.l = UnjoinDomain(Hostname$, Account$, Password$, UnjoinOptions)
            If ErrorCode = 0
                Result$ = "Unjoined"
            Else
                Result$ = "Error - (" + Str(ErrorCode) + ") " + _GetSystemMessage(ErrorCode) 
            EndIf
        EndIf   
        CloseLibrary(LibNr)        
    EndIf    
    ProcedureReturn Result$
EndProcedure
;NET_UnjoinDomain("Computername", "Admin@domain.com", "AdminPassword", 0)
;==========================
Procedure.s NET_JoinWorkgroup(Hostname$, Domain$, AccountOU$, Account$, Password$, JoinOptions.l) 
    Hostname$ = "\\" + RemoveString(Hostname$, "\")
    Result$ = "Error - Library could not be opened"
    LibNr.l = OpenLibrary(#PB_Any, "NetApi32.dll") 
    If LibNr
        JoinWorkgroup.NetJoinWorkgroup = GetFunction(LibNr, "NetJoinDomain")        
        If JoinWorkgroup <> 0
            ErrorCode.l = JoinWorkgroup(Hostname$, Domain$, AccountOU$, Account$, Password$, JoinOptions)
            If ErrorCode = 0
                Result$ = UCase(Hostname$) + " has joined the group " + UCase(Domain$)
            Else
                Result$ = "Error - (" + Str(ErrorCode) + ") " + _GetSystemMessage(ErrorCode) 
            EndIf
        EndIf   
        CloseLibrary(LibNr)        
    EndIf    
    ProcedureReturn Result$
EndProcedure
;(Computer must not be in a domain. Check with GetJoinInfo and if necessary unjoin from domain first)
;NET_JoinWorkgroup("Computername", "WORKGROUP", "", "LocalAdmin", "LocalAdminPassword", $20)
;==========================
Procedure.s NET_GetJoinInformation(Hostname$) 
    Hostname$ = "\\" + RemoveString(Hostname$, "\")
    Result$ = "Status is unknown"
    LibNr.l = OpenLibrary(#PB_Any, "NetApi32.dll") 
    If LibNr
        dwBufferType.l
        JoinInfo.NetGetJoinInformation = GetFunction(LibNr, "NetGetJoinInformation")
        If JoinInfo <> 0
            ErrorCode.l = JoinInfo(Hostname$, @*bufptr, @dwBufferType)
            If ErrorCode = 0
                Select dwBufferType
                    Case 1: Result$ = "Not joined to any domain or group" 
                    Case 2: Result$ = "Joined to the group: " + PeekS(*bufptr, -1, #PB_Unicode) 
                    Case 3: Result$ = "Joined to the domain: " + PeekS(*bufptr, -1, #PB_Unicode)
                EndSelect
            Else
                Result$ = "Error - (" + Str(ErrorCode) + ") " + _GetSystemMessage(ErrorCode) 
            EndIf
        EndIf
        NetApiBufferFree_(*bufptr)   
        CloseLibrary(LibNr)        
    EndIf    
    ProcedureReturn Result$
EndProcedure
GYP
User avatar
em_uk
Enthusiast
Enthusiast
Posts: 366
Joined: Sun Aug 08, 2010 3:32 pm
Location: Manchester UK

Re: Join Unjoin Domain or Workgroup

Post by em_uk »

Amazing, I was just working on something like this today using COMateplus.

Great stuff. I'd love to see some of you other code :D Really helpful.

Have you done anything with AD?
----

R Tape loading error, 0:1
TeddyLM
Enthusiast
Enthusiast
Posts: 133
Joined: Wed Apr 30, 2003 2:04 pm
Location: Germany (French expat)

Re: Join Unjoin Domain or Workgroup

Post by TeddyLM »

User avatar
em_uk
Enthusiast
Enthusiast
Posts: 366
Joined: Sun Aug 08, 2010 3:32 pm
Location: Manchester UK

Re: Join Unjoin Domain or Workgroup

Post by em_uk »

TeddyLM wrote:
Have you done anything with AD?
http://www.purebasic.fr/english/viewtop ... 12&t=46398
Brilliant stuff, thanks for sharing.
----

R Tape loading error, 0:1
Post Reply