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

📄 filebrowser.cpp

📁 这个是微软WINCE的OBEX协议例子
💻 CPP
📖 第 1 页 / 共 5 页
字号:
            //loop through the headers looking for info                      
            if(SUCCEEDED(hr) && ID_CONNECT_FB == LOWORD(wParam))
            {
                hr = pHeaderCollection->EnumHeaders(&pHeaderEnum);
                
                if(SUCCEEDED(hr))
                {
                    do 
                    {
                        hr = pHeaderEnum->Next(1, &obHeader, &ulHeadersFetched);
                        
                        if(SUCCEEDED(hr))
                        {
                            if(obHeader->bId == OBEX_HID_CONNECTIONID)
                            {
                                g_fServerBrowsable = TRUE;
                            }
                        }
                    } while(SUCCEEDED(hr) && ulHeadersFetched);
                    pHeaderEnum->Release();
                    hr = S_OK;
                }
            }                
            
            SetCursor(hCurOld);    
            
            if(FAILED(hr))
            {
                if(hr == OBEX_E_CONNECTION_NOT_ACCEPTED)
                {
                    MessageBox(g_hWnd, L"Server rejected connection (try setting a password or if you are using one, turn it off)", L"Error", MB_OK);
                }
                else
                {
                    MessageBox(g_hWnd, L"Error connecting", L"Error", MB_OK);
                }
                
                g_pObexDevice->Release();
                g_pObexDevice=0;
                g_fConnectedTo = CONNECTED_NOTHING;
            }
            
            else
            {
                if(ID_CONNECT_FB == LOWORD(wParam))
                    g_fConnectedTo = CONNECTED_FILEBROWSER;
                else
                    g_fConnectedTo = CONNECTED_INBOX;

                if(g_fServerBrowsable)
                {   
                    g_SelectionDisabled = TRUE;
                    RefreshDirectory(szRequestDir, g_htiParentNode);
                    g_SelectionDisabled = FALSE;
                }                        
            }
            
            if(pHeaderCollection)
                pHeaderCollection->Release();                                              
        }    
        else
        {
            MessageBox(g_hWnd, L"Error connecting", L"Error", MB_OK);
        }
    }
    else
        MessageBox(g_hWnd, L"Cant, already connected", L"Error", MB_OK);
    return 0;
}

/*****************************************************************************/
/*
/* Function:  MainProc
/* Purpose:   To handle all messages that are generated by the 
/*              main window
/*
/*****************************************************************************/
LRESULT CALLBACK MainProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)
    {
    case WM_CREATE:    
        {
            if (! g_hAygShellDll) {
                HWND hCmdBar = CommandBar_Create (g_hInst, hWnd, 1);
                CommandBar_InsertMenubar (hCmdBar, g_hInst, IDR_MENUBAR, 0);
                
                g_hMenu = CommandBar_GetMenu (hCmdBar, 1);
                g_iMenuHeight = CommandBar_Height (hCmdBar);
            }
            
            g_hTreeWnd = CreateATreeView(g_hInst, hWnd);
            
            //put a menu into the command bar
            if (g_hAygShellDll)
                g_hwndCB = CreateRpCommandBar(hWnd);
            
            return 0;
        }
        
        
    case WM_NOTIFY:
                         
        switch (((LPNMHDR) lParam)->code) {          
        case TVN_SELCHANGED:
            {
                if(!g_SelectionDisabled && g_fServerBrowsable)
                {
                    DIR_NODE *dNode = (DIR_NODE *)(((LPNMTREEVIEW)lParam)->itemNew.lParam); 
                    if(dNode && dNode->fDir && !dNode->fTraversed)
                    {
                        RefreshDirectory(dNode->pPath, dNode->htiParent);
                        dNode->fTraversed = true;
                    } 
                    g_SelectedNode = dNode;
                }
            }
            break;
        }
        break;
        
        
        case WM_COMMAND:
            EnterCriticalSection(&g_csLock);
            if(g_fTransferInProgress) {            
                MessageBox(hWnd, L"A transmission is already in progress.", L"Status", MB_OK);
                LeaveCriticalSection(&g_csLock);
                return 0;
            }
            LeaveCriticalSection(&g_csLock);
            
            switch(LOWORD(wParam))
            {
            case ID_CONNECT_REFRESH:               
                return RefreshConnection();
                
            case ID_CONFIG_SERVER:
                return ConfigureServer();

            case ID_CONFIG_CLIENT:
                return ConfigureClient();        
            
            case ID_GET_FILE:
            {
                 if(g_pObexDevice) {
                    CreateThread(NULL, NULL,GetFile, NULL, 0,NULL);  
                    return 0;
                }    
                break;
            }
            case ID_PUT_FILE:
                if(g_pObexDevice) {
                    CreateThread(NULL, NULL,PutFile, NULL, 0,NULL);  
                    return 0;
                }
                break;  
             
            case ID_DELETE_NODE:
                if(g_pObexDevice)
                    return DeleteNode();
                break;
                
            case ID_CREATE_DIRECTORY:
                if(g_pObexDevice)
                    return CreateDirectory();
                break;
                
            case ID_GET_VCARD: 
                if(g_pObexDevice)
                    return GetVCard();
                break;
                
            case ID_CONNECT_EXIT:                
                return ShutDownAndExit();                
                
                
            case ID_CONNECT_DISCONNECT: 
                if(g_pObexDevice)
                    return Disconnect();
                break;
                
            case ID_CONNECT_FB: //<--fall through
            case ID_CONNECT_INBOX:
                return Connect(wParam);
            }
            return 0;
            
            
            
    case WM_SIZE: 
        {
            DWORD width = LOWORD(lParam);
            DWORD height = HIWORD(lParam);
            DWORD top = g_hAygShellDll ? 0 : ((height -= MENU_HEIGHT), MENU_HEIGHT-1);
            
            //align the tree window
            MoveWindow(g_hTreeWnd,            
                0,
                top,
                width,
                height,
                true); 
            
            break;
        }
        
        
    case WM_DESTROY:    
        //post the quit message
        PostQuitMessage(0);
        
        return 0;
    }
    return DefWindowProc(hWnd, message, wParam, lParam);
}



