Using NSUserName lazarus / 33-macOS - Mac OS X ---------------------------------------------------------------------- ProgEd · 2013-07-22 20:43 I'm trying to get the user name on a Mac running OS X. This is used to validate what the user can and cannot do in the application. In Windows I have no problem, but I cannot seem to get it to work on the Mac. In the program I've defined: function NSUserName: CFStringRef: external name "_NSUserName'; And I get an error saying that fields cannot appear after a method or property definition, start a new visibility section first. I'm not sure what that means in this context. Ideas? Suggestions? Ed ---------------------------------------------------------------------- jwdietrich · 2013-07-22 22:08 Many roads lead to Rome. Usually, there are several methods in Mac OS X that pave the way to a given goal. Although there may be a way to employ NSUserName, I use a different approach in my applications. The environment variable USER holds the name of the user that invoked the application. It is available on all Unix systems. The following code is a platform-independent solution that makes use of this approach. It delivers the user name on Linux, Mac OS X and Windows: Code: [Select]var   gUserName: string; begin   {$IFDEF UNIX}   gUserName := GetEnvironmentVariable('USER');   {$ELSE}   GetUserName(UserName, arraySize);   gUserName := string(UserName);   {$ENDIF} end; ---------------------------------------------------------------------- ProgEd · 2013-07-22 22:31 Thanks, That's precisely what I needed! Ed ---------------------------------------------------------------------- jwdietrich · 2013-07-22 23:06 Sorry, I forgot to deliver the definition of arraySize that is essential for Windows: arraySize: DWord; The corrected code snippet would be: Code: [Select]var   gUserName: string;   arraySize: DWord; begin   {$IFDEF UNIX}   gUserName := GetEnvironmentVariable('USER');   {$ELSE}   GetUserName(UserName, arraySize);   gUserName := string(UserName);   {$ENDIF} end; ---------------------------------------------------------------------- BigChimp · 2013-07-23 05:44 Quote from: ProgEd on July 22, 2013, 10:43:07 pmAnd I get an error saying that fields cannot appear after a method or property definition, start a new visibility section first. That just mean you should reorder the definitions of your functions, methods and properties in your class definition... or add the same (public, private, or protected) identifier again and specify your function.