诗词
字典
板报
句子
名言
励志
学校
友答
搜索
首页
中考频道
作文频道
公务员频道
出国留学
医药考试
司法考试
图书频道
外语考试
建筑工程
成人高考
故事频道
教程频道
文档频道
早教
星座频道
校园
求职招聘
考研频道
职业资格
自考频道
计算机考试
财会考试
高考频道
当前位置:
首页
>
教程频道
>
开发语言
>
编程
>
闲来无事 练习题基础知识
2012-06-21
闲来无事 练习基础知识1、创建进程:STARTUPINFO si si.cb sizeof(si) memset(&si,0,sizeof(si))PROCES
闲来无事 练习基础知识
1、创建进程:
STARTUPINFO si ; si.cb = sizeof(si); memset(&si,0,sizeof(si)); PROCESS_INFORMATION pi; si.wShowWindow = TRUE; BOOL bRet = CreateProcess(NULL,"notepad.exe",NULL,NULL,FALSE,NULL,NULL,NULL,&si,&pi); if (bRet) { CString str; str.Format("%d",pi.dwThreadId); }
2、列举进程
PROCESSENTRY32 pe32; pe32.dwSize = sizeof(pe32); HANDLE hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0); if (hProcessSnap == INVALID_HANDLE_VALUE) { sprintf("%s\n","创建快照失败!"); return ; } BOOL bMore = ::Process32First(hProcessSnap,&pe32); while(bMore) { bMore = Process32Next(hProcessSnap,&pe32); sprintf(" 进程名称:%s 进程ID:%d;",pe32.szExeFile,pe32.th32ProcessID); } ::CloseHandle(hProcessSnap); return ;
3、时间
struct tm *pstr; time_t t; t = time(NULL); pstr = localtime(&t); int yyyy = (pstr->tm_year) + 1900; // 年份是从1900开始 int mm = (pstr->tm_mon) + 1; // 月是从0-11,所以要加1 int dd = (pstr->tm_mday) ; // 日是从1 开始 printf("%d年%d月%d日",yyyy,mm,dd);
4、显示关机对话框
typedef int (CALLBACK *SHUTDOWNDLG)(int); //显示关机对话框函数的指针 HINSTANCE hInst = LoadLibrary("shell32.dll"); //装入shell32.dll SHUTDOWNDLG ShutDownDialog; //指向shell32.dll库中显示关机对话框函数的指针 if(hInst != NULL) { //获得函数的地址并调用之 ShutDownDialog = (SHUTDOWNDLG)GetProcAddress(hInst,(LPSTR)60); (*ShutDownDialog)(0); }
5、结构体指针数组
#include <stdio.h>#include <string.h>#include <malloc.h>#include <memory.h>// 定义结构体typedef struct tag_STUDENT { char name[120]; int ID; int age;}STUDENT,*LPSTUDENT;// 函数声明void outputfun(LPSTUDENT pStu[3]);void setdatafun() ; // 自定义void setdatafun() { char *str = "*** 夜深人静,此情此刻我心依然 ! ***"; int i = 2; LPSTUDENT stu[3]; for (i=2; i>=0;i--) { stu[i] = (LPSTUDENT)malloc(sizeof(tag_STUDENT)); memset(stu[i],0,sizeof(tag_STUDENT)); stu[i]->age = 1008611 + i; stu[i]->ID = 181 + i; memset(stu[i]->name,0,sizeof(stu[i]->name)); strcpy(stu[i]->name,str); } // 把 i 个 指针数组 传进去outputfun函数 outputfun(stu); for (i=2; i>=0;i--) { free(stu[i]); }} //////////////////////////////////////////////////////////////////////////// 结构体指针数组,LPSTUDENT pStu[3] 表示pStu指向3个都为LPSTUDENT类型指针的数组。void outputfun(LPSTUDENT pStu[3]){ LPSTUDENT lpstu[3]; int i; for (i=0; i<3; i++) { lpstu[i] = (LPSTUDENT)malloc(sizeof(tag_STUDENT));// 申请空间 memset(lpstu[i],0,sizeof(tag_STUDENT)); // 内存清零 memcpy(lpstu[i],pStu[i],sizeof(tag_STUDENT));// 数组复制 printf("%s\n"," "); printf("===============第 %d 个数组=========\n",i+1); printf("age = %d\n",lpstu[i]->age); printf("ID = %d\n",lpstu[i]->ID ); printf("name = %s\n",lpstu[i]->name); // 释放内存空间 free(lpstu[i]); } }int main(int argc, char* argv[]){ setdatafun() ;return 0;}