⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 installpassthru.cpp

📁 NDIS IMD中间层驱动自动安装程序
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// InstallPassthru.cpp: implementation of the CInstallPassthru class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "Pegasus.h"
#include "InstallPassthru.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CInstallPassthru::CInstallPassthru():m_hwnd(NULL),m_IsSetup(false)
{

}

CInstallPassthru::CInstallPassthru(HWND hWnd)
{
	m_hwnd=hWnd;
}

CInstallPassthru::~CInstallPassthru()
{

}

void CInstallPassthru::SethWnd(HWND hWnd)
{
	m_hwnd=hWnd;	
}

void CInstallPassthru::InstallPassthru()
{
	DWORD  nResult;
	nResult = MessageBox(m_hwnd,_TEXT("你要安装 Passthru 网络驱动?" ),_TEXT ("Passthru网络过滤驱动"), MB_OKCANCEL | MB_ICONINFORMATION );
	if( nResult != IDOK )
		return;

	TCHAR szFileFullPath[ _MAX_PATH ]={0};
	TCHAR szDir[_MAX_DIR]={0};
	TCHAR szDrive[_MAX_DRIVE]={0};
	TCHAR szInfPath[_MAX_PATH]={0};
	TCHAR szInfPath2[_MAX_PATH]={0};
	::GetModuleFileName(NULL,szFileFullPath, MAX_PATH);
	//得到当前路径下netsf.inf
	_tsplitpath(szFileFullPath,szDrive,        
		szDir,
		m_processName,
		NULL);	
//
	StringCchCopy(szInfPath,sizeof(szInfPath),szDrive); 
	StringCchCat(szInfPath,sizeof(szInfPath),szDir);
	StringCchCopy(szInfPath2,sizeof(szInfPath2),szInfPath);
	StringCchCat(szInfPath,sizeof(szInfPath),TEXT("netsf_m.inf"));
	if ( !SetupCopyOEMInf(
		szInfPath,
		szInfPath2, // Other files are in the
		// same dir. as primary INF
		SPOST_PATH,     // First param is path to INF
		0,              // Default copy style
		NULL,           // Name of the INF after
		// it's copied to %windir%\inf
		0,              // Max buf. size for the above
		NULL,           // Required size if non-null
		NULL)           // Optionally get the filename
		// part of Inf name after it is copied.
		)
	{
		ShowError(TEXT("复制 PasstruMP 的inf安装文件到系统目录失败"));
		return;
	}
//
	
	
	StringCchCopy(szInfPath,sizeof(szInfPath),szDrive); 
	StringCchCat(szInfPath,sizeof(szInfPath),szDir);
	StringCchCat(szInfPath,sizeof(szInfPath),TEXT("netsf.inf"));
	LPTSTR  lpszPnpID=NULL;
	HRESULT hr = GetPnpID(szInfPath, &lpszPnpID);
	LPTSTR     lpszApp=NULL;
	if ( hr == S_OK ){
		INetCfg    *pnc;
		HRESULT    hr;
		hr = HrGetINetCfg(TRUE,m_processName,&pnc,&lpszApp);
		if ( hr == S_OK ){
			// Install the network component.
			hr = HrInstallNetComponent(pnc,
				lpszPnpID,
				&GUID_DEVCLASS_NETSERVICE,
				szInfPath);
			if ( (hr == S_OK) || (hr == NETCFG_S_REBOOT) )	
				hr = pnc->Apply();

			HrReleaseINetCfg(pnc,TRUE );
		}
		else if ( (hr == NETCFG_E_NO_WRITE_LOCK) && lpszApp )
			{
				ShowError(_TEXT("currently holds the lock, try later."));
				CoTaskMemFree(lpszApp);
			}
		else
			ShowError(_TEXT("Couldn't the get notify object interface."));

	}
	
	if (lpszPnpID)	
		CoTaskMemFree( lpszPnpID);
	if (lpszApp)
		CoTaskMemFree(lpszApp);

	if ( hr!= S_OK )
	{
		m_IsSetup=false;
		MessageBox(m_hwnd,_TEXT("安装失败"),_TEXT("- -"),0);
	}
	
	return ;
	
}

