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

📄 filebrowser.cpp

📁 这个是微软WINCE的OBEX协议例子
💻 CPP
📖 第 1 页 / 共 5 页
字号:
                {
                    DWORD written;
                    if(!WriteFile(hFile, 
                        inBuf, 
                        cbJustRead, &written, 0))
                    {
                        hr = E_FAIL;
                        break;
                    }
                    dwBytesSent += cbJustRead;
                    SendMessage(g_hWndStatus, WM_DO_UPDATE, 0, 0);
                }
            } while(SUCCEEDED(hr) && FALSE == g_fAbort);
            
            //
            // Close up the file
            CloseHandle(hFile);
             
            //
            // Handle the ABORT
            if(g_fAbort)  {
                IHeaderCollection *pAbortHeaderCollection = NULL;
                if(SUCCEEDED(CoCreateInstance(CLSID_HeaderCollection, 
                    NULL, 
                    CLSCTX_INPROC_SERVER, 
                    IID_IHeaderCollection,  
                    (void **)&pAbortHeaderCollection)) && NULL != pAbortHeaderCollection) 
               {                    
                        g_pObexDevice->Abort(pAbortHeaderCollection);                       
                        pAbortHeaderCollection->Release();
               }     
               g_fAbort = FALSE;
               
               //
               // Delete the file we just created
               if(0 == DeleteFile(saveFile)) {
                    MessageBox(g_hWndStatus, (L"Transfer aborted -- %s couldnt be deleted from local filesystem", saveFile), L"Error", MB_OK);
               } else {
                    MessageBox(g_hWndStatus, L"Transfer aborted", L"Finished", MB_OK);
               }
            }
            
            if(dwBytesSent)
                hr=S_OK;
            else
                hr = E_FAIL;
        }
        else
        {
            MessageBox(g_hWndStatus, (L"Error getting opening %s for writing", saveFile), L"Error", MB_OK);  
        }
        
        //hide the progress bar
        ShowWindow(g_hWndStatus, SW_HIDE);
        UpdateWindow(g_hWndStatus);
        UpdateWindow(g_hWnd);
        myStream->Release();
    }
    
    pHeaderCollection->Release();
    return hr;
}




