1. Avory Rose Pastoral
  2. PowerBuilder
  3. Thursday, 7 March 2024 23:59 PM UTC

Hi,

 

I'm using PowerBuilder 12.5 Classic and want to retrieve the username/computer name of the client upon login.

 

I have tried some scripts, but none of them are working. I need help with this. Any suggestions would be very much appreciated.

Thank you.

John Fauss Accepted Answer Pending Moderation
  1. Friday, 8 March 2024 03:50 AM UTC
  2. PowerBuilder
  3. # 1

Hi, Avory - 

You'll need two external function declarations, one for each of the two Windows API functions that will need to be called. If you have never declared and used an external function, here is some online help that may help (it's for PB 2022 R3, but external function declarations work the same in PB 12.5):

    https://docs.appeon.com/pb2022r3/application_techniques/Using_external_functions.html#Declaring_external_functions

Define the two external function declarations in the window or user object that will be finding and setting these two values:

FUNCTION Boolean GetComputerName ( &
   REF String       lpBuffer, &
   REF UnsignedLong nSize &
   ) LIBRARY "kernel32.dll" ALIAS FOR "GetComputerNameW"

FUNCTION Boolean GetUserName ( &
   REF String       lpBuffer, &
   REF UnsignedLong nSize &
   ) LIBRARY "advapi32.dll" ALIAS FOR "GetUserNameW"

Create a window/object function to invoke each API function:

// Function of_GetComputerName() Returns String

// No. of characters (not including the terminating null).
Constant UnsignedLong MAX_COMPUTERNAME_LENGTH = 31

Boolean	lb_rc
ULong   lul_size
String 	ls_name

// Pre-allocate space in a string that will receive the computer's name
lul_size = MAX_COMPUTERNAME_LENGTH
ls_name  = Space(lul_size)
lul_size = lul_size + 1 // Add 1 for the null character that ends each string.

// Obtain the name of the computer via a Windows API function.
// This API function returns True if successful.
lb_rc = This.GetComputerName(ls_name,lul_size)

If Not lb_rc Then
   Return ''
End If

Return ls_name
// Function of_GetUserName() Returns String

// No. of characters (not includeing the terminating null) in the user's name.
Constant UnsignedLong MAX_USERNAME_LENGTH = 255

boolean	lb_rc
ulong   lul_size = MAX_USERNAME_LENGTH
string 	ls_name

// Pre-allocate space in a string that will receive the user's ID/name.
ls_name = Space(lul_size)
lul_size = lul_size + 1  // Add 1 for the null character that ends each string.

// Obtain the user's name via a Windows API function.
// This API function returns True if successful.
lb_rc = This.GetUserName(ls_name,lul_size)

If Not lb_rc Then
   Return ""
End If

Return ls_name

Best regards, John

Comment
  1. Avory Rose Pastoral
  2. Tuesday, 12 March 2024 04:05 AM UTC
Hello,



I have created a function of_getcomputername and of_getusername both are having Unknown function name: of_getcomputername and of_getusername error.
  1. Helpful
There are no comments made yet.
Avory Rose Pastoral Accepted Answer Pending Moderation
  1. Tuesday, 12 March 2024 04:13 AM UTC
  2. PowerBuilder
  3. # 2

Hi I have managed to resolve the error now my problem is how can I display the result in login window.

Comment
  1. John Fauss
  2. Wednesday, 13 March 2024 02:21 AM UTC
I can't even begin to respond, as you have supplied so little information. Are you displaying these pieces of information as (read-only) text in the login window, or in input fields (such as single-line edit controls), or in a data entry-type of DataWindow? What is the big picture that you are wanting to accomplish? Please supply some context. Please also provide some idea of your level of expertise with PB.

And, in order to manage expectations, I will let you know now that I cannot write the code for you... that is NOT what we do here. A code snippet (like what was provided earlier to illustrate a point or technique) is one thing and I was happy to provide that in my previous response, but coding solutions for you is what paid consultants are for.
  1. Helpful 2
  1. Avory Rose Pastoral
  2. Wednesday, 13 March 2024 03:13 AM UTC
Thanks for your help. Glad to share with you that everything is working now. :) Thank you so much
  1. Helpful
There are no comments made yet.
Daniel Seguin Accepted Answer Pending Moderation
  1. Saturday, 17 August 2024 14:50 PM UTC
  2. PowerBuilder
  3. # 3

Hello,

Here is an alternative solution to get the username and computer name, you can create a global function which you can call upon opening the application.

string ls_userName
string ls_cmd1, ls_cmd2
integer li_return = 1   // 1= ok or -1 = not ok
string ls_currentdir
integer li_ufile, li_cfile, li_b1file, li_b2file
integer li_readstatus
string ls_ufile, ls_cfile 
string ls_script
string ls_cr = char(13)

// getuser.bat and getcomputer.bat will be generated in order to create the following files: username.txt and computername.txt
ls_currentdir = GetCurrentDirectory()
ls_cmd1 = trim(ls_currentdir) + "\" + "getuser.bat"
ls_cmd2 = trim(ls_currentdir) + "\" + "getcomputer.bat"

ls_script = "echo %USERNAME% > " + trim(gs_tmpdir) + "\username.txt"
li_b1file = FileOpen(ls_cmd1, TextMode!, Write!, LockWrite!, Replace!, EncodingANSI! )
li_return = FileWriteEx(li_b1file, ls_script)
FileClose(li_b1file)
if li_return > 0 then
   li_return = run(ls_cmd1)
   if li_return = -1 then
      return li_return
   end if
else
   return li_return
end if

ls_script = "echo %USERDOMAIN% > " + trim(gs_tmpdir) + "\computername.txt"
li_b2file = FileOpen(ls_cmd2, TextMode!, Write!, LockWrite!, Replace!, EncodingANSI! )
li_return = FileWriteEx(li_b2file, ls_script)
FileClose(li_b2file)
if li_return > 0 then
   li_return = run(ls_cmd2)
   if li_return = -1 then
     return li_return
   end if
else
   return li_return
end if

ls_ufile = trim(gs_tmpdir) + "\username.txt"
ls_cfile = trim(gs_tmpdir) + "\computername.txt"

// get the username variable 
if FileExists(ls_ufile) then
   li_ufile = fileOpen(ls_ufile,LineMode!,Read!)
   li_readstatus = FileReadEx(li_ufile,gs_username)
   if li_readstatus <= 0 then
      // default user name to system since could not get user name from the workstation
      gs_username = "system"	
   end if
   FileClose(li_ufile)
   FileDelete(ls_ufile)	
else
  // default user name to system since could not get user name from the workstation
  gs_username = "system"	
end if

// get the computer name variable
if FileExists(ls_cfile) then
   li_cfile = fileOpen(ls_cfile,LineMode!,Read!)
   li_readstatus = FileReadEx(li_cfile,gs_computername)
   if li_readstatus <= 0 then
      // default user name to system since could not get user name from the workstation
      gs_computername = "unknown"	
   end if
   FileClose(li_cfile)
   FileDelete(ls_cfile)	
else
   // default user name to system since could not get user name from the workstation
   gs_computername = "unknown"	
end if

return li_return
Comment
There are no comments made yet.
  • Page :
  • 1


There are no replies made for this question yet.
However, you are not allowed to reply to this question.