X
87 Rate this article:
No rating

INTERNAL: Printing IDL variables in a callable IDL Windows Console application

Anonym
Since IDL for Windows does not redirect IDL output to stdout as it does on UNIX, IDL's PRINT/IDL_Print() routines cannot be used to display output to the console. If you are developing a console (text-based) application that uses callable IDL, you will need to retrieve the variable values you would like to print, then display them using printf() in your C/C++ routine.

More information on the IDL typedefs and IDL_*() functions is available in the External Development Guide.

#include 
#include "stdafx.h"
#include "idl_export.h"

int _tmain(int argc, _TCHAR* argv[])
{
	char* output; /* handy string */
	IDL_INT* intArray; /* pointer to the array of IDL_INTs */
	IDL_VPTR variableVptr; /* pointer to the working IDL_VPTR */
	IDL_ARRAY* arrayVptr; /* pointer to the IDL_ARRAY structure*/
	int status = 0; /* disposable return variable */
	int i = 0; /* counter for looping */

	/* Initializes IDL without a GUI on Windows */
	/* (For IDL 6.2 and before...) */
	/*
	* if( IDL_Win32Init( 0, NULL, NULL, NULL ) ) 
	* {
	*/

        /* For IDL 6.3 and later... */ 
	init_data.options = IDL_INIT_NOCMDLINE;
	if (argc) {
		init_data.options |= IDL_INIT_CLARGS;
		init_data.clargs.argc = argc;
		init_data.clargs.argv = argv;
	}
  
	if (IDL_Initialize(&init_data)) {

		/* Define two variables inside IDL */
		status = IDL_ExecuteStr( "MYIDLSTRING = 'Hello!'" );
		status = IDL_ExecuteStr( "MYARRAY = INDGEN( 10 )" );

		/* Get and display the first (string) variable */
		variableVptr = IDL_GetVarAddr( "MYIDLSTRING" );
		output = IDL_VarGetString( variableVptr );
		printf( output );
		printf( "\n" );

		/* Get and display the second (int array) variable */
		variableVptr = IDL_GetVarAddr( "MYARRAY" );
		arrayVptr = variableVptr->value.arr;
		intArray = (IDL_INT *)arrayVptr->data;

		for( i = 0; i < arrayVptr->n_elts; i++ )
		{
			printf( "%d ", *intArray++ );
		}

		status = IDL_Cleanup( TRUE );
		return 0;
	}
	else
	{
		printf( "IDL not initialized!" );
		return -1;
	}
}