安全黑客
当前位置:安全黑客文章资讯编程技术VC/C++编程
日期:2011-03-22 13:55:00  来源:本站整理

用SetupAPI结合注册表获得USB优盘序列号[VC/C++编程]

赞助商链接



  本文“用SetupAPI结合注册表获得USB优盘序列号[VC/C++编程]”是由安全黑客为您精心收集,来源于网络转载,文章版权归文章作者所有,本站不对其观点以及内容做任何评价,请读者自行判断,以下是其具体内容:
//---------------------------------------------------------------------------
#include<setupapi.h>
#include<stdio.h>
#include<objbase.h>
#ifndef__BORLANDC__
#pragmacomment(lib,"setupapi.lib")
#endif
//---------------------------------------------------------------------------
charg_strCrnVerify[]=
{
  0xBB,0xB6,0xD3,0xAD,0xB9,0xE2,0xC1,0xD9,0x20,0x43,
  0x2B,0x2B,0x42,0x75,0x69,0x6C,0x64,0x65,0x72,0xD1,
  0xD0,0xBE,0xBF,0x20,0x2D,0x20,0x68,0x74,0x74,0x70,
  0x3A,0x2F,0x2F,0x77,0x77,0x77,0x2E,0x63,0x63,0x72,
  0x75,0x6E,0x2E,0x63,0x6F,0x6D
};
//---------------------------------------------------------------------------
voidCrnReplaceString(LPSTRlpBuf,charc1,charc2)
{
  intnLen=strlen(lpBuf);
  LPSTRlpTemp=newchar[nLen];
  memset(lpTemp,0,nLen);
  intj=0;
  for(inti=0;i<nLen;i++)
  {
    if(lpBuf[i]==c1)
    {
      if(c2!=0x00)
      {
        lpTemp[j]=c2;
        j++;
      }
    }
    else
    {
      lpTemp[j]=lpBuf[i];
      j++;
    }
  }
  //
  memset(lpBuf,0,nLen);
  strncpy(lpBuf,lpTemp,nLen);
  delete[]lpTemp;
}
//---------------------------------------------------------------------------
//获得指定盘符的序列号(针对USB优盘有效)
//charcDiskID:指定盘符,如'L'
//LPSTRlpPID:序列号存放的缓冲,如charszPID[32]
//byccrun(老妖)info@ccrun.com
//---------------------------------------------------------------------------
boolCrnGetUSBDiskID(charcDiskID,LPSTRlpPID)
{
  charszDrv[4];
  sprintf(szDrv,"%c:\",cDiskID);
  if(GetDriveType(szDrv)!=DRIVE_REMOVABLE)
  {
    MessageBox(0,"指定的盘不是有效的优盘.",
        "错误",MB_OK|MB_ICONWARNING);
    returnfalse;
  }
  charlpRegPath[512]={0};
  charlpRegValue[256]={0};
  sprintf(lpRegPath,"SYSTEM\MountedDevices");
  sprintf(lpRegValue,"\DosDevices\%c:",cDiskID);
  //
  DWORDdwDataSize(0);
  DWORDdwRegType(REG_BINARY);
  LPBYTElpRegBinData(NULL);
  LPSTRlpUSBKeyData(NULL);
  //查询注册表中映射驱动器的设备信息
  HKEYhKey;
  longlRet=::RegOpenKeyEx(
      HKEY_LOCAL_MACHINE,//rootkey
      lpRegPath,//要拜候的键的位置
      0,    //
      KEY_READ, //以查询的方法拜候注册表
      &hKey);  //hKEY保存此函数所翻开的键的句柄
  if(lRet!=ERROR_SUCCESS)
    returnfalse;
  else
  {
    lRet=::RegQueryValueEx(hKey,//所翻开的键的句柄
        lpRegValue,  //要查询的键值名
        NULL,
        &dwRegType,  //查询数据的范例
        lpRegBinData, //保存所查询的数据
        &dwDataSize); //预设置的数据长度
    if(lRet!=ERROR_SUCCESS)
    {
      ::RegCloseKey(hKey);
      returnfalse;
    }
    else
    {
      lpRegBinData=newBYTE[dwDataSize];
      lpUSBKeyData=newchar[dwDataSize];
      memset(lpUSBKeyData,0,dwDataSize);
      lRet=::RegQueryValueEx(hKey,
          lpRegValue,
          NULL,
          &dwRegType,
          lpRegBinData,
          &dwDataSize);
      if(lRet!=ERROR_SUCCESS)
      {
// 本文转自 C++Builder 研究 - http://www.ccrun.com/article.asp?i=1021&d=fgp83l
        delete[]lpRegBinData;
        delete[]lpUSBKeyData;
        ::RegCloseKey(hKey);
        returnfalse;
      }
    }
  }
  ::RegCloseKey(hKey);
  //过滤二进制串中的无用信息(将0x0字符去除)
  intj=0;
  for(DWORDi=0;i<dwDataSize;i++)
  {
    if(lpRegBinData[i]!=0x0)
    {
      lpUSBKeyData[j]=lpRegBinData[i];
      j++;
    }
  }
  delete[]lpRegBinData;
  //I'msorry:YoucanremoveunderlineinyourCode.
  MessageBox(0,g_strCrnVerify,"CrnGetUSBDiskID",0);
  //I'msorry:Onlyfor[ZhuanTieBuLiuMing].--#
  // 截取lpUSBKeyData中的有效信息,例:7&100a16f&0
  // ??STORAGE#RemovableMedia#7&100a16f&0&RM#{53f5630d-b6bf-11d0-94f2-00a0c91efb8b}
  // 63 63 72 75 6E 2E 63 6F 6D
  LPSTRlpPos1=strstr(lpUSBKeyData,"#RemovableMedia#")+16;
  LPSTRlpPos2=strstr(lpUSBKeyData,"RM");
  strncpy(lpUSBKeyData,lpPos1,int(lpPos2)-int(lpPos1));
  lpUSBKeyData[int(lpPos2)-int(lpPos1)-1]=0x0;
  strcpy(lpUSBKeyData,strupr(lpUSBKeyData));
  //DiskDevice(磁盘设备)的GUID
  GUIDguidUSB;
  CLSIDFromString(L"{53f56307-b6bf-11d0-94f2-00a0c91efb8b}",&guidUSB);
  //
  HDEVINFOhUSB=SetupDiGetClassDevs(
      &guidUSB,NULL,0,DIGCF_PRESENT|DIGCF_DEVICEINTERFACE);
  if(hUSB==INVALID_HANDLE_VALUE)
  {
    delete[]lpUSBKeyData;
    returnfalse;
  }
  //
  intnDevIndex=0;
  boolbSuccess;
  SP_DEVINFO_DATADevData;
  SP_DEVICE_INTERFACE_DATADevIntData;
  PSP_DEVICE_INTERFACE_DETAIL_DATAlpDevIntDetailData;
  DWORDdwBytesReturned;
  //遍历磁盘设备
  do
  {
    DevIntData.cbSize=sizeof(SP_DEVICE_INTERFACE_DATA);
    bSuccess=SetupDiEnumDeviceInterfaces(hUSB,NULL,&guidUSB,
        nDevIndex,&DevIntData)>0;
    if(bSuccess)
    {
      //获得接口具体信息
      DevData.cbSize=sizeof(SP_DEVINFO_DATA);
      dwBytesReturned=0;
      SetupDiGetDeviceInterfaceDetailA(hUSB,&DevIntData,
          NULL,0,&dwBytesReturned,&DevData);
      if(dwBytesReturned!=0&&GetLastError()==ERROR_INSUFFICIENT_BUFFER)
      {
        lpDevIntDetailData=(PSP_DEVICE_INTERFACE_DETAIL_DATA)
            GlobalAlloc(GMEM_FIXED,dwBytesReturned);
        lpDevIntDetailData->cbSize=
            sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
        if(SetupDiGetDeviceInterfaceDetailA(hUSB,&DevIntData,
            lpDevIntDetailData,dwBytesReturned,&dwBytesReturned,&DevData))
        {
          //获得设备接口具体信息并按照转化后的途径在注册表中查询
          LPSTRlpPathTemp=newchar[strlen(lpDevIntDetailData->DevicePath)+256];
          strcpy(lpRegPath,"SYSTEM\CurrentControlSet\Enum\");
          strcpy(lpPathTemp,lpDevIntDetailData->DevicePath);
          lpPos1=LPSTR(int(lpPathTemp)+4);
          lpPos2=LPSTR(int(strstr(lpPathTemp,"{"))-1);
          strncpy(lpPathTemp,lpPos1,int(lpPos2)-int(lpPos1));
          lpPathTemp[int(lpPos2)-int(lpPos1)]=0x0;
          CrnReplaceString(lpPathTemp,'#','\');
          strcat(lpRegPath,lpPathTemp);
          //
          if(RegOpenKeyEx(
              HKEY_LOCAL_MACHINE,
              lpRegPath,
              0,
              KEY_READ,
              &hKey)==ERROR_SUCCESS)
          {
            dwRegType=REG_SZ;
            LPSTRlpRegSzData=NULL;
            dwDataSize=0;
            lRet=::RegQueryValueEx(hKey,
                "ParentIdPrefix",
                NULL,
                &dwRegType,
                (LPBYTE)lpRegSzData,
                &dwDataSize);
            if(lRet==ERROR_SUCCESS)
            {
              lpRegSzData=newchar[dwDataSize];
              lRet=::RegQueryValueEx(hKey,
                  "ParentIdPrefix",
                  NULL,
                  &dwRegType,
                  (LPBYTE)lpRegSzData,
                  &dwDataSize);
              if(lRet==ERROR_SUCCESS)
              {
                strcpy(lpRegSzData,strupr(lpRegSzData));
                if(!strcmp(lpUSBKeyData,lpRegSzData))
                {
                  //经比对,找到要查询的磁盘设备
                  strcpy(lpPathTemp,LPSTR(int(strstr(
                    lpDevIntDetailData->DevicePath,"#"))+1));
                  lpPos1=LPSTR(int(strstr(lpPathTemp,"#"))+1);
                  lpPos2=LPSTR(int(strstr(lpPathTemp,"#{")));
                  strncpy(lpPathTemp,lpPos1,int(lpPos2)-int(lpPos1));
                  lpPathTemp[int(lpPos2)-int(lpPos1)]=0x0;
                  //获得终究的磁盘序列号
                  CrnReplaceString(lpPathTemp,'&',0x00);
                  if(lpPID)
                    strncpy(lpPID,strupr(lpPathTemp),32);
                  //
                  delete[]lpRegSzData;
                  delete[]lpPathTemp;
                  GlobalFree(lpDevIntDetailData);
                  ::RegCloseKey(hKey);
                  break;
                }
              }
              delete[]lpRegSzData;
            }
            ::RegCloseKey(hKey);
          }
          delete[]lpPathTemp;
          nDevIndex++;
        }
        GlobalFree(lpDevIntDetailData);
      }
    }
  }while(bSuccess);
  SetupDiDestroyDeviceInfoList(hUSB);
  delete[]lpUSBKeyData;
  returntrue;
}
//---------------------------------------------------------------------------
void__fastcallTForm1::Button1Click(TObject*Sender)
{
  charlpPID[32];
  if(CrnGetUSBDiskID('L',lpPID))
    ShowMessage(lpPID);
}

  以上是“用SetupAPI结合注册表获得USB优盘序列号[VC/C++编程]”的内容,如果你对以上该文章内容感兴趣,你可以看看安全黑客为您推荐以下文章:
  • J2ME游戏开辟学习之用setClip分割图片
  • 用SetupAPI结合注册表获得USB优盘序列号
  • <b>Flash高级本领之利用setTimeout</b>
  • 本文地址: 与您的QQ/BBS好友分享!

    文章评论评论内容只代表网友观点,与本站立场无关!

       评论摘要(共 0 条,得分 0 分,平均 0 分) 查看完整评论
    免责条款 - 广告合作 - 下载声明 - 欢迎投稿 - 友情连接 - 网站地图 -
    Copyright © 2012-2013 www.110hack.com. All Rights Reserved .