The functions you are looking for are ExitWindowsEx and LockWorkstation, respectively.
ex:
ExitWindowsEx (EWX_LOGOFF, SHTDN_REASON_MINOR_OTHER);
The second parameter of this function is the reason for the "shutdown" which may generate system events, logs, etc. depending on what you choose. Check the MSDN for more details.
LockWorkstation();
This one is pretty easy!
Hope this helps,
Alex