dzyssssss
驱动牛犊
驱动牛犊
  • 注册日期2006-07-10
  • 最后登录2010-04-08
  • 粉丝0
  • 关注0
  • 积分12分
  • 威望128点
  • 贡献值0点
  • 好评度74点
  • 原创分0分
  • 专家分0分
阅读:2207回复:5

软件实现U盘的插拔整个动作

楼主#
更多 发布于:2007-01-16 16:06
  在用一款软件时,可以对U盘进行重置。效果就像先拔除U盘后再插上。不知道这个是怎么实现的,请问各位大牛们。如果单独的删除USB设备,这个是可以的,但是怎么让程序实现自动再插上U盘呢?
dzyssssss
驱动牛犊
驱动牛犊
  • 注册日期2006-07-10
  • 最后登录2010-04-08
  • 粉丝0
  • 关注0
  • 积分12分
  • 威望128点
  • 贡献值0点
  • 好评度74点
  • 原创分0分
  • 专家分0分
沙发#
发布于:2007-01-17 10:00
怎么没有人回答阿?
sosojohn
论坛版主
论坛版主
  • 注册日期2006-01-29
  • 最后登录2021-06-25
  • 粉丝0
  • 关注1
  • 积分1047分
  • 威望535点
  • 贡献值1点
  • 好评度178点
  • 原创分0分
  • 专家分0分
  • 社区居民
板凳#
发布于:2007-01-17 10:31
见 武安河 书
rayyang2000
管理员
管理员
  • 注册日期2001-03-23
  • 最后登录2012-09-13
  • 粉丝3
  • 关注0
  • 积分1036分
  • 威望925点
  • 贡献值3点
  • 好评度823点
  • 原创分0分
  • 专家分0分
地板#
发布于:2007-01-18 07:24
ddk\src\setup\devcon
search for 'remove', 'install', 'disable', 'enable'
天天coding-debugging中----超稀饭memory dump file ======================================================== [b]Windows Device Driver Development and Consulting Service[/b] [color=blue][url]http://www.ybwork.com[/url][/color] ========================================================
pilixuanke
驱动中牛
驱动中牛
  • 注册日期2005-10-31
  • 最后登录2016-01-09
  • 粉丝0
  • 关注0
  • 积分1018分
  • 威望626点
  • 贡献值0点
  • 好评度512点
  • 原创分0分
  • 专家分0分
地下室#
发布于:2007-01-18 12:12
引用第3楼rayyang20002007-01-18 07:24发表的“”:
ddksrcsetupdevcon
search for 'remove', 'install', 'disable', 'enable'


SetupDixxx
向底层开发进军!!!
yaolixing
驱动小牛
驱动小牛
  • 注册日期2006-06-27
  • 最后登录2010-07-15
  • 粉丝1
  • 关注0
  • 积分991分
  • 威望135点
  • 贡献值0点
  • 好评度124点
  • 原创分0分
  • 专家分0分
5楼#
发布于:2007-06-26 16:42
#include "StdAfx.h"
#include "SetupDIMgr.h"
#include "Setupapi.h"
#include "afxwin.h"
#pragma comment(lib,"Setupapi.lib")


HDEVINFO        m_devInfo;
SP_DEVINFO_DATA m_devInfoData;
int                m_nDevIndex;

//define the GUID for your device class
GUID hidGuid = {0x4D36E96D, 0xE325, 0x11CE,
{0xBF,0xC1, 0x08, 0x00, 0x2B, 0xE1, 0x03, 0x18}};

