I know this is possible, because I have a binary I wrote a couple years ago to do exactly this. Unfortunately, I didn't save the source code.
I know that it was written in pure C, and I called against an OS X API and was able to lock the screen. Googling for "pure C OS X lock screen" and similar strings isn't getting me much; I saw documentation on how to do it in Objective C but that's not what I'm looking for.
Anyone have any idea how I accomplished this previously?
You're probably thinking of CGSCreateLoginSession()
-- it's a private function and not documented by Apple. See this question for an example of its use.