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

//+++++++++++++++++++++++++++++++++++
// +++++ DDEExecute 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]		//command
#define ARG3_IN		prhs[2]		//item
#define ARG4_IN		prhs[3]		//timeout

#define ARG1_OUT	plhs[0]		//return code




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

{
	DWORD	timeout		= TIMEOUT;
    double	Result		= 0;



	if (	TestIOArgs( nrhs, ARGS_IN_MIN, ARGS_IN_MAX ) &&		//alle erforderlichen Parameter angegeben?
			TestChannelArg( ARG1_IN ) &&						//Channel-Argument ok?
			TestCommandArg( ARG2_IN ) )							//Item-Argument ok?
	{
		if ( nrhs == ARGS_IN_MAX )								//4. Parameter (TimeOut)?		
			timeout = TestTimeoutArg( ARG4_IN );

		if (timeout != (DWORD)-1	)							//-1, wenn timeout fehlerhaft
		{
			DWORD	hChannel[2];	//2 mal 32 bit
			int		n;
	
//			char* szCommand = (char*) mxCalloc( n= (int)mxGetN(ARG2_IN) + 1, sizeof(char) );
			HLOCAL hCommand = LocalAlloc( LPTR, n= (int)mxGetN(ARG2_IN) + 1 );
			char* szCommand = (char*) LocalLock( hCommand ); 

			if ( szCommand )
			{
				mxGetString( ARG2_IN, szCommand, n );
		
		        hmemcpy( (char*)hChannel, (char huge*)mxGetPr(ARG1_IN), sizeof(double) );
		
		        HCONV		hConv	= hChannel[0];
			    HDDEDATA	hTrans;
		
				hTrans = DdeClientTransaction( (LPBYTE)szCommand, (DWORD)n, hConv, NULL, CF_TEXT,
												XTYP_EXECUTE, timeout, NULL );	//Item ist nicht signifikant
				//hTrans = 1, wenn o.k.
					if ( hTrans )
						Result = (double)1;
		
//				mxFree( szCommand );
				LocalUnlock( hCommand );
				LocalFree( hCommand );

			}
			else
			{
				mexPrintf( "???  Memory allocation error.\r\n" );
				ErrorBeep();
			}
		}
    }
    
	ARG1_OUT = mxCreateFull(1, 1, REAL);
	hmemcpy( (char huge*)mxGetPr(ARG1_OUT), (char*)&Result, sizeof(double) );
}
Detected encoding: ASCII (7 bit)2