HRESULT FetchFile(LPTSTR file, LPTSTR path)
{
    ASSERT(g_pObexDevice && file && path);
    if(FAILED(g_pObexDevice->SetPath(path, SETPATH_FLAG_DONT_CREATE, 0)))
    {
        return E_FAIL;
    }
    
    
    //get a header collection
    IHeaderCollection *pHeaderCollection;
    HRESULT hr = CoCreateInstance(CLSID_HeaderCollection, 
        NULL, 
        CLSCTX_INPROC_SERVER, 
        IID_IHeaderCollection,  
        (void **)&pHeaderCollection);
    
    if(FAILED(hr)) 
        return E_FAIL;
    
    //put in the name
    pHeaderCollection->AddName(file);
    
    
    IStream *myStream;   
    hr = g_pObexDevice->Get(pHeaderCollection, &myStream); 
    
    //set global transmission lock
    EnterCriticalSection(&g_csLock);
    ASSERT(!g_fTransferInProgress);
    g_fTransferInProgress = TRUE;         
    LeaveCriticalSection(&g_csLock);
    
    if(SUCCEEDED(hr))
    {
        WCHAR saveFile[MAX_PATH];
        swprintf(saveFile, L"%s\\%s", szStorageDir, file);
        //create a file with this name
        HANDLE hFile = CreateFile (saveFile, 
            GENERIC_WRITE, 0, NULL, 
            CREATE_ALWAYS, 0, NULL);
        
        dwBytesSent = 0;
        
        //load the status window
        ShowWindow(g_hWndStatus, SW_SHOW);
        UpdateWindow(g_hWndStatus);
        UpdateWindow(g_hWnd);
        
        if(INVALID_HANDLE_VALUE != hFile)
        {            
            char *pBuffer = 0;
            UINT pDirSize = 0;
            char inBuf[3000];  
            do
            {
                ULONG cbJustRead;
                hr = myStream->Read(inBuf, sizeof(inBuf), &cbJustRead);
                
                if(SUCCEEDED(hr))
                {
                    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);

            CloseHandle(hFile);

            //
            // 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();
               }     
                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);
                }
                CloseHandle(hFile);
            }
            
            if(dwBytesSent)
                hr = S_OK;
            else
                hr = E_FAIL;
            
            
        }
        else
        {
            MessageBox(NULL, (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();
    
    //set global transmission lock
    EnterCriticalSection(&g_csLock);
    ASSERT(g_fTransferInProgress);
    g_fTransferInProgress = FALSE;         
    LeaveCriticalSection(&g_csLock);
    return hr;
}


HRESULT FetchFileByMimeType(char *mimeType, WCHAR *destFileName)
{
    if(!g_pObexDevice || !mimeType || !destFileName)
    {
        ASSERT(FALSE);
        return E_FAIL;
    }
    
    //get a header collection
    IHeaderCollection *pHeaderCollection;
    HRESULT hr = CoCreateInstance(CLSID_HeaderCollection, 
        NULL, 
        CLSCTX_INPROC_SERVER, 
        IID_IHeaderCollection,  
        (void **)&pHeaderCollection);
    
    if(FAILED(hr)) 
        return E_FAIL;
    
    //put in the name
    pHeaderCollection->AddType(strlen(mimeType) + 1, (UCHAR *)mimeType);
    
    IStream *myStream;   
    hr = g_pObexDevice->Get(pHeaderCollection, &myStream); 
    
    if(SUCCEEDED(hr))
    {
        WCHAR saveFile[MAX_PATH];
        swprintf(saveFile, L"%s\\%s", szStorageDir, destFileName);
        //create a file with this name
        HANDLE hFile = CreateFile (saveFile, 
            GENERIC_WRITE, 0, NULL, 
            CREATE_ALWAYS, 0, NULL);
        
        dwBytesSent = 0;
        
        //load the status window
        ShowWindow(g_hWndStatus, SW_SHOW);
        UpdateWindow(g_hWndStatus);
        UpdateWindow(g_hWnd);
        
        if(INVALID_HANDLE_VALUE != hFile)
        {            
            char *pBuffer = 0;
            UINT pDirSize = 0;
            char inBuf[3000];           
            do
            {
                ULONG cbJustRead;
                hr = myStream->Read(inBuf, sizeof(inBuf), &cbJustRead);
                
                if(SUCCEEDED(hr))

⌨️ 快捷键说明

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