
C Language
JieLinDee
天道酬勤,厚积薄发.对Go语言有兴趣的朋友可以加:259693140(此群不允许闲聊)
展开
-
Golang1.8编译静态库给C使用
Go实例代码:package mainimport ( "fmt")import "C"//export Printffunc Printf(format, str string) { fmt.Printf(format, str)}func main(){}编译命令: go build -ldflags “-s -w” -buildmode=c-archive -o pr原创 2017-05-23 10:19:36 · 4269 阅读 · 0 评论 -
Golang1.7.3实现启动单一实例代码片段
var ( NameMutex = "Shamem" kernel = syscall.NewLazyDLL("kernel32.dll"))const ( IPC_RMID = 0 IPC_CREAT = 00001000 IPC_EXCL = 00002000 IPC_NOWAIT = 00004000)func Lock_wi原创 2016-11-25 18:12:39 · 1108 阅读 · 0 评论 -
Golang1.7使用CGO在Go代码中定义C函数
test.h文件内容:int test(void* fn);void println(char* str);void callback(void* fn);test.c文件内容:#include <test.h>int test(void* fn){ callback(fn); println("Hello,This from Clang"); return 0;原创 2016-10-10 10:26:44 · 3827 阅读 · 0 评论 -
Golang1.7动态库的使用,C和Golang的动态库以及使用
项目目录root@working-srv:/data/code/src# tree project workspace/project└── pro.goworkspace/└── main.gomain.gopackage mainimport ( "project")func main() { project.Hello() pro原创 2016-09-10 15:59:45 · 9165 阅读 · 0 评论 -
Golang1.7使用kernel32.dll 读取共享内存,代码小示例
package mainimport ( "fmt" "syscall" "unsafe")func main() { ShareMemName, _ := syscall.BytePtrFromString("ShareMemory") //ShareMemory共享内存名称. var sharememname = uintptr(unsafe.Point原创 2016-07-21 20:14:17 · 2071 阅读 · 2 评论 -
Go和C类型对应关系
C.charC.schar (signed char)C.uchar (unsigned char)C.shortC.ushort (unsigned short)C.intC.uint (unsigned int)C.longC.ulong (unsigned long)C.longlong (long long)C.ulonglong (unsigned long long原创 2016-04-17 21:33:58 · 3690 阅读 · 0 评论 -
Linux下使用C杀死进程.
#include#include#include#include#include void main(int argc,char *argv[]){ int num; num=atoi(argv[1]); int status; status=kill(num,SIGKILL); if (status == -1) printf("kill faild\n"); wai原创 2016-06-01 13:35:08 · 5362 阅读 · 0 评论 -
Linux计算cpu使用率
计算总的 CPU 使用率 TOTALCPUUSE1)采样两个足够短的时间间隔的 CPU 快照,即读取 /proc/stat 文件,获取两个时间点的下列数据:CPUT1 (user1, nice1, system1, idle1, iowait1, irq1, softirq1, stealstolen1, guest1);CPUT2 (user2, nice2, system2, idl原创 2016-03-10 10:16:19 · 2727 阅读 · 0 评论 -
C语言获取Windows总CPU使用率.
#include #include #include double FileTimeToDouble(FILETIME* pFiletime){ return (double)((*pFiletime).dwHighDateTime * 4.294967296E9) + (double)(*pFiletime).dwLowDateTime;}double m_fOldCPUIdl原创 2016-01-25 09:56:55 · 4530 阅读 · 0 评论 -
Go语言使用CGO获取Windows的CPU使用率
package main/*#include #include #include double FileTimeToDouble(FILETIME* pFiletime){ return (double)((*pFiletime).dwHighDateTime * 4.294967296E9) + (double)(*pFiletime).dwLowDateTime;}do原创 2016-01-25 09:55:20 · 3874 阅读 · 1 评论 -
Linux下GO语言内存共享,CGO实现
package ceimport ( "errors" "fmt" "os" "os/exec")/*#include #include #include #include #include int Size;int Shm_id;key_t Key;char *Shm_add;int getMem(char *pathname,int size){原创 2015-12-02 16:07:32 · 1290 阅读 · 0 评论 -
C 从标准输入读取字符串.
#includeint main(){ char c; while((c=getchar())!='\n'){ printf("%c",c); } return 0;}原创 2015-10-13 08:40:34 · 3628 阅读 · 0 评论 -
Go1.10用Go函数控制C函数的生命周期
package mainimport ( "context" "syscall" "time" "unsafe")/*int goexit(void* func){ ((int (*)())func)(); return 0;}*/import "C"func main() { go func() { ...原创 2018-03-20 16:48:28 · 431 阅读 · 0 评论