void CInstallPassthru::UninstallPassthru()
{
	int nResult = MessageBox(m_hwnd, TEXT("你要卸载Passthru网络驱动?" ),TEXT("Passthru网络过滤驱动"), MB_OKCANCEL | MB_ICONINFORMATION );
	if( nResult != IDOK )
		return ;
	
	INetCfg               *pnc;
	INetCfgComponent      *pncc;
	INetCfgClass          *pncClass;
	INetCfgClassSetup     *pncClassSetup;
	LPTSTR                lpszApp;
	GUID                  guidClass;
	OBO_TOKEN             obo;
	HRESULT               hr;
	
	hr = HrGetINetCfg( TRUE,m_processName, &pnc,&lpszApp);
	
	if ( hr == S_OK )
	{
		// Get a reference to the network component to uninstall.
		hr = pnc->FindComponent(_TEXT("ms_passthru"), &pncc );		
		if ( hr == S_OK )
		{
			// Get the class GUID.
			hr = pncc->GetClassGuid( &guidClass );	
			if ( hr == S_OK )
			{
				// Get a reference to component's class.
				hr = pnc->QueryNetCfgClass( &guidClass,
					IID_INetCfgClass,
					(PVOID *)&pncClass );
				if ( hr == S_OK )
				{
					// Get the setup interface.
					
					hr = pncClass->QueryInterface( IID_INetCfgClassSetup,
						(LPVOID *)&pncClassSetup );
					
					if ( hr == S_OK )
					{
						// Uninstall the component.
						ZeroMemory( &obo,sizeof(OBO_TOKEN) );
						obo.Type = OBO_USER;
						
						hr = pncClassSetup->DeInstall( pncc,
							&obo,NULL);
						if ( (hr == S_OK) || (hr == NETCFG_S_REBOOT) )
						{
							hr = pnc->Apply();
							
							if ( (hr != S_OK) && (hr != NETCFG_S_REBOOT) )
								ShowError(_TEXT ("卸载驱动之后无法成功应用!"));
							else
							{
								m_IsSetup=false;
								MessageBox(m_hwnd,_TEXT("成功卸载驱动!"),_TEXT("^@^"),0);
							}
						}
						else
							ShowError(_TEXT("卸载网络组件失败!"));
						
						ReleaseRef( pncClassSetup );
					}
					else
						ShowError(_TEXT("无法得到安装类接口!"));
					
					ReleaseRef( pncClass );
				}
				else
					ShowError(_TEXT("无法得到安装类接口!"));
				
			}
			else
				ShowError(_TEXT("无法得到安装类接口的 GUID !"));
			ReleaseRef( pncc );
		}
		else
			ShowError(_TEXT("无法得到一个接口指针!"));
		
		HrReleaseINetCfg( pnc,TRUE );
	}
	else
	{
		if ((hr == NETCFG_E_NO_WRITE_LOCK) && lpszApp )
		{
			ShowError(_TEXT("碰到死锁问题,稍后再试!"));
			CoTaskMemFree( lpszApp );
		}
		else
			ShowError(_TEXT("无法得到通知对象接口!"));
	}
	
	return ;

}

HRESULT CInstallPassthru::HrGetINetCfg (IN BOOL fGetWriteLock,
                   IN LPTSTR lpszAppName,
                      OUT INetCfg** ppnc,
                      LPTSTR *lpszLockedBy)
{
    INetCfg      *pnc = NULL;
    INetCfgLock  *pncLock = NULL;
    HRESULT      hr = S_OK;
    *ppnc = NULL;

    if ( lpszLockedBy )
    {
        *lpszLockedBy = NULL;
    }

    hr = CoInitialize( NULL );

    if ( hr == S_OK ) {
        hr = CoCreateInstance( CLSID_CNetCfg,
                               NULL, CLSCTX_INPROC_SERVER,
                               IID_INetCfg,
                               (void**)&pnc );
        if ( hr == S_OK ) {

            if ( fGetWriteLock ) {

                //
                // Get the locking reference
                //

                hr = pnc->QueryInterface( IID_INetCfgLock,
                                          (LPVOID *)&pncLock );
                if ( hr == S_OK ) {

                    //
                    // Attempt to lock the INetCfg for read/write
                    //

                    hr = pncLock->AcquireWriteLock( 10,
                                                    lpszAppName,
                                                    lpszLockedBy);
                    if (hr == S_FALSE ) {
                        hr = NETCFG_E_NO_WRITE_LOCK;
                    }
                }
            }

            if ( hr == S_OK ) {

                //
                // Initialize the INetCfg object.
                //

                hr = pnc->Initialize( NULL );

                if ( hr == S_OK ) {
                    *ppnc = pnc;
                    pnc->AddRef();
                }
                else {

                    //
                    // Initialize failed, if obtained lock, release it
                    //

                    if ( pncLock ) {
                        pncLock->ReleaseWriteLock();
                    }
                }
            }

            ReleaseRef( pncLock );
            ReleaseRef( pnc );
        }

        //
        // In case of error, uninitialize COM.
        //

        if ( hr != S_OK ) {
            CoUninitialize();
        }
    }

    return hr;
}

