calling GNUPLOT functions with straight C from WINDOWS gui

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • allynm
    New Member
    • Apr 2010
    • 20

    calling GNUPLOT functions with straight C from WINDOWS gui

    Hello everyone,

    I am trying to figure out how to call GNUPLOT commands from a WINDOWS app. I am not coding in C++, I'm using WINDOWS APIs.

    This is pretty straightforward problem if one is calling from a console app. Several developers (N. Devilla, for one) have done this. Their approach is to use _popen() (windows) or popen()(UNIX, LINUX). This approach doesnt work in a WINDOWS GUI app, however, only in a console app. It is also possible to use system()to do exactly the same thing.

    Somehow there must be a way to invoke GNUPLOT using CreateProcess() after creating a pipe. But, I can't figure out how to do it. MSDN library has an example of redirecting using CreatePipe, but I can't understand how to apply it in this circumstance. Can someone suggest a solution?

    Thanks,
    Mark Allyn
  • allynm
    New Member
    • Apr 2010
    • 20

    #2
    Here's an answer to my own question. I use anononymous pipe to a Gnuplot process. I WriteFile the plot commands on a redirected stdin handle. It works. It's ugly, but it does work.

    Code:
    #include <windows.h>
    #include <stdio.h>
    #include <tchar.h>
    
    int _tmain (int argc, LPTSTR argv [])
    
    {
    	DWORD i;
    	HANDLE hReadPipe, hWritePipe;
    	
    	SECURITY_ATTRIBUTES PipeSA = {sizeof (SECURITY_ATTRIBUTES), NULL, TRUE};
    			/* Init for inheritable handles. */
    	TCHAR outBuf[ ] = TEXT("a=2; plot sin(a*x)/x; pause mouse; plot exp(-a*x); pause mouse") ;	
    	TCHAR inBuf[80];
    	DWORD dwWritten, dwRead ;
    	BOOL  bSuccess = FALSE;
    	PROCESS_INFORMATION  ProcInfo2;
    	STARTUPINFO StartInfoCh2;
    	
    
    	/* Startup info for the Gnuplot process. */
    
    	GetStartupInfo (&StartInfoCh2);
    
    	/* Create an anonymous pipe with default size.
    		The handles are inheritable. */
    
    	bSuccess = CreatePipe (&hReadPipe, &hWritePipe, &PipeSA, 0);
    	if (bSuccess == TRUE) printf("pipe created\n");
    
    	WriteFile(hWritePipe, outBuf, sizeof(outBuf), &dwWritten, NULL) ;	
    	printf("Wrote %d bytes to Gnuplot\n", dwWritten) ;
    	
    	CloseHandle (hWritePipe);
    	
    	/* Repeat (symmetrically) for the child process. */
    
    	StartInfoCh2.hStdInput  = hReadPipe;
    	StartInfoCh2.hStdError  = GetStdHandle (STD_ERROR_HANDLE);
    	StartInfoCh2.hStdOutput = GetStdHandle (STD_OUTPUT_HANDLE);
    	StartInfoCh2.dwFlags = STARTF_USESTDHANDLES;
    	bSuccess = FALSE ;
    	bSuccess = CreateProcess ("c:\\gnuplot\\bin\\pgnuplot.exe", NULL, NULL, NULL,
    			TRUE,0, NULL, NULL, &StartInfoCh2, &ProcInfo2);
    	if (bSuccess == TRUE)
    	  printf("Created Gnuplot Process\n" ) ;
    	
    	WaitForSingleObject (ProcInfo2.hProcess, INFINITE);
    	CloseHandle (ProcInfo2.hThread); 
    	CloseHandle (hReadPipe);
    
    	/* Wait for Gnuplot process to complete.*/
    	
    	CloseHandle (ProcInfo2.hProcess);
    	return 0;
    }
    Mark Allyn

    Comment

    Working...