一.获取进程句柄

参考:http://wenku.baidu.com/view/286c30d084254b35eefd3466.html

二.获取父进程ID

http://www.vckbase.com/bbs/prime/viewprime.asp?id=337

typedef LONG (WINAPI *PROCNTQSIP)(HANDLE,UINT,PVOID,ULONG,PULONG);PROCNTQSIP NtQueryInformationProcess;DWORD GetParentProcessID(DWORD dwId);void main(int argc, char* argv[])
{
NtQueryInformationProcess = (PROCNTQSIP)GetProcAddress(GetModuleHandle("ntdll"),"NtQueryInformationProcess");if (!NtQueryInformationProcess)return;DWORD dwId=2984;
    printf("Parent PID for %lu is %lu\n",dwId,GetParentProcessID(dwId));}DWORD GetParentProcessID(DWORD dwId)
{LONG                      status;DWORD                     dwParentPID = (DWORD)-1;HANDLE                    hProcess;PROCESS_BASIC_INFORMATION pbi;// Get process handlehProcess = OpenProcess(PROCESS_QUERY_INFORMATION,FALSE,dwId);if (!hProcess)return (DWORD)-1;// Retrieve informationstatus = NtQueryInformationProcess( hProcess,ProcessBasicInformation,(PVOID)&pbi,sizeof(PROCESS_BASIC_INFORMATION),NULL);// Copy parent Id on successif  (!status)dwParentPID = pbi.InheritedFromUniqueProcessId;CloseHandle (hProcess);return dwParentPID;
}

三.Unicode和ANSI互转

来自windows核心编程

  1. Call MultiByteToWideChar, passing NULL for the pWideCharStr parameter and 0 for the cchWideChar parameter and -1 for the cbMultiByte parameter.

  2. Allocate a block of memory large enough to hold the converted Unicode string. This size is computed based on the value returned by the previous call to MultiByteToWideChar multiplied by sizeof(wchar_t).

  3. Call MultiByteToWideChar again, this time passing the address of the buffer as the pWideCharStr parameter and passing the size computed based on the value returned by the first call to MultiByteToWideChar multiplied by sizeof(wchar_t) as the cchWideChar parameter.

  4. Use the converted string.

  5. Free the memory block occupying the Unicode string.

MultiByteToWideChar的使用

char btyeStr[]="hello";
DWORD nLenOfWideCharStr = MultiByteToWideChar(CP_ACP, 0,btyeStr, -1, NULL, 0);
PWSTR pWideCharStr = (PWSTR)HeapAlloc(GetProcessHeap(), 0,nLenOfWideCharStr * sizeof(wchar_t));
ZeroMemory(pWideCharStr,nLenOfWideCharStr * sizeof(wchar_t));
MultiByteToWideChar(CP_ACP, 0,btyeStr, sizeof(btyeStr), pWideCharStr, nLenOfWideCharStr);
HeapFree(GetProcessHeap(), 0, pWideCharStr);

WideCharToMultiByte的使用

char btyeStr[]="hello";
int length=(int)strlen(btyeStr);
ZeroMemory(btyeStr,length);
WideCharToMultiByte(CP_ACP, 0, pWideCharStr, nLenOfWideCharStr * sizeof(wchar_t),btyeStr, length, NULL, NULL);

四.WaitForInputIdle

A thread can also suspend itself by calling WaitForInputIdle:

DWORD WaitForInputIdle(HANDLE hProcess,DWORD dwMilliseconds);

This function waits until the process identified by hProcess has no input pending in the thread that created the application's first window. This function is useful for a parent process. The parent process spawns a child process to do some work. When the parent process' thread calls Create-Process, the parent's thread continues to execute while the child process initializes. The parent's thread might need to get the handle of a window created by the child. The only way for the parent's thread to know when the child process has been fully initialized is to wait until the child is no longer processing any input. So after the call to CreateProcess, the parent's thread places a call to WaitForInputIdle.

参考:

http://www.cnblogs.com/carekee/articles/1748718.html

五.I/O完成端口

理解这个东西花了点时间.

