飘云阁

 找回密码
 加入我们

QQ登录

只需一步,快速开始

查看: 4042|回复: 2

[驱动源码] 虚拟设备(包括鼠标和键盘)

[复制链接]
  • TA的每日心情
    慵懒
    2020-9-27 20:02
  • 签到天数: 7 天

    [LV.3]偶尔看看II

    发表于 2012-4-7 11:06:00 | 显示全部楼层 |阅读模式
    源码发布概况
    编程语言: vc++
    详细描述: 此虚拟键盘驱动与虚拟鼠标驱动
    网址: http://bbs.chinapyg.com/
    本帖最后由 爱民 于 2012-4-7 11:14 编辑

    1. #include "stdafx.h"
    2. #include <windows.h>
    3. #include <vector>
    4. #include <setupapi.h>
    5. #include "c:\ntddk\inc\newdev.h"

    6. #pragma comment(lib , "C:\\NTDDK\\libchk\\i386\\newdev.lib")
    7. #pragma comment(lib , "Setupapi.lib")

    8. using namespace std;

    9. ///////////////////////////////////////////////////////////////////////////

    10. //GUID_CLASS_KEYBOARD
    11. GUID KeyboardClass = {0x4D36E96B,0xE325,0x11CE,0xBF,0xC1,0x08,0x00,0x2B,0xE1,0x03,0x18};

    12. // Hardware ID
    13. #define HardwareIDs "VKeyboard\0\0"
    14. #define DeviceName "Virtual Keyboard"

    15. int DisplayMsg(LPCTSTR msg)
    16. {
    17. return MessageBox(0,msg,"Message",MB_OK);
    18. }

    19. BOOL FindExistingDevice()
    20. {
    21. HDEVINFO DeviceInfoSet = SetupDiGetClassDevs(0,0,0,DIGCF_ALLCLASSES|DIGCF_PRESENT|DIGCF_PROFILE);
    22. if (DeviceInfoSet == INVALID_HANDLE_VALUE)
    23. {
    24. return false;
    25. }

    26. bool Found = false;

    27. SP_DEVINFO_DATA DeviceInfoData;
    28. DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);

    29. for (DWORD i=0;SetupDiEnumDeviceInfo(DeviceInfoSet,i,&DeviceInfoData);i++)
    30. {
    31. char buf[10240] = {0};
    32. DWORD buffersize = sizeof(buf);
    33. DWORD DataT = 0;

    34. if(!SetupDiGetDeviceRegistryProperty(DeviceInfoSet,&DeviceInfoData,
    35. SPDRP_HARDWAREID,
    36. &DataT,
    37. (PBYTE)buf,
    38. buffersize,
    39. &buffersize))
    40. {
    41. continue;
    42. }

    43. for (char* p=buf;*p;p++)
    44. {
    45. if (strcmp(HardwareIDs,p) == 0)
    46. {
    47. Found = true;
    48. break;
    49. }
    50. for(;*p;p++);
    51. }

    52. if (Found)
    53. {
    54. break;
    55. }
    56. }

    57. SetupDiDestroyDeviceInfoList(DeviceInfoSet);

    58. return Found;
    59. }

    60. int IsFileExist(LPCTSTR filename)
    61. {
    62. WIN32_FIND_DATA FindFileData;
    63. HANDLE hFind = FindFirstFile(filename, &FindFileData);
    64. if(hFind == INVALID_HANDLE_VALUE)
    65. {
    66. return false;
    67. }

    68. FindClose(hFind);

    69. return true;
    70. }

    71. int APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
    72. {
    73. if(FindExistingDevice())
    74. {
    75. DisplayMsg("Virtual Keyboard already installed");
    76. return 0;
    77. }

    78. char FileName[MAX_PATH];
    79. GetModuleFileName(hInstance,FileName,sizeof(FileName));

    80. char Drive[MAX_PATH],Path[MAX_PATH];
    81. _splitpath(FileName,Drive,Path,0,0);

    82. char infile[MAX_PATH];
    83. char sysfile[MAX_PATH];
    84. _makepath(infile,Drive,Path,"keyfdo",".inf");
    85. _makepath(sysfile,Drive,Path,"VKeyFdo",".sys");

    86. if(!IsFileExist(infile))
    87. {
    88. DisplayMsg("Can't find .INF file");
    89. return 0;
    90. }

    91. if(!IsFileExist(sysfile))
    92. {
    93. DisplayMsg("Can't find .SYS file");
    94. return 0;
    95. }

    96. HDEVINFO top;
    97. top=SetupDiCreateDeviceInfoList(&KeyboardClass,NULL);
    98. if (top == INVALID_HANDLE_VALUE)
    99. {
    100. DisplayMsg("Can't get device infolist");
    101. return 0;
    102. }

    103. SP_DEVINFO_DATA dev;
    104. dev.cbSize=sizeof(SP_DEVINFO_DATA);
    105. int status=SetupDiCreateDeviceInfo(top, DeviceName, &KeyboardClass,
    106. DeviceName,
    107. NULL, DICD_GENERATE_ID, &dev);

    108. if(!status)
    109. {
    110. DisplayMsg("Can't create device");
    111. return 0;
    112. }

    113. status=SetupDiRegisterDeviceInfo(top,&dev,0,NULL,NULL,NULL);

    114. if(!status)
    115. {
    116. DisplayMsg("Can't register device");
    117. return 0;
    118. }

    119. unsigned char idstr[256] = {0};
    120. memcpy(idstr,HardwareIDs,sizeof(idstr));
    121. status=SetupDiSetDeviceRegistryProperty(top, &dev, SPDRP_HARDWAREID,idstr, sizeof(idstr));

    122. if(!status)
    123. {
    124. DisplayMsg("Can't set device HardwareID");
    125. return 0;
    126. }

    127. SetupDiDestroyDeviceInfoList(top);

    128. int RebootRequired;
    129. status=UpdateDriverForPlugAndPlayDevices(GetDesktopWindow(),(LPCTSTR)idstr,infile,0,&RebootRequired);

    130. return 0;
    131. }
    复制代码



    本帖子中包含更多资源

    您需要 登录 才可以下载或查看,没有账号?加入我们

    x
    PYG19周年生日快乐!
  • TA的每日心情
    开心
    2024-7-17 08:58
  • 签到天数: 178 天

    [LV.7]常住居民III

    发表于 2012-4-9 13:00:56 | 显示全部楼层
    老大。。建议加上点注释。。如果能说明功能的实现就更好了。
    PYG19周年生日快乐!
  • TA的每日心情
    开心
    2024-10-27 12:49
  • 签到天数: 371 天

    [LV.9]以坛为家II

    发表于 2012-8-19 08:54:31 | 显示全部楼层
    是的这样太笼统了
    PYG19周年生日快乐!
    您需要登录后才可以回帖 登录 | 加入我们

    本版积分规则

    快速回复 返回顶部 返回列表