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

📄 obexsquirt.cpp

📁 這是 OEBX 使用在 Windows CE 上的範例程式 可以正確的模擬與執行
💻 CPP
📖 第 1 页 / 共 3 页
字号:
            if (SUCCEEDED (hr)) 
                pDev->dwFlags |= DEV_FLAG_DEVBOUND;
            else 
                Add2List (hWnd, TEXT("BindToDevice failed %x %d"), hr, 
                          GetLastError());

            if (SUCCEEDED (hr)) {
                i = SendFile (hWnd, pDevice, szName, pDev->dwFlags);
                Add2List (hWnd, TEXT("SendFile returned %d"), i);
            }
        }
        // Restart Enumeration after transfer
        if (fObex2IF)
            ((IObex2 *)pObex)->PauseDeviceEnum (FALSE);
        else {
            pObex->StartDeviceEnum();
            ResetDevList ();
            SendDlgItemMessage (hWnd, IDD_DEVICES, LB_RESETCONTENT, 0, 0);
        }
    }
    return 0;
}

//----------------------------------------------------------------------
// DoMainCommandDevList - Process Device list box commands.
//
LPARAM DoMainCommandDevList (HWND hWnd, WORD idItem, HWND hwndCtl,
                          WORD wNotifyCode) {
    int i;
    PMYOBEXDEVICEINFO pDev;

    if (wNotifyCode == LBN_SELCHANGE) {
        i = SendDlgItemMessage (hWnd, IDD_DEVICES, LB_GETCURSEL, 0, 0);
        if (i != LB_ERR) {
            pDev = (PMYOBEXDEVICEINFO)SendDlgItemMessage (hWnd, 
                                       IDD_DEVICES, LB_GETITEMDATA,i,0);

            EnableWindow (GetDlgItem (hWnd, IDD_SENDFILE), TRUE);
        }
    }
    return 0;
}
//----------------------------------------------------------------------
// Add2List - Add string to the report list box.
// 
void Add2List (HWND hWnd, LPTSTR lpszFormat, ...) {
    int nBuf, nLen;
    TCHAR szBuffer[512];
    va_list args;
    if (hWnd == 0)
        hWnd = hMain;

    EnterCriticalSection (&csPrintf);

    va_start(args, lpszFormat);
    nBuf = _vstprintf(szBuffer, lpszFormat, args);
    va_end(args);

    nLen = (lstrlen (szBuffer)+1) * sizeof (TCHAR);
    WriteMsgQueue (hQWrite, (LPBYTE)szBuffer, nLen, 0, 0);
    PostMessage (hWnd, MYMSG_PRINTF, 0, 0);
    LeaveCriticalSection (&csPrintf);
}
//----------------------------------------------------------------------
// MySetWindowText - Set Window title to passed printf style string.
//
void MySetWindowText (HWND hWnd, LPTSTR lpszFormat, ...) {
    int nBuf, nLen;
    TCHAR szBuffer[512];
    va_list args;

    EnterCriticalSection (&csPrintf);

    va_start(args, lpszFormat);
    nBuf = _vstprintf(szBuffer, lpszFormat, args);
    va_end(args);

    nLen = (lstrlen (szBuffer)+1) * sizeof (TCHAR);
    WriteMsgQueue (hQWrite, (LPBYTE)szBuffer, nLen, 0, MSGQUEUE_MSGALERT);
    PostMessage (hWnd, MYMSG_PRINTF, 0, 0);
    LeaveCriticalSection (&csPrintf);
}
//----------------------------------------------------------------------
// MyYield - Flushes the message queue during long operations
//
BOOL MyYield () {
    MSG msg;
    int rc = 0;

    while (PeekMessage (&msg, NULL, 0, 0, PM_NOREMOVE)) {
        if (msg.message == WM_QUIT)
            return FALSE;

        GetMessage (&msg, NULL, 0, 0);
        if ((hMain == 0) || !IsDialogMessage (hMain, &msg)) {
            TranslateMessage (&msg);
            DispatchMessage (&msg);
        }
    }
    return TRUE;
}
//----------------------------------------------------------------------
// InitObex - Initialize the Obex subsystem.
//
int InitObex (HWND hWnd) {
    HRESULT hr;

    hr = CoCreateInstance (__uuidof(Obex), NULL, CLSCTX_INPROC_SERVER, 
                           __uuidof(IObex2), (void **)&pObex);
    if(FAILED(hr)) {
        hr = CoCreateInstance (__uuidof(Obex), NULL, CLSCTX_INPROC_SERVER, 
                               __uuidof(IObex), (void **)&pObex);
    } else
        fObex2IF = TRUE;

    if(FAILED(hr)) {
        Add2List (hWnd, TEXT("Obex initialization failed! %d %x\n"), 
                  hr, GetLastError());
        return 0;
    }
    if (pObex != NULL)
        pObex->Initialize ();
    else
        return 0;

    //set device caps
    IObexCaps *pObexCaps = NULL;
    hr = pObex->QueryInterface(IID_IObexCaps, (LPVOID *)&pObexCaps);
    if(SUCCEEDED(hr)) {
        pObexCaps->SetCaps(SEND_DEVICE_UPDATES);
        pObexCaps->Release();
    }

    InitializeCriticalSection(&csLock);
    EnterCriticalSection(&csLock);    

    MyObexSink *pSink;
    pSink = new MyObexSink(hWnd);
    if (!pSink) {
        LeaveCriticalSection(&csLock);
        return 0;
    }

    // Create connection point container
    hr = pObex->QueryInterface (IID_IConnectionPointContainer, 
                                (LPVOID *)&pContainer); 
    if (!SUCCEEDED(hr) || (pContainer == 0)) {
        LeaveCriticalSection(&csLock);
        return 0;
    }

    hr = pContainer->FindConnectionPoint (IID_IObexSink, &pConPt);
    if (!SUCCEEDED(hr) || (pConPt == 0)) {
        pContainer->Release();
        LeaveCriticalSection(&csLock);
        return 0;
    }

    // Ask for notifications
    hr = pConPt->Advise((IUnknown *)pSink, &dwCookie);

    LeaveCriticalSection(&csLock);

    // Start device enumeration
    if (ERROR_SUCCESS != pObex->StartDeviceEnum())
        return 0;
    
    IDeviceEnum *pDeviceEnum = 0;
    hr = pObex->EnumDevices(&pDeviceEnum, CLSID_NULL);
    
    if(!SUCCEEDED(hr) || (pDeviceEnum == 0))
        return NULL;

    Add2List (hWnd, TEXT("EnumDevices succeeded"));
    pDeviceEnum->Release ();
    return 1;
}
//----------------------------------------------------------------------
// SendFile - Sends a file to another obex device
//
int SendFile (HWND hWnd, IObexDevice *pDevice, LPTSTR pszFileName, 
              DWORD dwFlags) {
    LPTSTR pszName;
    DWORD dwBytesWritten;
    int nCnt, nFileSize, nTotal;
    HRESULT hr;
    HANDLE hFile;
    PBYTE pBuff;

    Add2List (hWnd, TEXT("Sending file %s"), pszFileName);

    pBuff = (PBYTE)LocalAlloc (LPTR, BUFFSIZE);
    if (pBuff == 0) return 0;
        
    // prune the path from the file name
    pszName = wcsrchr (pszFileName, '\\');
    if (pszName == 0)
        pszName = pszFileName;
    else
        pszName++;

    // Open the file
    hFile = CreateFile (pszFileName, GENERIC_READ, FILE_SHARE_READ,
                        NULL, OPEN_EXISTING, 0, NULL);
    if (hFile == INVALID_HANDLE_VALUE) {
        Add2List (hWnd, TEXT("file opened failed. rc %d"), 
                  GetLastError());
        return -1;
    }

    // Get file size
    nFileSize = GetFileSize (hFile, NULL);
    if (!MyYield ()) return 0;

    IHeaderCollection *pHC = 0;
    hr = CoCreateInstance(__uuidof(HeaderCollection), NULL, 
                     CLSCTX_INPROC_SERVER, __uuidof(IHeaderCollection),
                     (void **)&pHC);
    if (!MyYield () || FAILED(hr)) {
        CloseHandle (hFile);
        return -2;
    }
    if (dwFlags & DEV_SERVICE_FTP)
        pHC->AddTarget (sizeof (CLSID_FileExchange_NetOrder),
                        (UCHAR *)&CLSID_FileExchange_NetOrder);

    // Connect to device
    hr = pDevice->Connect (NULL, 0, pHC);
    if (!MyYield () || FAILED(hr)) {
        Add2List (hWnd, TEXT("Connect fail %x %d"), hr, GetLastError());
        pHC->Release();
        CloseHandle (hFile);
        return -3;
    }
    Add2List (hWnd, TEXT("Connected..."));
    //get a header collection
    IHeaderCollection *pFileHC = 0;
    hr = CoCreateInstance(__uuidof(HeaderCollection), NULL, 
                     CLSCTX_INPROC_SERVER, __uuidof(IHeaderCollection),
                     (void **)&pFileHC);
    if (!MyYield () || FAILED(hr)) {
        pHC->Release();
        pDevice->Disconnect (pHC);
        CloseHandle (hFile);
        return -2;
    }
    // Add file name to header
    hr = pFileHC->AddName(pszName);
    if (!MyYield () || FAILED(hr)) {
        pHC->Release();
        pFileHC->Release();
        CloseHandle (hFile);
        return -3;
    }
    // Send header
    IStream *stOut = 0;    
    hr = pDevice->Put(pFileHC, &stOut);  
    if (!MyYield () || FAILED(hr)) {
        pDevice->Disconnect (pHC);
        pHC->Release();
        pFileHC->Release();
        CloseHandle (hFile);
        return -4;
    }
    // Send the data
    nTotal = nFileSize;
    while (nFileSize) {

        if (!MyYield ()) break;
        MySetWindowText (hWnd, TEXT ("%02d%% sent"), 
                         (nTotal-nFileSize)*100/nTotal);
        // Send up to the block size
        nCnt = min (BUFFSIZE, nFileSize);

        if (!ReadFile (hFile, pBuff, nCnt, &dwBytesWritten, FALSE)) {
            Add2List (hWnd, TEXT("ReadFile error %d "), GetLastError());
            break;
        }
        nCnt = (int)dwBytesWritten;

        Add2List (hWnd, TEXT("sending %d bytes"), nCnt);

        if (!MyYield ()) break;
        hr = stOut->Write (pBuff, nCnt, &dwBytesWritten);
        if(FAILED(hr)) {
            Add2List (hWnd, TEXT("send error %x %d"), hr, GetLastError());
            break;
        }
        nFileSize -= (int)dwBytesWritten;
    }
    MySetWindowText (hWnd, (LPTSTR)szTitleText);
    MyYield ();
    stOut->Commit (STGC_DEFAULT);
    // Clean up
    stOut->Release();
    pDevice->Disconnect (pHC);
    if(pHC)
        pHC->Release();
    CloseHandle(hFile);  
    if(pFileHC)
        pFileHC->Release();
    return 0;
}

⌨️ 快捷键说明

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