通过两次调用CreateIoCompletionPort方法,其会将一个设备与完成端口关联起来,在内部I/O完成端口会创建一个队列,那么当你对I/O进行读写的时候就可以用GetQueuedCompletionStatus函数来接受,
注意:如果没有读写I/O的话,才GetQueuedCompletionStatus将会阻塞,这其实就是异步I/O完成端口等待的功能,还可以利用PostQueuedCompletionStatus方法向I/O完成端口发送传递信息,其中会有一个键值作为标识,
否则你将无法知道是哪个操作(比如读和写),还要注意的是队列中的执行顺序.

参考:http://www.codeproject.com/KB/IP/IOCompletionPort.aspx

使用CreateIoCompletionPort创建一个I/O完成端口,这里指定了hSource,那么默认将会与hSource关联,然后再次与hDestination关联(CreateIoCompletionPort函数名并不恰当)

hSource = CreateFile(argv[1],GENERIC_READ | GENERIC_WRITE,NULL,NULL,OPEN_EXISTING,FILE_FLAG_OVERLAPPED | FILE_FLAG_NO_BUFFERING,NULL);
fileSize = GetFileSize(hSource,NULL);
hDestination = CreateFile(argv[2],GENERIC_READ | GENERIC_WRITE,NULL,NULL,OPEN_EXISTING,FILE_FLAG_OVERLAPPED | FILE_FLAG_NO_BUFFERING,NULL);
// Extending the size of the destination file. Read MSDN for more detail.
destFileSize = (fileSize + pageSize - 1) & ~(pageSize-1);
DWORD dwStatus = SetFilePointer(hDestination,destFileSize,NULL,FILE_BEGIN);
BOOL bReturn = SetEndOfFile(hDestination);
// Associate Source file with IO Completion Port.
hIOCompPort = CreateIoCompletionPort(hSource,NULL,read,10);
// Associate Destination File with IO Completion Port.
hIOCompPort = CreateIoCompletionPort(hDestination,hIOCompPort,write,10);

线程中的方法,GetQueuedCompletionStatus函数,大多数参数均为输出的.

DWORD WINAPI ThreadProc(LPVOID lpParameter)
{BOOL  bSuccess;DWORD bytesTransferred;DWORD key;LPOVERLAPPED overlappedComp;// Worker Thread will be waiting on IO Completion Port // to process IO Completion packets.bSuccess = GetQueuedCompletionStatus(hIOCompPort,&bytesTransferred,&key,&overlappedComp,(DWORD)-1);if( !bSuccess && (overlappedComp == NULL)){cout<<"::GetQueuedCompletionStatus Failed::"<<endl;ExitThread(-1);}
}

读写I/O,在main函数中WaitForMultipleObjects是为了防止程序退出,纯演示作用

// Spawing writer threads which will wait for IO packets to be dispatched
// on IO Completion Port.
for (int iThreads = 0; iThreads < g_NoOfThreads; iThreads++)
{lpHandle[iThreads] = CreateThread(NULL,NULL,ThreadProc,NULL,0,NULL);
}
CopySourceToDestination(fileSize);WaitForMultipleObjects(g_NoOfThreads,lpHandle,TRUE,60000);CloseHandle(hDestination);
CloseHandle(hSource);

CopySourceToDestination是真正操作I/O,调用ReadFile异步方法,完成后GetQueuedCompletionStatus就会收到通知,也可以用PostQueuedCompletionStatus函数发送

bSuccess = ReadFile(hSource,lpBufferData[iReadAsynOperations].lpData,BUFFER_SIZE,&bytesRead,&lpBufferData[iReadAsynOperations].overLapped);

另外请参考Windows核心编程,不过其做了部分的封装,学习过程中还是不要做太多封装比较好.

转载于:https://www.cnblogs.com/Clingingboy/archive/2011/05/20/2052478.html

