To lock your machine programmatically you can use the API LockWorkStation().
BOOL WINAPI LockWorkStation(void);
// Failed to lock the machine
This function has the same result as pressing Ctrl+Alt+Del and clicking Lock Workstation. To unlock the workstation, the user must log in. There is no function you can call to determine whether the workstation is locked.