github.com/cellofellow/gopkg@v0.0.0-20140722061823-eec0544a62ad/osext/_demo_windows.cc (about) 1 // Copyright 2012 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 #include <stdio.h> 6 #include <stdlib.h> 7 #include <signal.h> 8 9 #include <windows.h> 10 11 bool LockFile(const char* name, HANDLE* handle) { 12 *handle = INVALID_HANDLE_VALUE; 13 HANDLE f = CreateFileA(name, 14 GENERIC_READ | GENERIC_WRITE, 15 0, NULL, OPEN_ALWAYS, 16 0, NULL 17 ); 18 if(f == INVALID_HANDLE_VALUE) return false; 19 *handle = f; 20 return true; 21 } 22 23 bool UnlockFile(HANDLE handle) { 24 return handle == INVALID_HANDLE_VALUE || CloseHandle(handle) != FALSE; 25 } 26 27 HANDLE flock; 28 void sigHandle(int sig) { 29 switch(sig) { 30 case SIGINT: 31 printf("sigHandle: %d, SIGINT\n", sig); 32 break; 33 default: 34 printf("sigHandle: %d, OTHER\n", sig); 35 break; 36 } 37 UnlockFile(flock); 38 exit(1); 39 } 40 41 int main() { 42 if(LockFile("demo.lock", &flock)) { 43 printf("Lock Success!\n"); 44 } else { 45 printf("Lock Failed!\n"); 46 } 47 signal(SIGINT, sigHandle); 48 for(;;) {} 49 return 0; 50 }