コード例 #1
0
// @pymethod |PyIBackgroundCopyManager|CreateJob|Description of CreateJob.
PyObject *PyIBackgroundCopyManager::CreateJob(PyObject *self, PyObject *args)
{
	IBackgroundCopyManager *pIBCM = GetI(self);
	if ( pIBCM == NULL )
		return NULL;
	// @pyparm <o unicode>|DisplayName||Description for DisplayName
	BG_JOB_TYPE Type;
	// @pyparm int|Type||Job Type (See BG_JOB_TYPE_*)
	GUID pJobId;
	PyObject *obDisplayName;
	LPWSTR DisplayName;
	IBackgroundCopyJob *ppJob;
	if ( !PyArg_ParseTuple(args, "Ol:CreateJob", &obDisplayName, &Type) )
		return NULL;
	BOOL bPythonIsHappy = TRUE;
	if (bPythonIsHappy && !PyWinObject_AsBstr(obDisplayName, &DisplayName)) bPythonIsHappy = FALSE;
	if (!bPythonIsHappy) return NULL;
	HRESULT hr;
	PY_INTERFACE_PRECALL;
	hr = pIBCM->CreateJob( DisplayName, Type, &pJobId, &ppJob );
	SysFreeString(DisplayName);

	PY_INTERFACE_POSTCALL;

	if ( FAILED(hr) )
		return PyCom_BuildPyException(hr, pIBCM, IID_IBackgroundCopyManager );
	return PyCom_PyObjectFromIUnknown(ppJob, IID_IBackgroundCopyJob, FALSE);
}
コード例 #2
0
// @pymethod |PyIBackgroundCopyManager|GetErrorDescription|Description of GetErrorDescription.
PyObject *PyIBackgroundCopyManager::GetErrorDescription(PyObject *self, PyObject *args)
{
	IBackgroundCopyManager *pIBCM = GetI(self);
	if ( pIBCM == NULL )
		return NULL;
	// @pyparm int|hResult||Description for hResult
	// @pyparm int|LanguageId||Description for LanguageId
	WCHAR* pszErrorDescription;
	PyObject *obpErrorDescription;
	HRESULT hResult;
	DWORD LanguageId;
	if ( !PyArg_ParseTuple(args, "ll:GetErrorDescription", &hResult, &LanguageId) )
		return NULL;
	HRESULT hr;
	PY_INTERFACE_PRECALL;
	hr = pIBCM->GetErrorDescription( hResult, LanguageId, &pszErrorDescription );

	PY_INTERFACE_POSTCALL;

	if ( FAILED(hr) )
		return PyCom_BuildPyException(hr, pIBCM, IID_IBackgroundCopyManager );

	obpErrorDescription = PyWinObject_FromWCHAR(pszErrorDescription);
	return obpErrorDescription;
}
コード例 #3
0
// @pymethod |PyIBackgroundCopyManager|EnumJobs|Description of EnumJobs.
PyObject *PyIBackgroundCopyManager::EnumJobs(PyObject *self, PyObject *args)
{
    IBackgroundCopyManager *pIBCM = GetI(self);
    if ( pIBCM == NULL )
        return NULL;
    // @pyparm int|dwFlags||Description for dwFlags
    DWORD dwFlags;
    IEnumBackgroundCopyJobs *ppEnum;
    if ( !PyArg_ParseTuple(args, "l:EnumJobs", &dwFlags) )
        return NULL;
    HRESULT hr;
    PY_INTERFACE_PRECALL;
    hr = pIBCM->EnumJobs( dwFlags, &ppEnum );

    PY_INTERFACE_POSTCALL;

    if ( FAILED(hr) )
        return PyCom_BuildPyException(hr, pIBCM, IID_IBackgroundCopyManager );
    PyObject *obppEnum;

    obppEnum = PyCom_PyObjectFromIUnknown(ppEnum, IID_IEnumBackgroundCopyJobs, FALSE);
    PyObject *pyretval = Py_BuildValue("O", obppEnum);
    Py_XDECREF(obppEnum);
    return pyretval;
}
コード例 #4
0
// @pymethod |PyIBackgroundCopyManager|GetJob|Description of GetJob.
PyObject *PyIBackgroundCopyManager::GetJob(PyObject *self, PyObject *args)
{
    IBackgroundCopyManager *pIBCM = GetI(self);
    if ( pIBCM == NULL )
        return NULL;
    // @pyparm <o PyIID>|jobID||Description for jobID
    PyObject *objobID;
    IID jobID;
    IBackgroundCopyJob *ppJob;
    if ( !PyArg_ParseTuple(args, "O:GetJob", &objobID) )
        return NULL;
    BOOL bPythonIsHappy = TRUE;
    if (!PyWinObject_AsIID(objobID, &jobID)) bPythonIsHappy = FALSE;
    if (!bPythonIsHappy) return NULL;
    HRESULT hr;
    PY_INTERFACE_PRECALL;
    hr = pIBCM->GetJob( jobID, &ppJob );

    PY_INTERFACE_POSTCALL;

    if ( FAILED(hr) )
        return PyCom_BuildPyException(hr, pIBCM, IID_IBackgroundCopyManager );
    PyObject *obppJob;

    obppJob = PyCom_PyObjectFromIUnknown(ppJob, IID_IBackgroundCopyJob, FALSE);
    PyObject *pyretval = Py_BuildValue("O", obppJob);
    Py_XDECREF(obppJob);
    return pyretval;
}
コード例 #5
0
/*
 * 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;
}
コード例 #6
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;
}