Visual C++ 2011-5-18相关推荐

  1. Visual Studio 2011开发者预览版发布

    关键词:Visual,Studio | 作者:虫虫 | 收藏这篇资讯 Visual Studio 2011,也叫"vNext"是微软的下一代IDE.它覆盖了软件开发的整个生命周期, ...

  2. http://down.qiannao.com/space/file/qiannao/share/2011/1/18/yy.rar/.page

    http://down.qiannao.com/space/file/qiannao/share/2011/1/18/yy.rar/.page

  3. 2011/5/18工作笔记

    追求极致的系统效能 一.IDC(互联网数据中心)计算效率,采用PUE(电源使用效率)= 数据中心总设备能耗/IT设备能耗,PUE是一个比率,基准是2,越接近1表明能效水平越好. 二.应用系统的性能优化 ...

  4. sap 获取计划订单bapi_【原创】2011.09.18 SAP 订单中修改订单净价

    本文以销售订单为例,说一下如何使用SAP预置的BAPI来更新销售订单(SD)的净价. 1.前提 首先需要了解一下销售订单所对应的表的关系: 订单抬头表:VBAK--订单行项目表:VBAP. 我们所要修 ...

  5. 一个简单的媒体文件播放器 2011.07.18

    这两天看 Directshow开发指南 ,一直在为filter的事情学习. 看到第五章的时候,有个播放器例子,研究了一下还不算难, 就跟着人家写了一遍. 相对来说,比直接用那个用 ActiveMovi ...

  6. 读者2011年18期_卷首语_一期一会

          大津秀一  译/语妍 有一个患者,当他在京都病得很严重,快不行了的时候,他的朋友从北海道.九州.美国等地飞来看望他.朋友们围在他的床前,他强打精神,努力想把朋友们的样子牢牢记在心底. 其实 ...

  7. 更换VS.NET 2010的皮肤 [Visual Studio Blog]

    写在前面      最近的工作变动使自己的主要工作集中在Visual Studio的扩展开发上,其实这方面我已经关注和研究很长时间了,过程中发现了不少相关的资源,细到具体的技术方面:VSX,Vs.NE ...

  8. 分享Silverlight/WPF/Windows Phone/HTML5一周学习导读(12月12日-12月18日)

    分享Silverlight/WPF/Windows Phone/HTML5一周学习导读(12月12日-12月18日) 本周Silverlight学习资源更新 Silverlight学习小记 阿里山地方 ...

  9. 我眼中的Visual Studio 2010架“.NET研究”构工具

    影响架构质量的是构建体系架构的思想.原则.实践与架构师的经验,绝不是工具.即使是最优秀的架构工具,也不可能像倚天宝剑一般--倚天一出,谁与争锋--似乎谁握住了这把利刃,就能够成为武林盟主.架构工具可以 ...

  10. Visual Studio 2010开发AutoCAD 2012 .net 应用程序调试时断点不起作用

    AutoCAD 2012系列产品很快就要正式发布了,对于.net 开发者来说,VS2010无疑是最好用的工具,所以也有好多人在VS2010和AutoCAD 2012 beta版上做开发测试.你在开发时 ...

最新文章

  1. 【zabbix学习笔记之二】部署zabbix-server端
  2. 电脑账户头像怎么删掉_电脑用户账户头像在哪里更改?怎么更改?
  3. 多重 for 循环,如何提高效率?
  4. android调用webservice发送header身份验证不成功
  5. Python:[-1]、[:-1]、[::-1]、[n::-1] 原理大详解(超全超仔细!)
  6. shell截取字符串的8种方法
  7. 数据库工具DBeaver
  8. 做微商如何快速加好友找到客源
  9. listing directory /
  10. 微信小程序毕业设计 基于微信校园二手交易信息小程序系统开题报告
  11. 每日三思:优化微信小程序中倒计时占内存较大(19-0612-1917)
  12. tf.RaggedTensor
  13. (c++)五分制成绩(函数实现)
  14. iOS软件开发实现类似微信上传图片选择
  15. SpringBoot程序排除@Configuration配置类
  16. java生成word(报告报表)含统计图表图片、循环表格,Spring Boot整合word生成
  17. Nordic nRF52内部DCDC使能
  18. Cannotnbsp;sendnbsp;sessionnbsp;cachenbsp;limite…
  19. Android应用更新(一)
  20. CAN总线波特率计算及设置方法(STM32,SJA1000,LPC2292)

热门文章

  1. 动态规划---01背包问题(2种方法)
  2. 2017 ACM-ICPC南宁网络赛: G. Finding the Radius for an Inserted Circle
  3. bzoj 3040: 最短路(road)(堆优化dijkstra)
  4. Java 利用泛型实现折半查找法
  5. k8s优先级priority的使用
  6. Prometheus Alertmanager报警组件
  7. json标注工具与labelme安装
  8. wget 和scp对比_curl与wget区别
  9. Nginx编译./configure翻译
  10. 【转】深入理解JVM—JVM内存模型