📄 timeviewer.cpp
字号:
}
//------------------------------------------------------------------
// Popup a dialog to allow the user to select the device
//------------------------------------------------------------------
hr = piMgrUI->GetDevice(hWnd, g_piMgr, &piPlatform, &piDevice);
if (FAILED(hr) || !piDevice)
{
goto Exit;
}
lstrcpy(g_szMessage, "Attempting to connect to the device");
::InvalidateRect(hWnd, NULL, TRUE);
//------------------------------------------------------------------
// Connect to the device
//------------------------------------------------------------------
hr = piMgrUI->Connect(hWnd, piDevice, &piConnection);
if (FAILED(hr) || !piConnection)
{
MessageBox(hWnd,
"Unable to connect",
"Connect To Device",
MB_OK);
goto Exit;
}
//------------------------------------------------------------------
// Queue up the the package
//------------------------------------------------------------------
hr = piConnection->QueuePackage(TimeViewerID, L"\\Windows", FALSE);
if (FAILED(hr))
{
MessageBox(hWnd,
"Unable to Queue Package",
"Connect To Device",
MB_OK);
goto Exit;
}
//------------------------------------------------------------------
// Copy the package file(s) to the device
//------------------------------------------------------------------
hr = piConnection->CopyQueuedFiles(NULL, &dwSize);
if (FAILED(hr) && hr != STG_E_FILEALREADYEXISTS)
{
MessageBox(hWnd,
"Unable to copy TimeViewerCE.exe to device",
"Connect To Device",
MB_OK);
goto Exit;
}
//------------------------------------------------------------------
// Update the Window Title to show the connection
//------------------------------------------------------------------
LPOLESTR pszName;
char szDeviceName[80];
piPlatform->GetPlatformName(&pszName);
stRet = wcstombs(szDeviceName, pszName, sizeof szDeviceName);
char szTitle[128];
strcpy(szTitle, "TimeViewer : ");
strcat(szTitle, szDeviceName);
SetWindowText(hWnd, szTitle);
//------------------------------------------------------------------
// Start up the device side executable
//------------------------------------------------------------------
WCHAR wszDeviceExe[MAX_PATH];
wcscpy(wszDeviceExe, L"\\Windows\\TimeViewerCE.exe");
hr = piConnection->Launch(wszDeviceExe, NULL);
if (FAILED(hr))
{
MessageBox(hWnd,
"Unable to launch device EXE",
"Connect To Device",
MB_OK);
goto Exit;
}
//------------------------------------------------------------------
// Create the stream (Synchronous)
//------------------------------------------------------------------
g_piStream = NULL;
hr = piConnection->CreateStream(TimeViewerID,
0,
&g_piStream,
NULL);
if (FAILED(hr))
{
MessageBox(hWnd,
"Unable to create a stream",
"Connect",
MB_OK);
goto Exit;
}
//------------------------------------------------------------------
// Pop up a dialog to inform the user that we are connected
//------------------------------------------------------------------
wsprintf(szText, "We are connected to %s", szDeviceName);
::MessageBox(hWnd, szText, "Connect To Device", MB_OK | MB_ICONEXCLAMATION);
bConnected = TRUE;
Exit:
//------------------------------------------------------------------
// Cleanup
//------------------------------------------------------------------
if (piConnection)
piConnection->Release();
if (piDevice)
piDevice->Release();
if (piPlatform)
piPlatform->Release();
if (piMgrUI)
piMgrUI->Release();
// Update the menu items
UpdateMenu(hWnd, bConnected);
return bConnected;
}
//**********************************************************************
HRESULT Refresh(HWND hWnd)
//**********************************************************************
{
DWORD dwCmd = CMD_NONE;
DWORD dwSizeSent = 0;
HRESULT hr = E_FAIL;
SYSTEMTIME systemTime;
if (!g_piStream)
{
MessageBox(hWnd, "Not Connected", "Refresh", MB_OK);
return hr;
}
lstrcpy(g_szMessage, "Error trying to read data");
//------------------------------------------------------------------
// Send a command to get the device system time
//------------------------------------------------------------------
dwCmd = CMD_GET_DATA;
dwSizeSent = sizeof(DWORD);
hr = g_piStream->Send(sizeof(DWORD), (BYTE *)&dwCmd, &dwSizeSent);
if (FAILED(hr))
{
char szText[80];
wsprintf(szText, "Error occurred when trying to send the get command: 0x%x", hr);
MessageBox(hWnd, szText, "Refresh", MB_OK);
return hr;
}
//------------------------------------------------------------------
// Wait for a response
//------------------------------------------------------------------
dwSizeSent = sizeof(SYSTEMTIME);
hr = g_piStream->Receive(&dwSizeSent); // Find out how many bytes
// are being sent
if (SUCCEEDED(hr))
{
// Get the data from ITL
hr = g_piStream->ReadBytes(sizeof(SYSTEMTIME),
(BYTE *)&systemTime,
&dwSizeSent);
if (SUCCEEDED(hr))
{
// Format the time for display
wsprintf(g_szMessage, "The CE time is %2d:%2.2d:%2.2d",
systemTime.wHour, systemTime.wMinute, systemTime.wSecond);
::InvalidateRect(hWnd, NULL, TRUE);
}
}
else
{
char szText[80];
wsprintf(szText, "Unable to obtain data from the device: hr = 0x%x", hr);
::MessageBox(hWnd, szText, "Refresh", MB_OK);
}
return hr;
}
//**********************************************************************
void DisconnectFromDevice(HWND hWnd)
//**********************************************************************
{
if (g_piStream) // We have an open connection
{
//--------------------------------------------------------------
// Tell the device that we're done
// NOTE: Don't wait for a response
//--------------------------------------------------------------
DWORD dwCmd = CMD_FINISHED;
DWORD dwSizeSent = 0;
g_piStream->Send(sizeof(DWORD), (BYTE *)&dwCmd, &dwSizeSent);
//--------------------------------------------------------------
// Close the stream
//--------------------------------------------------------------
g_piStream->Close();
g_piStream->Release();
g_piStream = NULL;
if (hWnd)
{
lstrcpy(g_szMessage, "Disconnected!");
::InvalidateRect(hWnd, NULL, TRUE);
UpdateMenu(hWnd, FALSE);
}
}
else
{
::MessageBox(hWnd,
"Not connected to device",
"Disconnect",
MB_OK | MB_ICONEXCLAMATION);
}
if (g_piMgr) // Platform Manager exists
{
g_piMgr->Release();
g_piMgr = NULL;
}
return;
}
//**********************************************************************
void ConfigurePlatMan(HWND hWnd)
//**********************************************************************
{
IPlatformManagerUI *piMgrUI;
//------------------------------------------------------------------
// Instantiate PlatformManagerUI
//------------------------------------------------------------------
HRESULT hr = CoCreateInstance(CLSID_PlatformManagerUI,
NULL,
CLSCTX_INPROC_SERVER,
IID_IPlatformManagerUI,
(void**)&piMgrUI) ;
if (SUCCEEDED(hr))
{
// Use the built-in configuration dialogs
piMgrUI->Configure(hWnd);
piMgrUI->Release();
}
return;
}
//**********************************************************************
void UpdateMenu(HWND hWnd, BOOL bConnected)
//**********************************************************************
{
HMENU hMenu = GetMenu(hWnd);
if (bConnected)
{
EnableMenuItem(hMenu, ID_CONNECTION_CONNECTTODEVICE, MF_GRAYED);
EnableMenuItem(hMenu, ID_CONNECTION_REFRESH, MF_ENABLED);
EnableMenuItem(hMenu, ID_CONNECTION_DISCONNECTFROMDEVICE, MF_ENABLED);
}
else
{
EnableMenuItem(hMenu, ID_CONNECTION_CONNECTTODEVICE, MF_ENABLED);
EnableMenuItem(hMenu, ID_CONNECTION_REFRESH, MF_GRAYED);
EnableMenuItem(hMenu, ID_CONNECTION_DISCONNECTFROMDEVICE, MF_GRAYED);
}
return;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -