Page 1 of 1

Join Unjoin Domain or Workgroup

Posted: Thu May 05, 2011 3:29 pm
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

Re: Join Unjoin Domain or Workgroup

Posted: Thu May 12, 2011 10:57 pm
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?

Re: Join Unjoin Domain or Workgroup

Posted: Thu May 19, 2011 8:40 am
by TeddyLM

Re: Join Unjoin Domain or Workgroup

Posted: Thu May 19, 2011 9:37 pm
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.