Mega Code Archive

 
Categories / Delphi / Examples
 

Username

I found that GetUserName (in Win95/98) returns the userid of the last user who logged on to Windows on the machine. If another user decides not to log on (i.e. presses Cancel on the login screen); starts the program that calls GetUserName, the program will "think" that the previous user is still logged on. Meaning : if no user is logged on, the user data is not reset. (hope you understand what my point is here) For Novell you could use something like this : (it's a snippet from a component I wrote, but I'm sure you'll know how to adapt it to your own needs) uses CalWin32, ClxWin32, NetWin32, Delphi_u; var FUserID : String; connRef : nuint32; buffer : Array[1..MaxSize] of char; nwver : NWCCVersion; userID : nuint32; connHandle : NWCONN_HANDLE; ctxFlags : nuint32; objType : nuint16; scanIterator : nuint32; context : NWDSContextHandle; cCode : NWCCODE; dsCode : NWDSCCODE; tmp : nptr; PosDot, i : Integer; begin // Get Netware UserId try tmp := Nil; cCode := NWCallsInit(tmp, tmp); if cCode<>0 then Raise EDBEngineError.CreateFmt('Error initializing Novell client',[]); scanIterator := 0; (* Use NWCCScanConnRefs() to retrieve a server connection *) cCode := NWCCScanConnRefs(@scanIterator, @connRef); if cCode<> 0 then Raise EDBEngineError.CreateFmt('No server connection found',[]); { Get tree name from the connection reference } cCode := NWCCGetConnRefInfo(connRef, NWCC_INFO_TREE_NAME, SizeOf(buffer), @buffer); if cCode<> 0 then Raise EDBEngineError.CreateFmt('No tree context found',[]); // we'll not overwrite this buffer, sinc we're going to need the tree name later { Get server version from the connection reference } cCode := NWCCGetConnRefInfo(connRef, NWCC_INFO_SERVER_VERSION, SizeOf(nwver), @nwver); if cCode<> 0 then Raise EDBEngineError.CreateFmt('Server version not found',[]); { Get the user ID and name from the connection reference } FUserID := ''; cCode := NWCCGetConnRefInfo(connRef, NWCC_INFO_USER_ID, SizeOf(userID), @userID); if cCode<> 0 then Raise EDBEngineError.CreateFmt('User id not found',[]); { Having the user ID, we can now get the user name from bindery or NDS ... first the connection number for the target server, then query for this user name } cCode := NWCCOpenConnByRef(connRef, NWCC_OPEN_UNLICENSED, NWCC_RESERVED, @ConnHandle); if cCode<> 0 then Raise EDBEngineError.CreateFmt('Connection number for target server not found',[]); if cCode=0 then case nwver.major of 2..3: begin { bindery method to read user name and last login time } cCode := NWGetObjectName(connHandle, userID, @buffer, @objType); if (cCode=0) then FuserID := StrPas(@buffer) else FUserid:=''; end; 4..99: begin NWDSCreateContextHandle(context); { Create an NDS context } if context<>ERR_CONTEXT_CREATION then begin { if we could be attached to multiple trees, we need to set it } NWDSSetContext(context, DCK_TREE_NAME, @buffer); { use typeless, fully specified names } NWDSGetContext(context, DCK_FLAGS, @ctxFlags); ctxFlags := (ctxFlags or DCV_TYPELESS_NAMES); ctxFlags := (ctxFlags AND NOT DCV_CANONICALIZE_NAMES); NWDSSetContext(context, DCK_FLAGS, @ctxFlags); { Map the user ID to the user name } dsCode := NWDSMapIDToName(context, connHandle, userID, @buffer); if dsCode=0 then FUserID := StrPas(@buffer); NWDSFreeContext(context); end; end; end; {case} PosDot:=Pos('.', FUserID); if PosDot<>0 then SetLength(FUserID, PosDot - 1); FUserID:=UpperCase(FUserID); HTH Bert Bert De Ridder Analyst/DBA Nissan Belgium Disclaimer : The opinions expressed in this message are my own, and are not necessarily those of NV Nissan Belgium. >>> <h.ketting@kpn.com> 06/10/2000 07:38:08 >>> Checked code in Delphi3 and I had to modify the 'count' type : procedure TForm1.Button1Click(Sender: TObject); VAR Temp : PChar; Count : dword; begin Count := 0; GetUserName(nil,Count); Temp := StrAlloc(Count); GetUserName(Temp, Count); caption := StrPas(Temp); StrDispose(Temp); end; Works fine on Novell; but I do not know about Linux rgds -----Original Message----- From: Martin Jambor [mailto:JamborM@gytool.cz] Sent: donderdag 5 oktober 2000 18:41 To: delphi@elists.org Subject: Re: Network User Name? Will this work on Novell Networks, or users logged into some Liinux/Unix box? Or is it NT Specific :( ? TIA Martin Jambor GenJerDan wrote: > > > Further to saving multiple user INI files: How do I determine the current > > network user name? > > I do it this way: > > VAR > Temp : PChar; > Count : cardinal; > begin > Count := 0; > GetUserName(nil,Count); > Temp := StrAlloc(Count); > GetUserName(Temp, Count); > UserName := StrPas(Temp); > StrDispose(Temp); > end; _______________________________________________ Delphi mailing list -> Delphi@elists.org http://elists.org/mailman/listinfo/delphi _______________________________________________ Delphi mailing list -> Delphi@elists.org http://elists.org/mailman/listinfo/delphi _______________________________________________ Delphi mailing list -> Delphi@elists.org http://elists.org/mailman/listinfo/delphi