Source file: /~heha/messtech/matdde.zip/source/ddeunadv/ddeunadv.cpp

//+++++++++++++++++++++++++++++++++++
// +++++ DDEUnadvise for MATLAB 16bit

#include <windows.h>
#include <ddeml.h>

#define DLLMEX
extern "C"
{
   #include "..\header\mex.h"
   #include "..\header\testargs.h"
}
#include "..\header\fastdde.h"
#pragma hdrstop


#define ARGS_IN_MIN 2
#define ARGS_IN_MAX 4


#define ARG1_IN	  prhs[0]  //channel
#define ARG2_IN	  prhs[1]  //item
#define ARG3_IN	  prhs[2]  //cbFormat
#define ARG4_IN	  prhs[3]  //timeout

#define ARG1_OUT  plhs[0]  //Result



/////////////////////////////////////////////////////////////////////////////////////////////////////
extern "C" void mexFunction(
	int	nlhs,
	Matrix	*plhs[],
	int	nrhs,
	Matrix	*prhs[]
	)

{
   DWORD timeout	= TIMEOUT;
   WORD	 cbFormat    = CF_TEXT;	 //default
    double  Result	   = 0;

   if (	 TestIOArgs( nrhs, ARGS_IN_MIN, ARGS_IN_MAX ) &&    //alle erforderlichen Parameter angegeben?
	 TestChannelArg( ARG1_IN ) &&		      //Channel-Argument ok?
	 TestItemArg( ARG2_IN ) )			     //Item-Argument ok?
   {

      if ( nrhs >= ARGS_IN_MAX - 1 )		      //3. Parameter (Clipboard-Format)?
      {
	 cbFormat = TestFormatArg( ARG3_IN, NO_CHK_STR_FORMAT, NULL );
	 if ( nrhs == ARGS_IN_MAX )		   //4. Parameter (TimeOut)?
	    timeout = TestTimeoutArg( ARG4_IN );
      }

      if (cbFormat != (WORD)-1 &&		      //sind bei ungueltigen Argumenten
	 timeout != (DWORD)-1 )			   // -1
      {
	 DWORD hChannel[2];   //2 mal 32 bit
	 int	  n;

	 char* szItem = (char*) mxCalloc( n= (int)mxGetN(ARG2_IN) + 1, sizeof(char) );

	 if ( szItem )
	 {
	    mxGetString( ARG2_IN, szItem, n );

	      hmemcpy( (char*)hChannel, (char huge*)mxGetPr(ARG1_IN), sizeof(double) );

	      HCONV  hConv = hChannel[0];
	    DWORD idInst   = hChannel[1];
	    HSZ	     hszItem  = DdeCreateStringHandle( idInst, szItem, CP_WINANSI );

	    mxFree( szItem );

	      if ( hszItem )
	      {
		HDDEDATA   hTrans;

	       hTrans = DdeClientTransaction( (LPBYTE)NULL, (DWORD)0, hConv, hszItem, cbFormat,
				       XTYP_ADVSTOP, timeout, NULL );
	       //hTrans = 1, wenn o.k.
	       if ( hTrans )
	       {
		  if ( CallDDEUnadvise( hConv, hszItem, cbFormat ) ) //ist TRUE, wenn Abmeldung erfolgreich
		     Result = (double)1;
	       }

	       DdeFreeStringHandle ( idInst, hszItem );
	    }
	    else
	       HandleDdeError(DdeGetLastError(idInst));
	 }
	 else
	 {
	    mexPrintf( "???  Memory allocation error.\r\n" );
	    ErrorBeep();
	 }
      }
   }

   ARG1_OUT = mxCreateFull(1, 1, REAL);
   hmemcpy( (char huge*)mxGetPr(ARG1_OUT), (char*)&Result, sizeof(double) );
}



/////////////////////////////////////////////////////////////////////////////////////////////////////
BOOL CallDDEUnadvise( HCONV hConv, HSZ hszItem, WORD cbFormat )
{
   BOOL (FAR PASCAL *lpfnDDEUnadvise)( HCONV, HSZ, WORD );  //Zeiger auf Funktion DDEUnadvise()


   HMODULE hModule = GetModuleHandle( (LPCSTR) "ddeadv.dll" );
      if ( hModule )
      {
	 lpfnDDEUnadvise = (BOOL (FAR PASCAL*) (HCONV, HSZ, WORD)) GetProcAddress( (HINSTANCE)hModule,
						      (LPCSTR) "DDEUnadvise" );
	 if ( lpfnDDEUnadvise )
	    return (*lpfnDDEUnadvise)( hConv, hszItem, cbFormat );
      }
   return FALSE;
}


Detected encoding: ASCII (7 bit)2