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

📄 myvoipctl.cpp

📁 这是书上的代码
💻 CPP
📖 第 1 页 / 共 3 页
字号:


// MyvoipCtl.cpp : Implementation of the CMyvoipCtrl ActiveX Control class.

#include "stdafx.h"
#include "myvoip.h"
#include "MyvoipCtl.h"
#include "MyvoipPpg.h"
#include <windows.h>
#include <windowsx.h>
#include <basetsd.h>
#include <ddraw.h>
#include <dinput.h>
#include <dmusici.h>

typedef HRESULT(WINAPI * DIRECTDRAWCREATE)( GUID*, LPDIRECTDRAW*, IUnknown* );
typedef HRESULT(WINAPI * DIRECTDRAWCREATEEX)( GUID*, VOID**, REFIID, IUnknown* );
typedef HRESULT(WINAPI * DIRECTINPUTCREATE)( HINSTANCE, DWORD, LPDIRECTINPUT*,
                                             IUnknown* );

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

BOOL bDone;
HWND hWnd;
LPDIRECTSOUND              g_pDS;
LPDIRECTSOUNDCAPTURE       g_pDSCapture ;
LPDIRECTSOUNDBUFFER        g_pDSBPrimary;
LPDIRECTSOUNDBUFFER        g_pDSBOutput;
LPDIRECTSOUNDCAPTUREBUFFER g_pDSBCapture;
LPDIRECTSOUNDNOTIFY        g_pDSNotify ;
DSBPOSITIONNOTIFY    g_aPosNotify[ NUM_PLAY_NOTIFICATIONS ];  
HANDLE               g_hNotificationEvent; 
BOOL                 g_abOutputFormatSupported;
BOOL                 g_abInputFormatSupported;
DWORD                g_dwOutputBufferSize;
DWORD                g_dwCaptureBufferSize;
DWORD                g_dwNextOutputOffset;
DWORD                g_dwNextCaptureOffset;
DWORD                g_dwNotifySize;
WAVEFORMATEX         g_wfxCaptureWaveFormat;
BOOL                 g_bRecording;
CWaveFile*                  g_pWaveFile;
VOID* pDSCaptureLockedBuffer    = NULL;
DWORD dwDSCaptureLockedBufferSize;
CMySock Sock;
CString destIP;
//CMySock Sock;
//CString destIP;
CString destID;


class CMyvoipCtrl;

IMPLEMENT_DYNCREATE(CMyvoipCtrl, COleControl)


/////////////////////////////////////////////////////////////////////////////
// Message map

BEGIN_MESSAGE_MAP(CMyvoipCtrl, COleControl)
	//{{AFX_MSG_MAP(CMyvoipCtrl)
	//}}AFX_MSG_MAP
	ON_MESSAGE(UM_RECEIVERTP,RecMsg)

	ON_OLEVERB(AFX_IDS_VERB_PROPERTIES, OnProperties)
END_MESSAGE_MAP()


/////////////////////////////////////////////////////////////////////////////
// Dispatch map

BEGIN_DISPATCH_MAP(CMyvoipCtrl, COleControl)
	//{{AFX_DISPATCH_MAP(CMyvoipCtrl)
	DISP_FUNCTION(CMyvoipCtrl, "Record", Record, VT_EMPTY, VTS_BSTR VTS_BSTR)
	DISP_FUNCTION(CMyvoipCtrl, "Stop", Stop, VT_EMPTY, VTS_NONE)
	//}}AFX_DISPATCH_MAP
	DISP_FUNCTION_ID(CMyvoipCtrl, "AboutBox", DISPID_ABOUTBOX, AboutBox, VT_EMPTY, VTS_NONE)
END_DISPATCH_MAP()


/////////////////////////////////////////////////////////////////////////////
// Event map

BEGIN_EVENT_MAP(CMyvoipCtrl, COleControl)
	//{{AFX_EVENT_MAP(CMyvoipCtrl)
	// NOTE - ClassWizard will add and remove event map entries
	//    DO NOT EDIT what you see in these blocks of generated code !
	//}}AFX_EVENT_MAP
END_EVENT_MAP()


/////////////////////////////////////////////////////////////////////////////
// Property pages

// TODO: Add more property pages as needed.  Remember to increase the count!
BEGIN_PROPPAGEIDS(CMyvoipCtrl, 1)
	PROPPAGEID(CMyvoipPropPage::guid)
END_PROPPAGEIDS(CMyvoipCtrl)


/////////////////////////////////////////////////////////////////////////////
// Initialize class factory and guid

IMPLEMENT_OLECREATE_EX(CMyvoipCtrl, "MYVOIP.MyvoipCtrl.1",
	0xaec15949, 0x3c64, 0x4f55, 0xa2, 0x20, 0x79, 0x65, 0x3, 0x4c, 0x39, 0x86)


/////////////////////////////////////////////////////////////////////////////
// Type library ID and version

IMPLEMENT_OLETYPELIB(CMyvoipCtrl, _tlid, _wVerMajor, _wVerMinor)


/////////////////////////////////////////////////////////////////////////////
// Interface IDs

const IID BASED_CODE IID_DMyvoip =
		{ 0xd7d58478, 0x5b21, 0x4a60, { 0xa5, 0xaa, 0xc2, 0xfa, 0xa, 0x37, 0xaf, 0xe5 } };
const IID BASED_CODE IID_DMyvoipEvents =
		{ 0x934d56aa, 0x188d, 0x4b27, { 0xba, 0xbc, 0xad, 0xf8, 0xdd, 0x7b, 0xc5, 0xc3 } };


/////////////////////////////////////////////////////////////////////////////
// Control type information

static const DWORD BASED_CODE _dwMyvoipOleMisc =
	OLEMISC_INVISIBLEATRUNTIME |
	OLEMISC_ACTIVATEWHENVISIBLE |
	OLEMISC_SETCLIENTSITEFIRST |
	OLEMISC_INSIDEOUT |
	OLEMISC_CANTLINKINSIDE |
	OLEMISC_RECOMPOSEONRESIZE;

IMPLEMENT_OLECTLTYPE(CMyvoipCtrl, IDS_MYVOIP, _dwMyvoipOleMisc)

////////////////////
BEGIN_INTERFACE_MAP(CMyvoipCtrl, COleControl )
	INTERFACE_PART(CMyvoipCtrl, IID_IObjectSafety, ObjSafe)
END_INTERFACE_MAP()

/////////////////////////////////////////////////////////////////////////////
// IObjectSafety member functions

// Delegate AddRef, Release, QueryInterface

ULONG FAR EXPORT CMyvoipCtrl::XObjSafe::AddRef()
{
    METHOD_PROLOGUE(CMyvoipCtrl, ObjSafe)
    return pThis->ExternalAddRef();
}

ULONG FAR EXPORT CMyvoipCtrl::XObjSafe::Release()
{
    METHOD_PROLOGUE(CMyvoipCtrl, ObjSafe)
    return pThis->ExternalRelease();
}

HRESULT FAR EXPORT CMyvoipCtrl::XObjSafe::QueryInterface(
    REFIID iid, void FAR* FAR* ppvObj)
{
    METHOD_PROLOGUE(CMyvoipCtrl, ObjSafe)
    return (HRESULT)pThis->ExternalQueryInterface(&iid, ppvObj);
}

const DWORD dwSupportedBits = 
		INTERFACESAFE_FOR_UNTRUSTED_CALLER |
		INTERFACESAFE_FOR_UNTRUSTED_DATA;
const DWORD dwNotSupportedBits = ~ dwSupportedBits;
		
/////////////////////////////////////////////////////////////////////////////


// Allows container to query what interfaces are safe for what. We're
// optimizing significantly by ignoring which interface the caller is
// asking for.
HRESULT STDMETHODCALLTYPE 
      CMyvoipCtrl::XObjSafe::GetInterfaceSafetyOptions( 
		/* [in] */ REFIID riid,
        /* [out] */ DWORD __RPC_FAR *pdwSupportedOptions,
        /* [out] */ DWORD __RPC_FAR *pdwEnabledOptions)
{
	METHOD_PROLOGUE(CMyvoipCtrl, ObjSafe)

	HRESULT retval = ResultFromScode(S_OK);

	// does interface exist?
	IUnknown FAR* punkInterface;
	retval = pThis->ExternalQueryInterface(&riid, 
					(void * *)&punkInterface);
	if (retval != E_NOINTERFACE) {	// interface exists
		punkInterface->Release(); // release it--just checking!
	}
	
	// we support both kinds of safety and have always both set,
	// regardless of interface
	*pdwSupportedOptions = *pdwEnabledOptions = dwSupportedBits;

	return retval; // E_NOINTERFACE if QI failed
}

/////////////////////////////////////////////////////////////////////////////
// CStopLiteCtrl::XObjSafe::SetInterfaceSafetyOptions
// Since we're always safe, this is a no-brainer--but we do check to make
// sure the interface requested exists and that the options we're asked to
// set exist and are set on (we don't support unsafe mode).
HRESULT STDMETHODCALLTYPE 
	CMyvoipCtrl::XObjSafe::SetInterfaceSafetyOptions( 
        /* [in] */ REFIID riid,
        /* [in] */ DWORD dwOptionSetMask,
        /* [in] */ DWORD dwEnabledOptions)
{
    METHOD_PROLOGUE(CMyvoipCtrl, ObjSafe)
	
	// does interface exist?
	IUnknown FAR* punkInterface;
	pThis->ExternalQueryInterface(&riid, (void * *)&punkInterface);
	if (punkInterface) {	// interface exists
		punkInterface->Release(); // release it--just checking!
	}
	else { // interface doesn't exist
		return ResultFromScode(E_NOINTERFACE);
	}

	// can't set bits we don't support
	if (dwOptionSetMask & dwNotSupportedBits) { 
		return ResultFromScode(E_FAIL);
	}
	
	// can't set bits we do support to zero
	dwEnabledOptions &= dwSupportedBits;
	// (we already know there are no extra bits in mask )
	if ((dwOptionSetMask & dwEnabledOptions) !=
		 dwOptionSetMask) {
		return ResultFromScode(E_FAIL);
	}								
	
	// don't need to change anything since we're always safe
	return ResultFromScode(S_OK);
}


/////////////////////////////////////////////////////////////////////////////
UINT ComputeThreadProc(LPVOID pParam)
{  

	  DWORD   dwResult;
	  HRESULT hr;
	  bDone=FALSE;
	  while( !bDone ) 
	  { 
        dwResult = MsgWaitForMultipleObjects( 1, &g_hNotificationEvent, 
                                              FALSE, INFINITE, QS_ALLEVENTS);
        switch( dwResult )
        {
         case WAIT_OBJECT_0 + 0:
                // g_hNotificationEvent is signaled

                // This means that DirectSound just finished playing 
                // a piece of the buffer, so we need to fill the circular 
                // buffer with new sound from the wav file
		
			 if( FAILED( hr =CMyvoipCtrl::HandleNotification() ) )
                {
                    DXTRACE_ERR( TEXT("HandleNotification"), hr );
				
                    bDone = TRUE;
                }

                break;

        }
    }		
		return 0;
}
// CMyvoipCtrl::CMyvoipCtrlFactory::UpdateRegistry -
// Adds or removes system registry entries for CMyvoipCtrl

BOOL CMyvoipCtrl::CMyvoipCtrlFactory::UpdateRegistry(BOOL bRegister)
{
	// TODO: Verify that your control follows apartment-model threading rules.
	// Refer to MFC TechNote 64 for more information.
	// If your control does not conform to the apartment-model rules, then
	// you must modify the code below, changing the 6th parameter from
	// afxRegApartmentThreading to 0.

	if (bRegister)
		return AfxOleRegisterControlClass(
			AfxGetInstanceHandle(),
			m_clsid,
			m_lpszProgID,
			IDS_MYVOIP,
			IDB_MYVOIP,
			afxRegApartmentThreading,
			_dwMyvoipOleMisc,
			_tlid,
			_wVerMajor,
			_wVerMinor);
	else
		return AfxOleUnregisterClass(m_clsid, m_lpszProgID);
}


/////////////////////////////////////////////////////////////////////////////
// CMyvoipCtrl::CMyvoipCtrl - Constructor

//long CMyvoipCtrl::timer=0;

CMyvoipCtrl::CMyvoipCtrl()
{
	InitializeIIDs(&IID_DMyvoip, &IID_DMyvoipEvents);
     bRecord=false;

	// TODO: Initialize your control's instance data here.
}


/////////////////////////////////////////////////////////////////////////////
// CMyvoipCtrl::~CMyvoipCtrl - Destructor

CMyvoipCtrl::~CMyvoipCtrl()
{
	// TODO: Cleanup your control's instance data here.
}


/////////////////////////////////////////////////////////////////////////////
// CMyvoipCtrl::OnDraw - Drawing function

void CMyvoipCtrl::OnDraw(
			CDC* pdc, const CRect& rcBounds, const CRect& rcInvalid)
{
	// TODO: Replace the following code with your own drawing code.
	pdc->FillRect(rcBounds, CBrush::FromHandle((HBRUSH)GetStockObject(WHITE_BRUSH)));
	pdc->Ellipse(rcBounds);
}


/////////////////////////////////////////////////////////////////////////////
// CMyvoipCtrl::DoPropExchange - Persistence support

void CMyvoipCtrl::DoPropExchange(CPropExchange* pPX)
{
	ExchangeVersion(pPX, MAKELONG(_wVerMinor, _wVerMajor));
	COleControl::DoPropExchange(pPX);

	// TODO: Call PX_ functions for each persistent custom property.

}


/////////////////////////////////////////////////////////////////////////////
// CMyvoipCtrl::OnResetState - Reset control to default state

void CMyvoipCtrl::OnResetState()
{
	COleControl::OnResetState();  // Resets defaults found in DoPropExchange

	// TODO: Reset any other control state here.
}


/////////////////////////////////////////////////////////////////////////////
// CMyvoipCtrl::AboutBox - Display an "About" box to the user

⌨️ 快捷键说明

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