HRESULT RefreshDirectory(LPTSTR pswDir, HTREEITEM htiParent)
{
    ASSERT(g_pObexDevice && g_pObex);
    HCURSOR hCurOld;
    HRESULT hr = E_FAIL;
    hCurOld = SetCursor(LoadCursor(NULL, IDC_WAIT));
    
    if(!g_pObexDevice || FAILED(g_pObexDevice->SetPath(pswDir, SETPATH_FLAG_DONT_CREATE, 0)))
    {
        SetCursor(hCurOld);
        return E_FAIL;
    }
    
    UINT mask = TVIF_PARAM | TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE;
    
    //add yourself to the node
    if(g_htiParentNode == 0)
    {       
        g_htiParentNode = AddItemToTree(g_hTreeWnd, 
            TVIF_PARAM | TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE, 
            szRequestDir, 
            1,
            1,
            TVIS_BOLD, 
            0, 
            (LPARAM)&g_ParentNode, 
            0,
            TVI_SORT);
        htiParent = g_htiParentNode;
        g_ParentNode.fTraversed = TRUE;
        g_ParentNode.fDir = TRUE;
        
    }
    
    
    //get a header collection
    IHeaderCollection *pHeaderCollection;
    hr = CoCreateInstance(CLSID_HeaderCollection, 
        NULL, 
        CLSCTX_INPROC_SERVER, 
        IID_IHeaderCollection,  
        (void **)&pHeaderCollection);
    
    //put a name of "" so we get the directory listing
    pHeaderCollection->AddName(L"");
    pHeaderCollection->AddType (sizeof(LISTING_TYPE), (unsigned char *)LISTING_TYPE);
    
    IStream *myStream;   
    hr = g_pObexDevice->Get(pHeaderCollection, &myStream);  
    
    if(FAILED(hr))
    {
        SetCursor(hCurOld);
        return E_FAIL;
    }
    
    
    WIN32_FIND_DATA wfd;   
    ObexFileSearch *pSearch = ObexFindFileFirst (myStream, L"*.*", &wfd);
    if (pSearch) {
        do {
            DIR_NODE *pdn = 0;
            if (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
            {                
                ASSERT(NULL != pswDir);
                
                UINT uipswDirSize = wcslen(pswDir);
                UINT uiFileNameSize = wcslen(wfd.cFileName);
                UINT uiDirSize = uipswDirSize + uiFileNameSize + 2;
                pdn = new DIR_NODE();    
                pdn->fDir = TRUE;
                pdn->fTraversed = FALSE;
                pdn->pFileName = 0;
                pdn->pPath = new TCHAR[uiDirSize];
                pdn->dwSize = 0;
                
                if(pswDir[uipswDirSize-1] != '\\' && wfd.cFileName[uipswDirSize-1] != '\\')
                    swprintf(pdn->pPath, L"%s\\%s", pswDir, wfd.cFileName);
                else
                    swprintf(pdn->pPath, L"%s%s", pswDir, wfd.cFileName);
                
                pdn->htiParent = AddItemToTree(
                    g_hTreeWnd, 
                    mask, 
                    wfd.cFileName, 
                    1,
                    1,
                    TVIS_BOLD, 
                    0, 
                    (LPARAM)pdn, 
                    htiParent,
                    TVI_SORT);
            }
            else
            {
                UINT uiFileSize = wcslen(pswDir) + wcslen(wfd.cFileName) + 2;
                UINT uiDirSize = wcslen(pswDir) + 1;
                pdn = new DIR_NODE(); 
                pdn->pFileName = new TCHAR[uiFileSize];
                pdn->pPath = new TCHAR[uiDirSize];
                pdn->fDir = FALSE;
                pdn->fTraversed = FALSE;
                pdn->dwSize = wfd.nFileSizeLow;
                swprintf(pdn->pPath, L"%s", pswDir);
                swprintf(pdn->pFileName, L"%s", wfd.cFileName);
                
                AddItemToTree(
                    g_hTreeWnd, 
                    mask, 
                    wfd.cFileName, 
                    0,
                    0,
                    TVIS_BOLD, 
                    0, 
                    (LPARAM)pdn, 
                    htiParent,
                    TVI_SORT);
            }
            
            //insert the DIR NODE into a list
            DIRNODE_LIST *pTemp = new DIRNODE_LIST();
            pTemp->pNext = g_pNodeList;
            pTemp->pItem = pdn;
            g_pNodeList = pTemp;
            
        } while (ObexFindFileNext (pSearch, &wfd));
        
        ObexCloseSearch (pSearch);
    }
    
    myStream->Release();
    pHeaderCollection->Release();
    SetCursor(hCurOld);
    return S_OK;
}




HRESULT SendFile()
{   
    ASSERT(g_pObexDevice && g_pObex && g_SelectedNode);
    HRESULT hr;
    HANDLE hFile = INVALID_HANDLE_VALUE;
    TCHAR szFileName[MAX_PATH];
    TCHAR szTitleName[MAX_PATH];
    IStream *myStream = 0;    
    IHeaderCollection *pHeaderCollection = 0;

    if(!g_pObexDevice || (g_fServerBrowsable && FAILED(g_pObexDevice->SetPath(g_SelectedNode->pPath, SETPATH_FLAG_DONT_CREATE, 0))))
    {
        return E_FAIL;
    }
    
    
    //fetch a file name    
    szFileName[0]=TEXT('\0');
    szTitleName[0]=TEXT('\0');;
    if(0 == PopFileOpenDlg(g_hInst, g_hWnd, szFileName, szTitleName))
        return E_FAIL;

    //create a file with this name
    hFile = CreateFile (szFileName, 
            GENERIC_READ, 0, NULL, 
            OPEN_EXISTING, 0, NULL);      

    if (hFile == INVALID_HANDLE_VALUE)
    {
        MessageBox(NULL, (L"Error getting opening %s for reading", szFileName), L"Error", MB_OK); 
        return E_FAIL;
    }

    //set global transmission lock
    EnterCriticalSection(&g_csLock);
    ASSERT(!g_fTransferInProgress);
    g_fTransferInProgress = TRUE;         
    LeaveCriticalSection(&g_csLock);

    DWORD dwFileLen = GetFileSize (hFile, NULL);
    
    //get a header collection
    hr = CoCreateInstance(CLSID_HeaderCollection, 
        NULL, 
        CLSCTX_INPROC_SERVER, 
        IID_IHeaderCollection,  
        (void **)&pHeaderCollection);
    
    
    if(SUCCEEDED(hr))
    {
        //put in the name
        LPTSTR name = wcsrchr(szFileName, '\\');
        ASSERT(name);
        if(name)
            name ++;
        
        if(!name || !(*name))
            name = szFileName;
        hr = pHeaderCollection->AddName(name);
    }
    
    if(SUCCEEDED(hr))
    {
        hr = pHeaderCollection->AddLength (dwFileLen);
    }

    if(SUCCEEDED(hr)) {
        hr = g_pObexDevice->Put(pHeaderCollection, &myStream);  
    } else {
        MessageBox(g_hWndStatus, L"Error getting Sending PUT -- perhaps the server doesnt allow them", L"Error", MB_OK);
    }
    if(SUCCEEDED(hr))
    {
        dwBytesTotal = dwFileLen;
        dwBytesSent = 0;
        
        //load the status window
        ShowWindow(g_hWndStatus, SW_SHOW);
        UpdateWindow(g_hWndStatus);
        UpdateWindow(g_hWnd);
    }
    
    if(SUCCEEDED(hr) && INVALID_HANDLE_VALUE != hFile)
    {        
        char inBuf[5000];  
        DWORD written;
        ULONG cbJustRead;            
        do
        {
            if(!ReadFile(hFile, 
                inBuf, 
                sizeof(inBuf), &cbJustRead, 0))
            {
                hr = E_FAIL;
                break;
            }
            
            hr = myStream->Write(inBuf, cbJustRead, &written);            

            dwBytesSent += written;
            SendMessage(g_hWndStatus, WM_DO_UPDATE, 0, 0);
            
        } while(SUCCEEDED(hr) && cbJustRead == sizeof(inBuf) && FALSE == g_fAbort);
        
        //
        // Handle an ABORT
        if(g_fAbort)  {
            IHeaderCollection *pAbortHeaderCollection = NULL;
            if(SUCCEEDED(CoCreateInstance(CLSID_HeaderCollection, 
                NULL, 
                CLSCTX_INPROC_SERVER, 
                IID_IHeaderCollection,  
                (void **)&pAbortHeaderCollection)) && NULL != pAbortHeaderCollection) 
           {                    
                    g_pObexDevice->Abort(pAbortHeaderCollection);                       
                    pAbortHeaderCollection->Release();
           }  
           MessageBox(g_hWndStatus, L"Transfer aborted", L"Finished", MB_OK);           
           g_fAbort = FALSE;
        }
    
        if(myStream) {
            //
            // Commit the stream so we can catch errors that may happen on the 
            //   final packets (but only if all the Writes() have succeeded)
			if(SUCCEEDED(hr))
				hr = myStream->Commit(0); 
            myStream->Release();
        }    
        
        if(FAILED(hr)) {
            MessageBox(g_hWndStatus, L"Write Failed -- could the server be in read-only mode?", L"Error", MB_OK);
        }
        myStream = 0;
        hr = S_OK;
    }
    
    //cleanup
    if(INVALID_HANDLE_VALUE != hFile)
        CloseHandle(hFile);  
    
    if(myStream)
        myStream->Release();
    if(pHeaderCollection)
        pHeaderCollection->Release();
    
    //load the status window
    ShowWindow(g_hWndStatus, SW_HIDE);
    UpdateWindow(g_hWndStatus);   

    //unset global transmission lock
    EnterCriticalSection(&g_csLock);
    ASSERT(g_fTransferInProgress);
    g_fTransferInProgress = FALSE;         
    LeaveCriticalSection(&g_csLock);
    return hr;
}


LRESULT CALLBACK StatusProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    static BOOL fRepaintAll; 
    
    switch(message)
    {
    case WM_COMMAND:
        if(WM_ABORT == LOWORD(wParam))
            g_fAbort = TRUE;
        break; 
    case WM_DO_UPDATE:  
        fRepaintAll = FALSE;
        InvalidateRect(hWnd, 0, 0);
        UpdateWindow(hWnd);
        break;
        
    case WM_CREATE:
        fRepaintAll = TRUE;
        return 0;
    case WM_DESTROY:
        return 0;
    case WM_PAINT:
        {            
            RECT rcWindBounds; 
            RECT rcMyBounds;
            PAINTSTRUCT ps;
            HDC hDC = BeginPaint(hWnd, &ps);
            GetWindowRect( hWnd, &rcWindBounds);  
            
            if(fRepaintAll)
            {
                rcMyBounds.left = 0;
                rcMyBounds.top = 0;
                rcMyBounds.right = rcWindBounds.right - rcWindBounds.left;
                rcMyBounds.bottom = rcWindBounds.bottom - rcWindBounds.top;
                FillRect(hDC,&rcMyBounds, GetSysColorBrush(COLOR_WINDOWTEXT)); 

⌨️ 快捷键说明

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