HRESULT CInstallPassthru::HrReleaseINetCfg (IN INetCfg* pnc,
                          IN BOOL fHasWriteLock)
{
    INetCfgLock    *pncLock = NULL;
    HRESULT        hr = S_OK;

    //
    // Uninitialize INetCfg
    //

    hr = pnc->Uninitialize();

    //
    // If write lock is present, unlock it
    //

    if ( hr == S_OK && fHasWriteLock ) {

        //
        // Get the locking reference
        //

        hr = pnc->QueryInterface( IID_INetCfgLock,
                                  (LPVOID *)&pncLock);
        if ( hr == S_OK ) {
           hr = pncLock->ReleaseWriteLock();
           ReleaseRef( pncLock );
        }
    }

    ReleaseRef( pnc );

    //
    // Uninitialize COM.
    //

    CoUninitialize();

    return hr;
}


HRESULT CInstallPassthru::HrInstallNetComponent (IN INetCfg *pnc,
                               IN LPTSTR lpszComponentId,
                               IN const GUID *pguidClass,
                               IN LPTSTR lpszInfFullPath)
{
    DWORD     dwError;
    HRESULT   hr = S_OK;
    TCHAR     Drive[_MAX_DRIVE];
    TCHAR     Dir[_MAX_DIR];
    TCHAR     DestDir[_MAX_DRIVE+_MAX_DIR];
	// Get the path where the INF file is.
	_tsplitpath(lpszInfFullPath,Drive,        
		Dir,
		NULL,
		NULL);
	
	StringCchCopy(DestDir,sizeof(DestDir),Drive); 
	StringCchCat(DestDir,sizeof(DestDir),Dir);
	AfxMessageBox(lpszInfFullPath);
	AfxMessageBox(DestDir);
	if (!SetupCopyOEMInf( 
		(PWSTR)lpszInfFullPath, 
		(PWSTR)DestDir, // Other files are in the 
		SPOST_PATH,    // First param is path to INF 
		SP_COPY_REPLACEONLY,// Default copy style 
		NULL,          // Name of the INF after 
		// it's copied to %windir%\inf 
		0,              // Max buf. size for the above 
		NULL,          // Required size if non-null 
		NULL)          // Optionally get the filename 
		// part of Inf name after it is copied. 
		) 
	{
		dwError = GetLastError();
		hr = HRESULT_FROM_WIN32( dwError );	
		ShowError(_TEXT("复制inf错误1"));
		
	}

    if ( S_OK == hr ) {

        // Install the network component.
        hr = HrInstallComponent( pnc,
                                 lpszComponentId,
                                 pguidClass );
        if ( hr == S_OK ) {
            // On success, apply the changes
            hr = pnc->Apply();
			m_IsSetup=true;
			MessageBox(m_hwnd,_TEXT("安装成功"),_TEXT("^@^"),0);
			
        }
		else
			ShowError(_TEXT("HrInstallComponent error"));
    }

    return hr;
}


HRESULT CInstallPassthru::HrInstallComponent(IN INetCfg* pnc,
                           IN LPTSTR szComponentId,
                           IN const GUID* pguidClass)
{
    INetCfgClassSetup   *pncClassSetup = NULL;
    INetCfgComponent    *pncc = NULL;
    OBO_TOKEN           OboToken;
    HRESULT             hr = S_OK;

    //
    // OBO_TOKEN specifies on whose behalf this
    // component is being installed.
    // Set it to OBO_USER so that szComponentId will be installed
    // on behalf of the user.
    //

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -