// WinClientDlg.cpp : implementation file // #include "stdafx.h" #include "WinClient.h" #include "WinClientDlg.h" #include "..\OleCharConv.h" // Wide string support #include "..\tribbleinterface.h" // The tribble's CLSID #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CWinClientDlg dialog CWinClientDlg::CWinClientDlg(CWnd* pParent /*=NULL*/) : CDialog(CWinClientDlg::IDD, pParent) { //{{AFX_DATA_INIT(CWinClientDlg) //}}AFX_DATA_INIT // Note that LoadIcon does not require a subsequent DestroyIcon in Win32 m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); } void CWinClientDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CWinClientDlg) DDX_Control(pDX, IDC_TEXT3, m_Text3); DDX_Control(pDX, IDC_ITEMNAME, m_ItemName); DDX_Control(pDX, IDC_RAWMONIKER, m_RawMoniker); DDX_Control(pDX, IDC_TEXT2, m_Text2); DDX_Control(pDX, IDC_FILENAME2, m_FileName2); DDX_Control(pDX, IDC_TEXT1, m_Text1); DDX_Control(pDX, IDC_FILENAME, m_FileName); //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CWinClientDlg, CDialog) //{{AFX_MSG_MAP(CWinClientDlg) ON_WM_PAINT() ON_WM_QUERYDRAGICON() ON_WM_CLOSE() ON_BN_CLICKED(IDC_FILEBIND, OnFileBind) ON_BN_CLICKED(IDC_CMPBIND, OnCmpBind) ON_BN_CLICKED(IDC_RAWBIND, OnRawBind) //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CWinClientDlg message handlers BOOL CWinClientDlg::OnInitDialog() { CDialog::OnInitDialog(); // Set the icon for this dialog. The framework does this automatically // when the application's main window is not a dialog SetIcon(m_hIcon, TRUE); // Set big icon SetIcon(m_hIcon, FALSE); // Set small icon // Enter out appartment HRESULT hr = CoInitialize(0); if(FAILED(hr)) ErrorHelper(hr,"CoInitialize Failure"); m_FileName.SetFocus(); return FALSE; // return TRUE unless you set the focus to a control } // If you add a minimize button to your dialog, you will need the code below // to draw the icon. For MFC applications using the document/view model, // this is automatically done for you by the framework. void CWinClientDlg::OnPaint() { if (IsIconic()) { CPaintDC dc(this); // device context for painting SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0); // Center icon in client rectangle int cxIcon = GetSystemMetrics(SM_CXICON); int cyIcon = GetSystemMetrics(SM_CYICON); CRect rect; GetClientRect(&rect); int x = (rect.Width() - cxIcon + 1) / 2; int y = (rect.Height() - cyIcon + 1) / 2; // Draw the icon dc.DrawIcon(x, y, m_hIcon); } else { CDialog::OnPaint(); } } // The system calls this to obtain the cursor to display while the user drags // the minimized window. HCURSOR CWinClientDlg::OnQueryDragIcon() { return (HCURSOR) m_hIcon; } //--------------------------------------------------------------------------- // Method: ErrorHelper // Purpose: Displays a piece of text and hresult //--------------------------------------------------------------------------- void ErrorHelper(HRESULT hr,const char * lpszText) { CString strMsg; strMsg.Format("%s hr = %lx\n",lpszText,hr); OutputDebugString(strMsg); } void CWinClientDlg::OnClose() { // Exit out appartment CoUninitialize(); CDialog::OnClose(); } //--------------------------------------------------------------------------- // Method: CWinClientDlg::OnFileBind // Purpose: Demonstrates the use of a File moniker and BindToObject //--------------------------------------------------------------------------- void CWinClientDlg::OnFileBind() { // We're going to get hold of a Tribble ITribble * pITribble = NULL; IMoniker * pFileMoniker = NULL; // Use this Wrapper API to get a file moniker CString strTmp; m_FileName.GetWindowText(strTmp); COleCharConv fn(strTmp); HRESULT hr = CreateFileMoniker(fn,&pFileMoniker); if(FAILED(hr)) { ErrorHelper(hr,"Failed to get hold of a file Moniker"); goto TheExit; } // Now get the tribble using another Wrapper hr = BindMoniker(pFileMoniker,0,IID_ITribble,(void **)&pITribble); if(FAILED(hr)) { ErrorHelper(hr,"Failed to BindToObject"); // The moniker is no longer needed pFileMoniker->Release(); goto TheExit; // Oh-No a goto - this is only demo code! } // The moniker is no longer needed pFileMoniker->Release(); // Now use the tribble char Name[128]; // scratch memory char Colour[128]; // scratch memory strcpy(Name,""); strcpy(Colour,""); pITribble->get_Name((unsigned char *)&Name); pITribble->get_Colour((unsigned char *)&Colour); strTmp.Format("Hi, I'm %s - a %s tribble\n",Name,Colour); m_Text1.SetWindowText(strTmp); // The tribble is nolonger needed pITribble->Release(); TheExit: return; } // CWinClientDlg::OnFileBind //--------------------------------------------------------------------------- // Method: CWinClientDlg::OnCmpBind( // Purpose: This method instanciates a tribble // using a file!item moniker. // The file is read but the item // overrides the tribbles // original colour. //--------------------------------------------------------------------------- void CWinClientDlg::OnCmpBind() { // We're going to get hold of a Tribble ITribble * pITribble = NULL; IMoniker * pCmpMoniker = NULL; IMoniker * pFileMoniker = NULL; IMoniker * pItemMoniker = NULL; // make wide strings CString strTmp; m_FileName2.GetWindowText(strTmp); COleCharConv fn(strTmp); //convert the file name m_ItemName.GetWindowText(strTmp); COleCharConv item(strTmp); //convert the item // Step 1: Use this Wrapper API to get a file moniker HRESULT hr = CreateFileMoniker(fn,&pFileMoniker); if(FAILED(hr)) { ErrorHelper(hr,"Failed to get hold of a file moniker\n"); goto TheExit; // Oh-No a goto - this is only demo code! } // Step 2: Use this Wrapper to create an Item Moniker hr = CreateItemMoniker(OLESTR("!"),item,&pItemMoniker); if(FAILED(hr)) { ErrorHelper(hr,"Failed to get hold of an item moniker"); pFileMoniker->Release(); goto TheExit; // Oh-No a goto - this is only demo code! } // Step3 - Combine the two into a composite hr = pFileMoniker->ComposeWith(pItemMoniker, FALSE,&pCmpMoniker); if(FAILED(hr)) { ErrorHelper(hr,"Failed to get hold of a composite moniker"); pFileMoniker->Release(); pItemMoniker->Release(); goto TheExit; // Oh-No a goto - this is only demo code! } // Now get the tribble using another Wrapper hr = BindMoniker(pCmpMoniker,0,IID_ITribble,(void **)&pITribble); // The moniker is no longer needed pFileMoniker->Release(); pItemMoniker->Release(); pCmpMoniker->Release(); if(FAILED(hr)) { ErrorHelper(hr,"Failed to BindToObject"); goto TheExit; // Oh-No a goto - this is only demo code! } // Now use the tribble char Name[128]; // scratch memory char Colour[128]; // scratch memory strcpy(Name,""); strcpy(Colour,""); pITribble->get_Name((unsigned char *)&Name); pITribble->get_Colour((unsigned char *)&Colour); strTmp.Format("Hi, I'm %s - a %s tribble\n",Name,Colour); m_Text2.SetWindowText(strTmp); // The tribble is nolonger needed pITribble->Release(); TheExit: return; } // CWinClientDlg::OnCmpBind( //--------------------------------------------------------------------------- // Method: CWinClientDlg::OnRawBind // Purpose: Shows the power of monikers. Type in any type of moniker // complete with ProgIDs and, if your object supports it, watch it bind // Try typing a filename - for example: c:\temp\test.trb // Try typing a composit //--------------------------------------------------------------------------- void CWinClientDlg::OnRawBind() { // make wide strings CString strTmp; m_RawMoniker.GetWindowText(strTmp); COleCharConv fn(strTmp); // Convert the Moniker ITribble * pITribble = NULL; HRESULT hr = CoGetObject(fn, // Text string for the object we want (The display Name) 0, // Bind Options IID_ITribble, // the interface needed reinterpret_cast(&pITribble)); if(FAILED(hr)) { ErrorHelper(hr,"Failed to BindToObject"); goto TheExit; // Oh-No a goto - this is only demo code! } // Now use the tribble char Name[128]; // scratch memory char Colour[128]; // scratch memory strcpy(Name,""); strcpy(Colour,""); pITribble->get_Name((unsigned char *)&Name); pITribble->get_Colour((unsigned char *)&Colour); strTmp.Format("Hi, I'm %s - a %s tribble\n",Name,Colour); m_Text3.SetWindowText(strTmp); // The tribble is nolonger needed pITribble->Release(); TheExit: return; }