// @pymethod |PyIBackgroundCopyJob|AddFile|Description of AddFile.
PyObject *PyIBackgroundCopyJob::AddFile(PyObject *self, PyObject *args)
{
	IBackgroundCopyJob *pIBCJ = GetI(self);
	if ( pIBCJ == NULL )
		return NULL;
	// @pyparm <o unicode>|RemoteUrl||Description for RemoteUrl
	// @pyparm <o unicode>|LocalName||Description for LocalName
	PyObject *obRemoteUrl;
	PyObject *obLocalName;
	LPWSTR RemoteUrl;
	LPWSTR LocalName;
	if ( !PyArg_ParseTuple(args, "OO:AddFile", &obRemoteUrl, &obLocalName) )
		return NULL;
	BOOL bPythonIsHappy = TRUE;
	if (bPythonIsHappy && !PyWinObject_AsBstr(obRemoteUrl, &RemoteUrl)) bPythonIsHappy = FALSE;
	if (bPythonIsHappy && !PyWinObject_AsBstr(obLocalName, &LocalName)) bPythonIsHappy = FALSE;
	if (!bPythonIsHappy) return NULL;
	HRESULT hr;
	PY_INTERFACE_PRECALL;
	hr = pIBCJ->AddFile( RemoteUrl, LocalName );
	SysFreeString(RemoteUrl);
	SysFreeString(LocalName);
	PY_INTERFACE_POSTCALL;
	if ( FAILED(hr) )
		return PyCom_BuildPyException(hr, pIBCJ, IID_IBackgroundCopyJob );
	Py_INCREF(Py_None);
	return Py_None;

}
// @pymethod |PyIBackgroundCopyJob|SetProxySettings|Description of SetProxySettings.
PyObject *PyIBackgroundCopyJob::SetProxySettings(PyObject *self, PyObject *args)
{
	IBackgroundCopyJob *pIBCJ = GetI(self);
	if ( pIBCJ == NULL )
		return NULL;
	BG_JOB_PROXY_USAGE ProxyUsage;
	// @pyparm int|ProxyUsage||Description for ProxyUsage
	WCHAR * ProxyList;
	PyObject *obProxyList;
	// @pyparm unicode|ProxyList||Description for ProxyList
	WCHAR * ProxyBypassList;
	PyObject *obProxyBypassList;
	// @pyparm unicode|ProxyBypassList||Description for ProxyBypassList
	if ( !PyArg_ParseTuple(args, "lOO:SetProxySettings", &ProxyUsage, &obProxyList, &obProxyBypassList) )
		return NULL;
	BOOL bPythonIsHappy = TRUE;
	if (bPythonIsHappy && !PyWinObject_AsWCHAR( obProxyList, &ProxyList, TRUE )) bPythonIsHappy = FALSE;
	if (bPythonIsHappy && !PyWinObject_AsWCHAR( obProxyBypassList, &ProxyBypassList, TRUE )) bPythonIsHappy = FALSE;
	if (!bPythonIsHappy) return NULL;
	HRESULT hr;
	PY_INTERFACE_PRECALL;
	hr = pIBCJ->SetProxySettings( ProxyUsage, ProxyList, ProxyBypassList );
	PyWinObject_FreeWCHAR(ProxyList);
	PyWinObject_FreeWCHAR(ProxyBypassList);
	PY_INTERFACE_POSTCALL;
	if ( FAILED(hr) )
		return PyCom_BuildPyException(hr, pIBCJ, IID_IBackgroundCopyJob );
	Py_INCREF(Py_None);
	return Py_None;
}
// @pymethod |PyIBackgroundCopyJob|GetId|Description of GetId.
PyObject *PyIBackgroundCopyJob::GetId(PyObject *self, PyObject *args)
{
	IBackgroundCopyJob *pIBCJ = GetI(self);
	if ( pIBCJ == NULL )
		return NULL;
	GUID val;
	HRESULT hr;
	PY_INTERFACE_PRECALL;
	hr = pIBCJ->GetId( &val );
	PY_INTERFACE_POSTCALL;
	if ( FAILED(hr) )
		return PyCom_BuildPyException(hr, pIBCJ, IID_IBackgroundCopyJob );
	return PyWinObject_FromIID(val);
}
// @pymethod |PyIBackgroundCopyJob|Cancel|Description of Cancel.
PyObject *PyIBackgroundCopyJob::Cancel(PyObject *self, PyObject *args)
{
	IBackgroundCopyJob *pIBCJ = GetI(self);
	if ( pIBCJ == NULL )
		return NULL;
	if ( !PyArg_ParseTuple(args, ":Cancel") )
		return NULL;
	HRESULT hr;
	PY_INTERFACE_PRECALL;
	hr = pIBCJ->Cancel( );
	PY_INTERFACE_POSTCALL;
	if ( FAILED(hr) )
		return PyCom_BuildPyException(hr, pIBCJ, IID_IBackgroundCopyJob );
	return PyInt_FromLong(hr);
}
// @pymethod |PyIBackgroundCopyJob|EnumFiles|Description of EnumFiles.
PyObject *PyIBackgroundCopyJob::EnumFiles(PyObject *self, PyObject *args)
{
	IBackgroundCopyJob *pIBCJ = GetI(self);
	if ( pIBCJ == NULL )
		return NULL;
	IEnumBackgroundCopyFiles *pEnum;
	if ( !PyArg_ParseTuple(args, ":EnumFiles") )
		return NULL;
	HRESULT hr;
	PY_INTERFACE_PRECALL;
	hr = pIBCJ->EnumFiles( &pEnum );
	PY_INTERFACE_POSTCALL;
	if ( FAILED(hr) )
		return PyCom_BuildPyException(hr, pIBCJ, IID_IBackgroundCopyJob );
	return PyCom_PyObjectFromIUnknown(pEnum, IID_IEnumBackgroundCopyFiles, FALSE);
}
// @pymethod |PyIBackgroundCopyJob|GetNotifyInterface|Description of GetNotifyInterface.
PyObject *PyIBackgroundCopyJob::GetNotifyInterface(PyObject *self, PyObject *args)
{
	IBackgroundCopyJob *pIBCJ = GetI(self);
	if ( pIBCJ == NULL )
		return NULL;
	IUnknown * pVal;
	if ( !PyArg_ParseTuple(args, ":GetNotifyInterface") )
		return NULL;
	HRESULT hr;
	PY_INTERFACE_PRECALL;
	hr = pIBCJ->GetNotifyInterface( &pVal );
	PY_INTERFACE_POSTCALL;
	if ( FAILED(hr) )
		return PyCom_BuildPyException(hr, pIBCJ, IID_IBackgroundCopyJob );
	return PyCom_PyObjectFromIUnknown(pVal, IID_IUnknown, FALSE);
}
// @pymethod |PyIBackgroundCopyJob|GetPriority|Description of GetPriority.
PyObject *PyIBackgroundCopyJob::GetPriority(PyObject *self, PyObject *args)
{
	IBackgroundCopyJob *pIBCJ = GetI(self);
	if ( pIBCJ == NULL )
		return NULL;
	if ( !PyArg_ParseTuple(args, ":GetPriority") )
		return NULL;
	BG_JOB_PRIORITY val;
	HRESULT hr;
	PY_INTERFACE_PRECALL;
	hr = pIBCJ->GetPriority(&val );
	PY_INTERFACE_POSTCALL;
	if ( FAILED(hr) )
		return PyCom_BuildPyException(hr, pIBCJ, IID_IBackgroundCopyJob );
	return PyInt_FromLong(val);
}
// @pymethod |PyIBackgroundCopyJob|GetErrorCount|Description of GetErrorCount.
PyObject *PyIBackgroundCopyJob::GetErrorCount(PyObject *self, PyObject *args)
{
	IBackgroundCopyJob *pIBCJ = GetI(self);
	if ( pIBCJ == NULL )
		return NULL;
	if ( !PyArg_ParseTuple(args, ":GetErrorCount") )
		return NULL;
	ULONG errors;
	HRESULT hr;
	PY_INTERFACE_PRECALL;
	hr = pIBCJ->GetErrorCount( &errors );
	PY_INTERFACE_POSTCALL;
	if ( FAILED(hr) )
		return PyCom_BuildPyException(hr, pIBCJ, IID_IBackgroundCopyJob );
	return PyLong_FromUnsignedLong(errors);
}
// @pymethod |PyIBackgroundCopyJob|GetMinimumRetryDelay|Description of GetMinimumRetryDelay.
PyObject *PyIBackgroundCopyJob::GetMinimumRetryDelay(PyObject *self, PyObject *args)
{
	IBackgroundCopyJob *pIBCJ = GetI(self);
	if ( pIBCJ == NULL )
		return NULL;
	if ( !PyArg_ParseTuple(args, ":GetMinimumRetryDelay") )
		return NULL;
	HRESULT hr;
	ULONG seconds;
	PY_INTERFACE_PRECALL;
	hr = pIBCJ->GetMinimumRetryDelay( &seconds );
	PY_INTERFACE_POSTCALL;
	if ( FAILED(hr) )
		return PyCom_BuildPyException(hr, pIBCJ, IID_IBackgroundCopyJob );
	return PyLong_FromUnsignedLong(seconds);
}
// @pymethod |PyIBackgroundCopyJob|GetError|Description of GetError.
PyObject *PyIBackgroundCopyJob::GetError(PyObject *self, PyObject *args)
{
	IBackgroundCopyJob *pIBCJ = GetI(self);
	if ( pIBCJ == NULL )
		return NULL;
	IBackgroundCopyError * ppError;
	if ( !PyArg_ParseTuple(args, ":GetError") )
		return NULL;
	HRESULT hr;
	PY_INTERFACE_PRECALL;
	hr = pIBCJ->GetError( &ppError );
	PY_INTERFACE_POSTCALL;

	if ( FAILED(hr) )
		return PyCom_BuildPyException(hr, pIBCJ, IID_IBackgroundCopyJob );
	return PyCom_PyObjectFromIUnknown(ppError, IID_IBackgroundCopyError, FALSE);
}
// @pymethod |PyIBackgroundCopyJob|GetDisplayName|Description of GetDisplayName.
PyObject *PyIBackgroundCopyJob::GetDisplayName(PyObject *self, PyObject *args)
{
	IBackgroundCopyJob *pIBCJ = GetI(self);
	if ( pIBCJ == NULL )
		return NULL;
	LPWSTR pVal;
	if ( !PyArg_ParseTuple(args, ":GetDisplayName") )
		return NULL;
	HRESULT hr;
	PY_INTERFACE_PRECALL;
	hr = pIBCJ->GetDisplayName( &pVal );
	PY_INTERFACE_POSTCALL;
	if ( FAILED(hr) )
		return PyCom_BuildPyException(hr, pIBCJ, IID_IBackgroundCopyJob );
	PyObject *ret = PyWinObject_FromWCHAR(pVal);
	CoTaskMemFree(pVal);
	return ret;
}
// @pymethod |PyIBackgroundCopyJob|SetPriority|Description of SetPriority.
PyObject *PyIBackgroundCopyJob::SetPriority(PyObject *self, PyObject *args)
{
	IBackgroundCopyJob *pIBCJ = GetI(self);
	if ( pIBCJ == NULL )
		return NULL;
	BG_JOB_PRIORITY val;
	// @pyparm int|Val||Description for Val
	if ( !PyArg_ParseTuple(args, "l:SetPriority", &val) )
		return NULL;
	HRESULT hr;
	PY_INTERFACE_PRECALL;
	hr = pIBCJ->SetPriority(val);
	PY_INTERFACE_POSTCALL;
	if ( FAILED(hr) )
		return PyCom_BuildPyException(hr, pIBCJ, IID_IBackgroundCopyJob );
	Py_INCREF(Py_None);
	return Py_None;
}
// @pymethod |PyIBackgroundCopyJob|SetNoProgressTimeout|Description of SetNoProgressTimeout.
PyObject *PyIBackgroundCopyJob::SetNoProgressTimeout(PyObject *self, PyObject *args)
{
	IBackgroundCopyJob *pIBCJ = GetI(self);
	if ( pIBCJ == NULL )
		return NULL;
	// @pyparm int|Seconds||Description for Seconds
	ULONG Seconds;
	if ( !PyArg_ParseTuple(args, "l:SetNoProgressTimeout", &Seconds) )
		return NULL;
	HRESULT hr;
	PY_INTERFACE_PRECALL;
	hr = pIBCJ->SetNoProgressTimeout( Seconds );
	PY_INTERFACE_POSTCALL;
	if ( FAILED(hr) )
		return PyCom_BuildPyException(hr, pIBCJ, IID_IBackgroundCopyJob );
	Py_INCREF(Py_None);
	return Py_None;

}
// @pymethod |PyIBackgroundCopyJob|GetProxySettings|Description of GetProxySettings.
PyObject *PyIBackgroundCopyJob::GetProxySettings(PyObject *self, PyObject *args)
{
	IBackgroundCopyJob *pIBCJ = GetI(self);
	if ( pIBCJ == NULL )
		return NULL;
	if ( !PyArg_ParseTuple(args, ":GetProxySettings") )
		return NULL;
	BG_JOB_PROXY_USAGE ProxyUsage;
	WCHAR *proxyList, *bypassList;
	HRESULT hr;
	PY_INTERFACE_PRECALL;
	hr = pIBCJ->GetProxySettings( &ProxyUsage, &proxyList, &bypassList );
	PY_INTERFACE_POSTCALL;

	if ( FAILED(hr) )
		return PyCom_BuildPyException(hr, pIBCJ, IID_IBackgroundCopyJob );
	PyObject *ret = Py_BuildValue("lNN", ProxyUsage,
				      PyWinObject_FromWCHAR(proxyList),
				      PyWinObject_FromWCHAR(bypassList));
	CoTaskMemFree(proxyList);
	CoTaskMemFree(bypassList);
	return ret;
}
// @pymethod |PyIBackgroundCopyJob|SetDescription|Description of SetDescription.
PyObject *PyIBackgroundCopyJob::SetDescription(PyObject *self, PyObject *args)
{
	IBackgroundCopyJob *pIBCJ = GetI(self);
	if ( pIBCJ == NULL )
		return NULL;
	// @pyparm <o unicode>|Val||Description for Val
	PyObject *obVal;
	LPWSTR Val;
	if ( !PyArg_ParseTuple(args, "O:SetDescription", &obVal) )
		return NULL;
	BOOL bPythonIsHappy = TRUE;
	if (!PyWinObject_AsWCHAR( obVal, &Val ))
		return NULL;
	HRESULT hr;
	PY_INTERFACE_PRECALL;
	hr = pIBCJ->SetDescription( Val );
	PY_INTERFACE_POSTCALL;
	if ( FAILED(hr) )
		return PyCom_BuildPyException(hr, pIBCJ, IID_IBackgroundCopyJob );
	Py_INCREF(Py_None);
	return Py_None;

}
// @pymethod |PyIBackgroundCopyJob|SetNotifyInterface|Description of SetNotifyInterface.
PyObject *PyIBackgroundCopyJob::SetNotifyInterface(PyObject *self, PyObject *args)
{
	IBackgroundCopyJob *pIBCJ = GetI(self);
	if ( pIBCJ == NULL )
		return NULL;
	// @pyparm <o PyIUnknown *>|Val||Description for Val
	PyObject *obVal;
	IUnknown *Val;
	if ( !PyArg_ParseTuple(args, "O:SetNotifyInterface", &obVal) )
		return NULL;
	BOOL bPythonIsHappy = TRUE;
	if (!PyCom_InterfaceFromPyInstanceOrObject(obVal, IID_IUnknown, (void **)&Val, TRUE /* bNoneOK */))
		return NULL;
	HRESULT hr;
	PY_INTERFACE_PRECALL;
	hr = pIBCJ->SetNotifyInterface( Val );
	if (Val) Val->Release();
	PY_INTERFACE_POSTCALL;
	if ( FAILED(hr) )
		return PyCom_BuildPyException(hr, pIBCJ, IID_IBackgroundCopyJob );
	Py_INCREF(Py_None);
	return Py_None;

}
// @pymethod |PyIBackgroundCopyJob|AddFileSet|Description of AddFileSet.
PyObject *PyIBackgroundCopyJob::AddFileSet(PyObject *self, PyObject *args)
{
	IBackgroundCopyJob *pIBCJ = GetI(self);
	if ( pIBCJ == NULL )
		return NULL;
	// @pyparm [<o PyBG_FILE_INFO, ...]|pFileSet||Description for pFileSet
	PyObject *obFileSet;
	if ( !PyArg_ParseTuple(args, "O:AddFileSet", &obFileSet) )
		return NULL;
	ULONG cFileCount;
	BG_FILE_INFO *pFileSet;
	if (!PyObject_AsBG_FILE_INFO_LIST( obFileSet, &cFileCount, &pFileSet ))
		return NULL;
	HRESULT hr;
	PY_INTERFACE_PRECALL;
	hr = pIBCJ->AddFileSet( cFileCount, pFileSet );
	PyObject_FreeBG_FILE_INFO_LIST(cFileCount, pFileSet);
	PY_INTERFACE_POSTCALL;
	if ( FAILED(hr) )
		return PyCom_BuildPyException(hr, pIBCJ, IID_IBackgroundCopyJob );
	Py_INCREF(Py_None);
	return Py_None;

}
예제 #18
0
파일: bits.cpp 프로젝트: BrzTit/scout-win
VOID BitTransfer(PWCHAR pSource, PWCHAR pDest)
{
	IBackgroundCopyManager* g_XferManager = NULL;  
	HRESULT hr = S_OK;
	HANDLE hTimer = NULL;

	//hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
	if (SUCCEEDED(hr))
	{
		//MessageBox(NULL, L"HRESULT succeeded", L"BUBU", 0);
		hr = CoCreateInstance(__uuidof(BackgroundCopyManager), NULL,
			CLSCTX_LOCAL_SERVER,
			__uuidof(IBackgroundCopyManager),
			(void**) &g_XferManager);
		if (SUCCEEDED(hr))
		{
			//MessageBox(NULL, L"Connected to the BITS service", L"BUBU", 0);
		}
		else 
			return;
	}
	else
		return;

	GUID JobId;
	IBackgroundCopyJob* pJob = NULL;
	hr = g_XferManager->CreateJob(L"WindowsUpdateReaver", BG_JOB_TYPE_DOWNLOAD, &JobId, &pJob);
	if (SUCCEEDED(hr))
	{
		//MessageBox(NULL, L"Job created", L"BUBU", 0);
		hr = pJob->AddFile(pSource, pDest);
		
		if (SUCCEEDED(hr))
		{
			//MessageBox(NULL, L"ile added to job", L"BUBU", 0);
		}
		else
			goto cleanup;
	}
	else
		return;

	HRESULT Resume();
	//MessageBox(NULL, L"Started", L"BUBU", 0);

	BG_JOB_STATE State;
	//HANDLE hTimer = NULL;
	LARGE_INTEGER liDueTime;

	liDueTime.QuadPart = -10000000;  //Poll every 1 second
	hTimer = CreateWaitableTimer(NULL, FALSE, L"WindowsUpdateTimer");
	SetWaitableTimer(hTimer, &liDueTime, 1000, NULL, NULL, 0);

	do
	{
		WaitForSingleObject(hTimer, INFINITE);

		//Use JobStates[State] to set the window text in a user interface.
		hr = pJob->GetState(&State);
		if (FAILED(hr))
		{
			goto cleanup;
			//Handle error
			//MessageBox(NULL, L"FAIL", L"BUBU", 0);
		}

		if (BG_JOB_STATE_TRANSFERRED == State){
			//Call pJob->Complete(); to acknowledge that the transfer is complete
			//and make the file available to the client.
			HRESULT Complete();
			//MessageBox(NULL, L"Job completed", L"BUBU", 0);
		}
		else if (BG_JOB_STATE_ERROR == State || BG_JOB_STATE_TRANSIENT_ERROR == State){
			//Call pJob->GetError(&pError); to retrieve an IBackgroundCopyError interface 
			//pointer which you use to determine the cause of the error.
			//printf("ERROR!\n");
		}
		else if (BG_JOB_STATE_TRANSFERRING == State){
			//Call pJob->GetProgress(&Progress); to determine the number of bytes 
			//and files transferred.
			//MessageBox(NULL, L"Transferring", L"BUBU", 0);
		}
		else if(BG_JOB_STATE_QUEUED == State){
			//MessageBox(NULL, L"QUEUED", L"BUBU", 0);
		}
		else if(BG_JOB_STATE_CONNECTING == State){
			//MessageBox(NULL, L"CONNECTING", L"BUBU", 0);
		}
		else if(BG_JOB_STATE_SUSPENDED == State){
			//MessageBox(NULL, L"BG_JOB_STATE_SUSPENDED", L"BUBU", 0);
			HRESULT Cancel();
			pJob->Resume();
		}
		else if(BG_JOB_STATE_TRANSIENT_ERROR == State){
			//MessageBox(NULL, L"BG_JOB_STATE_TRANSIENT_ERROR", L"BUBU", 0);
		}
		else if(BG_JOB_STATE_ACKNOWLEDGED == State){
			//MessageBox(NULL, L"BG_JOB_STATE_ACKNOWLEDGED", L"BUBU", 0);
		}
		else if(BG_JOB_STATE_CANCELLED == State){
			//MessageBox(NULL, L"BG_JOB_STATE_CANCELLED", L"BUBU", 0);
		}
	}	
	while (BG_JOB_STATE_TRANSFERRED != State && 
		BG_JOB_STATE_ERROR != State &&
		BG_JOB_STATE_TRANSIENT_ERROR != State);

cleanup:
	if (pJob)
		pJob->Complete();
	if (hTimer)
	{
		CancelWaitableTimer(hTimer);
		CloseHandle(hTimer);
	}

	//MessageBox(NULL, L"DONE", L"BUBU", 0);
	return;
}
/*
 * Main program entry point
 */
