2013-10-05 45 views
0

我試圖通過客戶端 - 服務器應用程序發送手柄代碼。使用DirectInput API成功顯示手柄數據後,我無法弄清楚手柄中的數據存儲在哪些變量中,以便將它們傳遞到緩衝區併發送。任何幫助,將不勝感激。C++ DirectInput示例從手柄中檢索即時數據

的代碼塊,其顯示了手柄數據:

//-------------------------------------------------------------------------------------------------- 
// Name: UpdateInputState() 
// Desc: Get the input device's state and display it. 
//-------------------------------------------------------------------------------------------------- 
HRESULT UpdateInputState(HWND hDlg) 
{ 
    HRESULT hr; 
    TCHAR strText[512] = {0}; // Device state text. 
    DIJOYSTATE2 js; // DirectInput joypad state. 

    if(NULL == g_pJoypad) 
     return S_OK; 

    // Poll the device to read the current state. 
    hr = g_pJoypad->Poll(); 
    if(FAILED(hr)) 
    { 
     // DirectInput is telling us that the input stream has been interrupted. We aren't tracking 
     // any state between polls, so we don't have any special reset that needs to be done. We just 
     // re-acquire and try again. 
     hr = g_pJoypad->Acquire(); 
     while(hr == DIERR_INPUTLOST) 
      hr = g_pJoypad->Acquire(); 

     // hr may be DIERR_OTHERAPPHASPRIO or other errors. This may occur when the application is 
     // minimized or in the process of switching, so just try again later. 
     return S_OK; 
    } 

    // Get the input's device state. 
    // GetDeviceState() retrieves immediate data from the device. 
    if(FAILED(hr = g_pJoypad->GetDeviceState(sizeof(DIJOYSTATE2), &js))) 
     return hr; // The device should have been acquired during the Poll(). 

    // Display joypad state to dialog. 

    // Axes. 
    _stprintf_s(strText, 512, TEXT("%ld"), js.lX); 
    SetWindowText(GetDlgItem(hDlg, IDC_X_AXIS_L), strText); 
    _stprintf_s(strText, 512, TEXT("%ld"), js.lY); 
    SetWindowText(GetDlgItem(hDlg, IDC_Y_AXIS_L), strText); 
    _stprintf_s(strText, 512, TEXT("%ld"), js.lZ); 
    SetWindowText(GetDlgItem(hDlg, IDC_X_AXIS_R), strText); 
    _stprintf_s(strText, 512, TEXT("%ld"), js.lRx); 
    _stprintf_s(strText, 512, TEXT("%ld"), js.lRz); 
    SetWindowText(GetDlgItem(hDlg, IDC_Y_AXIS_R), strText); 

    // POV. 
    _stprintf_s(strText, 512, TEXT("%lu"), js.rgdwPOV[0]); 
    SetWindowText(GetDlgItem(hDlg, IDC_POV), strText); 

    // Buttons. 
    // Fill up text with which buttons are pressed. 
    _tcscpy_s(strText, 512, TEXT("")); 
    for(int i = 0; i < 128; i++) 
    { 
     if(js.rgbButtons[i] & 0x80) 
     { 
      TCHAR sz[128]; 
      _stprintf_s(sz, 128, TEXT("%02d"), i); 
      _tcscat_s(strText, 512, sz); 
     } 
    } 
    SetWindowText(GetDlgItem(hDlg, IDC_BUTTONS), strText); 

    return S_OK; 
} 

的代碼塊,所述數據發送到服務器:

// Send a message. 
    if (send(ConnectSocket, sendbuf, sizeof(sendbuf), 0) == SOCKET_ERROR) 
    { 
     MessageBox(NULL, TEXT("The joypad data could not be sent."), TEXT("Client says"), 
          MB_ICONERROR | MB_OK); 
    } 

回答

0

比我想簡單。數據存儲在strText(軸和POV)和sz(按鈕)中。但是,這兩個緩衝區都被聲明爲TCHAR,並且需要將轉換爲CHAR才能作爲參數傳遞給send()(代替sendbuf)。