BOOL
GetRegProperty(CString &sProperty, DWORD dwWhich)
{
    DWORD regDataType = 0;
    LPTSTR deviceName =
    (LPTSTR) GetDeviceRegistryProperty(
                                        m_devInfo,
                                        &m_devInfoData,
                                        dwWhich,
                                        &regDataType );

    if(NULL != deviceName)
    {
        // just to make sure we are getting the expected type of buffer
        if(REG_SZ != regDataType)
        {
            if(REG_MULTI_SZ == regDataType)
            {
                LPTSTR sScanner = deviceName;
                sProperty.Empty();
                while(0 != _tcslen(sScanner))
                {
                    sProperty += sScanner;
                    sScanner += _tcslen(sScanner) + sizeof(TCHAR);
                    if(0 != _tcslen(sScanner))
                    {
                        sProperty += ",";
                    }
                }
                free(deviceName);
                return TRUE;
            }

            TRACE("registry key is not an SZ or MULTI_SZ!\n");
            free(deviceName);
            return FALSE;
        }

        // if the device name starts with \Device, cut that off (all
        // devices will start with it, so it is redundant)
        if(_tcsncmp(deviceName, _T("\\Device"), 7) == 0)
        {
            memmove(deviceName, deviceName+7, (_tcslen(deviceName)-6)*sizeof(_TCHAR));
        }

        sProperty = deviceName;
        free(deviceName);
        return TRUE;
    }
    return FALSE;
}

BOOL Enum(void)
{
    if(INVALID_HANDLE_VALUE == m_devInfo)
    {
        return FALSE;
    }

    // as per DDK docs on SetupDiEnumDeviceInfo
    m_devInfoData.cbSize = sizeof(m_devInfoData);

    // step through the list of devices for this handle
    // get device info at index deviceIndex, the function returns FALSE
    // when there is no device at the given index.
    if(!SetupDiEnumDeviceInfo(m_devInfo, m_nDevIndex, &m_devInfoData))
    {
        //TRACE("Done enumerating...\n");
        return FALSE;
    }

    m_nDevIndex++;
    return TRUE;
}


BOOL
RestartDevice(void)
{
    CString sHardwareID;
    // get a list of devices which support the given interface
    TCHAR sEnumerator[] = _T("USB");
    m_devInfo = SetupDiGetClassDevs(NULL,
                                   sEnumerator,
                                   0,
                                   DIGCF_ALLCLASSES | (bPresentOnly ? DIGCF_PRESENT : 0));

    if(m_devInfo == INVALID_HANDLE_VALUE )
    {
        TRACE("got INVALID_HANDLE_VALUE!\n");
    }
    m_nDevIndex = 0;
    
    //get m_devInfoData,updata m_nDevIndex
    Enum();
        
    if( GetRegProperty(sHardwareID, SPDRP_HARDWAREID))
    {
        do
        {
            SP_PROPCHANGE_PARAMS params;
//            SP_DEVINSTALL_PARAMS installParams;
            
            // for future compatibility; this will zero out the entire struct, rather
            // than just the fields which exist now
            memset(&params, 0, sizeof(SP_PROPCHANGE_PARAMS));
            
            // initialize the SP_CLASSINSTALL_HEADER struct at the beginning of the
            // SP_PROPCHANGE_PARAMS struct, so that SetupDiSetClassInstallParams will
            // work
            params.ClassInstallHeader.cbSize = sizeof(SP_CLASSINSTALL_HEADER);
            params.ClassInstallHeader.InstallFunction = DIF_PROPERTYCHANGE;
            
            // initialize SP_PROPCHANGE_PARAMS such that the device will be stopped.
            params.StateChange = DICS_PROPCHANGE;
            params.Scope       = DICS_FLAG_CONFIGSPECIFIC;
            params.HwProfile   = 0; // current profile
            
            // prepare for the call to SetupDiCallClassInstaller (to stop the device)
            if(!SetupDiSetClassInstallParams(m_devInfo,
                                            &m_devInfoData,
                                            (PSP_CLASSINSTALL_HEADER) &params,
                                            sizeof(SP_PROPCHANGE_PARAMS)) )
            {
                TRACE("in RestartDevice(): couldn't set the install parameters!");
                TRACE(" error: %u\n", GetLastError());
                return FALSE;
            }
            
            // stop the device
            if(!SetupDiCallClassInstaller(DIF_PROPERTYCHANGE,
                                          m_devInfo,
                                          &m_devInfoData))
            {
                TRACE("in RestartDevice(): call to class installer (STOP) failed!");
                TRACE(" error: %u\n", GetLastError() );
                return FALSE;
            }

        }

        return TRUE;
    }
    return FALSE;
}
游客

返回顶部