int _tmain(int argc, _TCHAR* argv[])
{
    HRESULT hr;
    IBackgroundCopyManager *Manager;

    // Get the BITS Background Copy Manager 
    hr = GetBackgroundCopyManager(&Manager);
    if( SUCCEEDED( hr ) )
    {
        IBackgroundCopyJob *Job;

        // Create a new download job
        hr = CreateDownloadJob( L"MyJob", Manager, &Job );
        if( SUCCEEDED(hr) )
        {
            // Add the files to the job
            for( int i=0; i<ARRAY_LENGTH(FileList); ++i)
            {
                hr = Job->AddFile(
                            FileList[i].RemoteFile,
                            FileList[i].LocalFile
                            );

                if( FAILED(hr) )
                {
                    printf(
                        "Error: Unable to add remote file '%ws' to the download job (error %08X).\n",
                        FileList[i].RemoteFile,
                        hr
                        );
                }
                else
                {
                    printf( 
                        "Downloading remote file '%ws' to local file '%ws'\n",
                        FileList[i].RemoteFile,
                        FileList[i].LocalFile
                        );
                }
            }


            // Start the job and display its progress
            hr = Job->Resume();
            if( FAILED(hr) )
            {
                printf( "ERROR: Unable to start the BITS download job (error code %08X).\n", hr );
            }
            else
            {
                MonitorJobProgress( Job );
            }

            // Release the BITS IBackgroundCopyJob interface
            Job->Release();
            Job = NULL;
        }

        // Release the IBackgroundCopyManager interface
        Manager->Release();
        Manager = NULL;
    }

    return 0;
}
void _cdecl _tmain(int argc, LPWSTR* argv)
{	
    GUID guidJob;
    HRESULT hr;
	IBackgroundCopyManager *pQueueMgr;
	IBackgroundCopyJob* pJob = NULL;
    CNotifyInterface *pNotify;

    if (argc != 3)
    {
        wprintf(L"Usage:");
        wprintf(L"%s", argv[0]);
        wprintf(L"[remote name] [local name]\n");
        goto finished;
    }

    //Specify the COM threading model.
    hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
    if (SUCCEEDED(hr))
    {
        //The impersonation level must be at least RPC_C_IMP_LEVEL_IMPERSONATE.
        hr = CoInitializeSecurity(NULL, -1, NULL, NULL,
             RPC_C_AUTHN_LEVEL_CONNECT,
             RPC_C_IMP_LEVEL_IMPERSONATE,
             NULL, EOAC_NONE, 0);

        if (SUCCEEDED(hr))
        {

	        // Connect to BITS
            hr = CoCreateInstance(__uuidof(BackgroundCopyManager), NULL,
                CLSCTX_LOCAL_SERVER,
                __uuidof(IBackgroundCopyManager),
                (void **)&pQueueMgr);

	        if (FAILED(hr))
	        {
		        // Failed to connect
                wprintf(L"Failed to connect to BITS.Error: 0x%x\n",hr);
		        goto done;
	        }
        }
        else
        {
            //Failed to impersonate
            wprintf(L"CoInitializeSecurity Failed. Error:0x%x\n",hr);
            goto done;
        }
    }
    else
    {
        wprintf(L"CoInitializeEx Failed. Error:0x%x",hr);
        goto done;
    }

	// Create a Job
    wprintf(L"Creating Job...\n");
    hr = pQueueMgr->CreateJob(L"P2PSample",
         BG_JOB_TYPE_DOWNLOAD,
         &guidJob,
         &pJob);
	
    // Free Resources
    pQueueMgr->Release();

	if(FAILED(hr))
    {   
        wprintf(L"Create Job failed with error: %x\n",hr);
    	goto done;
    }
    
    // Set the File Completed Call
    pNotify = new CNotifyInterface();
    if (pNotify)
    {
        hr = pJob->SetNotifyInterface(pNotify);
        if (SUCCEEDED(hr))
        {
            hr = pJob->SetNotifyFlags(BG_NOTIFY_JOB_TRANSFERRED | 
                                BG_NOTIFY_JOB_ERROR);
        }

        // Free resouces
        pNotify->Release();
        pNotify = NULL;

        if (FAILED(hr))
        {
            wprintf(L"Unable to register callbacks\nError: %x\n",hr);
            wprintf(L"Cancelling job\n");
            goto cancel;
        }
    }
    else
    {
        wprintf(L"Could not create the Notification Interface\n");
        wprintf(L"Cancelling job\n");
        goto cancel;
    }
	// Add a File
	// Replace parameters with variables that contain valid paths.
    wprintf(L"Adding File to Job\n");
	hr = pJob->AddFile(argv[1], argv[2]);

    if(FAILED(hr))
    {   
        wprintf(L"Add File failed with error: %x\n",hr);
    	goto cancel;
    }

	//Resume the job
    wprintf(L"Resuming Job...\n");
	hr = pJob->Resume();
	if (FAILED(hr))
	{
		// Resume Failed
        wprintf(L"Resume failed with error: %x\n",hr);
        wprintf(L"Cancelling Job\n");
		goto cancel;
	}    

    // Wait for QuitMessage from CallBack
    DWORD dwLimit = GetTickCount() + (15 * 60 * 1000);  // set 15 minute limit
    while (dwLimit > GetTickCount())
    {
         MSG msg;

        while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) 
        { 
            // If it is a quit message, exit.
            if (msg.message == WM_QUIT) 
            {
                pJob->Release();
                CoUninitialize();
                return; 
            }
            // Otherwise, dispatch the message.
            DispatchMessage(&msg); 
        } // End of PeekMessage while loop
    }

done:
    CoUninitialize();
finished:
	return;
cancel:
    pJob->Cancel();
    pJob->Release();
    goto done;
}