05 2014 档案
CreateMutex实现只能打开一个客户端
摘要:#include "stdafx.h"#include #include using namespace std;int _tmain (int argc, LPTSTR argv[]){ HANDLE h=CreateMutex(NULL,TRUE,"AAAAAAA"); if(Get... 阅读全文
posted @ 2014-05-30 14:30 上海—Michael 阅读(324) 评论(0) 推荐(0)
IOCP编程原理(转)
摘要:在我的博客之前写了很多关于IOCP的“行云流水”似的看了让人发狂的文章,尤其是几篇关于 IOCP加线程池文章,更是让一些功力不够深厚的初学IOCP者,有种吐血的感觉。为了让大家能够立刻提升内力修为,并且迅速的掌握IOCP这个 Windows平台上的乾坤大挪移心法,这次我决定给大家好好补补这个基础。要... 阅读全文
posted @ 2014-05-29 18:06 上海—Michael 阅读(1284) 评论(0) 推荐(0)
I/O完成端口(IOCP)
摘要:服务器: #include "stdafx.h"#include #pragma comment(lib, "ws2_32.lib")#include using namespace std;// 单句柄数据typedef struct tagPER_HANDLE_DATA{ SOCKET Sock... 阅读全文
posted @ 2014-05-28 18:12 上海—Michael 阅读(355) 评论(0) 推荐(0)
CreateWaitableTimer和SetWaitableTimer
摘要:负值表示相对时间,正值表示绝对时间,定时器精度为100ns (1ns=1/10亿 s),所以 -50000000 代表5秒,详见MSDN。程序一为自动重置(先等待5秒,然后每1秒输出一次):#include "stdafx.h"#include#include#includeusing namesp... 阅读全文
posted @ 2014-05-27 16:03 上海—Michael 阅读(1203) 评论(0) 推荐(0)
Winsock 传输文件
摘要:文件传输的原理:发送方把文件读到socket发送端缓冲区中,接收方把socket接收端缓端冲区中的数据写到一个新文件中。当然了,大文件需要循环读写!服务器端为发送端:#include "stdafx.h"#include#include#pragma comment(lib,"ws2_32.lib"... 阅读全文
posted @ 2014-05-23 22:15 上海—Michael 阅读(705) 评论(0) 推荐(0)
windows服务控制(开启/停止已有服务)
摘要:#include "stdafx.h"#include #include #include #include #pragma comment(lib, "advapi32.lib")TCHAR szCommand[10];TCHAR szSvcName[80];SC_HANDLE schSCMana... 阅读全文
posted @ 2014-05-23 14:17 上海—Michael 阅读(629) 评论(0) 推荐(0)
windows服务编写和“以管理员运行”程序的方法
摘要:本文将首先解释如何 创建 一个定期查询可用物理内存并将结果写入某个文本文件的服务。然后指导你完成生成,安装和实现服务的整个过程。第一步:主函数和全局定义首先,包含所需的头文件。例子要调用 Win32 函数(windows.h )和磁盘文件写入(stdio.h ):接着,定义两个常量:#define ... 阅读全文
posted @ 2014-05-22 23:07 上海—Michael 阅读(1757) 评论(0) 推荐(0)
c++如何编写线程安全的DLL
摘要:DLL有个共同的特点就是都有一个初始化函数,一个资源释放函数,其他几个函数都是核心功能函数。而且这些DLL有时会被多个进程同时调用,这就牵扯到多进程的多线程调用DLL的问题。有点绕口,以下我根据我实践中遇到的问题,分四种情况分享一下我解决此类问题的经验:1、动态库只有一个导出函数。这种情况非常少,也... 阅读全文
posted @ 2014-05-21 18:05 上海—Michael 阅读(1579) 评论(0) 推荐(0)
Socket中常见的几个转换函数(htonl,htons,ntohl,ntohs,inet_addr,inet_ntoa)
摘要:Socket中常见的几个转换函数(htonl,htons,ntohl,ntohs,inet_addr,inet_ntoa) htonl() htons() ntohl() ntohs()及inet_ntoa() inet_addr()的用法 注:其中的h表示“host”,n表示“net”,l表... 阅读全文
posted @ 2014-05-21 10:41 上海—Michael 阅读(1754) 评论(0) 推荐(0)
Winsock 示例
摘要:#include "stdafx.h"#include #include #pragma comment(lib,"ws2_32.lib")using namespace std;int main(int argc, char* argv[]){ WSADATA wsaData; int... 阅读全文
posted @ 2014-05-20 22:47 上海—Michael 阅读(563) 评论(0) 推荐(0)
命名管道的使用方式:消息模式/字节模式
摘要:转自:http://blog.sina.com.cn/s/blog_71b3a9690100usem.html由于自己在写进程间通信的相关程序,查阅了关于资料。觉得命名管道方法实现通信是不错的选择,可是对于其使用方式却有诸多的不解。同步方式、异步方式、重叠方式………..有很多的不解。首先是关于Wai... 阅读全文
posted @ 2014-05-20 12:29 上海—Michael 阅读(3056) 评论(0) 推荐(1)
命名管道-简单的例子
摘要:#include "stdafx.h"#include#include#includeusing namespace std;DWORD WINAPI thread1(LPVOID param){ char buf[256]; DWORD rlen=0; HANDLE hPipe ... 阅读全文
posted @ 2014-05-19 16:46 上海—Michael 阅读(802) 评论(0) 推荐(0)
命名管道-MSDN例子
摘要:服务器: 1 #include "stdafx.h" 2 #include 3 #include 4 #include 5 #include 6 7 #define BUFSIZE 512 8 9 DWORD WINAPI InstanceThread(LPVOI... 阅读全文
posted @ 2014-05-19 15:34 上海—Michael 阅读(796) 评论(0) 推荐(0)
_tprintf(), printf(),wprintf() 与控制字符 %s 和 %S(Unicoe与GB2312))
摘要:_tprintf() 是 printf() 和 wprintf() 的通用类型;如果定义了 _unicode,那么 _tprintf() 就会转换为 wprintf(),否则为 printf() 。在这 3 个函数中有两个字符串控制输出字符为 %s 和 %S 。使用它们时,有如下区别:%s当使用 p... 阅读全文
posted @ 2014-05-19 15:22 上海—Michael 阅读(11720) 评论(0) 推荐(1)
命名管道与匿名管道
摘要:匿名管道父进程#include#includemain(){HANDLEread=NULL,write=NULL;//定义两句柄SECURITY_ATTRIBUTESss;STARTUPINFOsa={0};PROCESS_INFORMATIONpp={0};//定义结构体SECURITY_ATTR... 阅读全文
posted @ 2014-05-18 00:00 上海—Michael 阅读(1029) 评论(0) 推荐(0)
Windows条件变量
摘要:详细见MSDN:http://msdn.microsoft.com/en-us/library/windows/desktop/ms686903%28v=vs.85%29.aspx我们已经看到,当想让写入者线程和读取者线程以独占的方式或共享的方式访问一个资源的时候,可以使用SRWLock。在这些情况... 阅读全文
posted @ 2014-05-15 11:24 上海—Michael 阅读(1822) 评论(0) 推荐(0)
openmp并行计算
摘要:要在Visual C++2012 中使用OpenMP其实不难,只要将 Project 的Properties中C/C++里Language的OpenMP Support开启(参数为 /openmp),就可以让VC++2005 在编译时支持OpenMP 的语法了;而在编写使用OpenMP 的程序时,则... 阅读全文
posted @ 2014-05-14 10:51 上海—Michael 阅读(439) 评论(0) 推荐(0)
编程中的命名设计那点事
摘要:转自:http://coolshell.cn/articles/990.html在设计过程中给类,方法和函数好的命名会带来好的设计,虽然这不是一定成立,但是如果坏的命名那一定不会给你带来好的设计。在设计过程,如果你发现 你很难命名某一个模块,某个方法时,可能你真正遇到的问题不是难命名的问题,而是这个... 阅读全文
posted @ 2014-05-14 09:46 上海—Michael 阅读(232) 评论(0) 推荐(0)
线程池的使用
摘要:#include "stdafx.h"#include #include #include using namespace std;INT i;VOID CALLBACK Fun(PTP_CALLBACK_INSTANCE Instancd,PVOID Context,PTP_WORK Work){... 阅读全文
posted @ 2014-05-13 18:04 上海—Michael 阅读(754) 评论(0) 推荐(0)
SRW锁的使用
摘要:SRWLock的目的和关键段相同:对一个资源进行保护,不让其它线程访问它。但是,与关键段不同的是,SRWLock允许我们区分哪些想要读取资源的值 的线程(读取者线程)和想要更新资源的值的线程(写入者线程)。让所有的读取者线程在同一时刻访问共享资源应该是可行的,这是因为仅仅读取资源的值并不存 在破坏数... 阅读全文
posted @ 2014-05-13 10:34 上海—Michael 阅读(711) 评论(0) 推荐(0)
内存屏障
摘要:内存屏障是指“由于编译器的优化和缓存的使用,导致对内存的写入操作不能及时的反应出来,也就是说当完成对内存的写入操作之后,读取出来的可能是旧的内容”(摘自《独辟蹊径品内核》)。内存屏障的分类:编译器引起的内存屏障缓存引起的内存屏障乱序执行引起的内存屏障1、编译器引起的内存屏障:我们都知道,从寄存器里面... 阅读全文
posted @ 2014-05-10 22:37 上海—Michael 阅读(549) 评论(0) 推荐(0)
VC用Beep整几首歌听听~~~
摘要://生日快乐歌#include "stdafx.h"#include void main(void) { unsigned FREQUENCY[] = {392,392,440,392,523,494, 392,392,440,392,587,523, 392,392,784,659,52... 阅读全文
posted @ 2014-05-09 14:12 上海—Michael 阅读(385) 评论(0) 推荐(0)
简单的多线程并发同步演示(4种同步方法)
摘要:转自:http://blog.csdn.net/morewindows/article/details/7392749 相交进程之间的关系主要有两种,同步与互斥。所谓互斥,是指散步在不同进程之间的若干程序片断,当某个进程运行其中一个程序片段时,其它进程就不能运行它 们之中的任一程序片段,只能等到该... 阅读全文
posted @ 2014-05-08 18:05 上海—Michael 阅读(542) 评论(0) 推荐(0)
C语言生成程序问题
摘要:问题:我用VS2013写好C语言程序调试运行后就在debug文件夹下生成了EXE文件,可以在本机运行。但是这个EXE文件在别的没装过VS2013的电脑上就不能直接运行,说丢失MSVCR120D.dll文件。我重装系统后,没来得及安装VS2013,那些生成好的EXE时也都出现了上述问题。由此,我认为是... 阅读全文
posted @ 2014-05-08 10:54 上海—Michael 阅读(398) 评论(0) 推荐(0)
文件操作(输出倒数第二行、逆序输出)
摘要:1.输出倒数第二行#include "stdafx.h"#include #include #include using namespace std;int main(){ //cin.imbue(locale("chs")); cout.imbue(locale("chs"));//控制台输出中文... 阅读全文
posted @ 2014-05-07 17:05 上海—Michael 阅读(1121) 评论(0) 推荐(0)
CreateRemoteThread 远程dll注入
摘要:1.dll中的内容// dllmain.cpp : 定义 DLL 应用程序的入口点。#include "stdafx.h"BOOL APIENTRY DllMain( HMODULE hModule, DWORD ul_reason_for_call, ... 阅读全文
posted @ 2014-05-06 23:12 上海—Michael 阅读(526) 评论(0) 推荐(0)
CreateRemoteThread 远程注入
摘要:在release中可以成功,在debug中被注入的程序停止工作#pragma once#include "stdafx.h"#include #include #include //线程参数结构体定义typedef struct _RemoteParam { char szMsg[12]; //Me... 阅读全文
posted @ 2014-05-06 15:54 上海—Michael 阅读(343) 评论(0) 推荐(0)
CreateThread demo
摘要:#include "stdafx.h"#include#include//win2003SDK必须安装 要不无此头文件。此文件是为了实现StringCchPrintf,StringCchLength。#define MAX_THREADS 5#define BUF_SIZE 255typedef s... 阅读全文
posted @ 2014-05-06 12:23 上海—Michael 阅读(386) 评论(0) 推荐(0)
简单的windows作业管理(自己也没弄透彻)
摘要:先把代码贴出来,以后有时间再研究!简单的说,作业就相当于沙箱,可以使程序在一定范围内活动。#include "stdafx.h"#include "windows.h"#include using namespace std;SECURITY_ATTRIBUTES sa;STARTUPINFO si... 阅读全文
posted @ 2014-05-05 22:41 上海—Michael 阅读(479) 评论(0) 推荐(0)
createprocess并行运算
摘要:#include "stdafx.h"#include "windows.h"#include using namespace std;int _tmain(int argc, _TCHAR* argv[]){ STARTUPINFO si; PROCESS_INFORMATION... 阅读全文
posted @ 2014-05-04 20:52 上海—Michael 阅读(262) 评论(0) 推荐(0)
演示通过环境变量在不同进程间传递数据
摘要:#include "stdafx.h"#include "windows.h"#include using namespace std;int _tmain(int argc, _TCHAR* argv[]){ STARTUPINFO si; PROCESS_INFORMATION... 阅读全文
posted @ 2014-05-04 17:22 上海—Michael 阅读(485) 评论(0) 推荐(0)