473,320 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

calling GNUPLOT functions with straight C from WINDOWS gui

20
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
Jan 26 '12 #1
1 4216
allynm
20
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.

Expand|Select|Wrap|Line Numbers
  1. #include <windows.h>
  2. #include <stdio.h>
  3. #include <tchar.h>
  4.  
  5. int _tmain (int argc, LPTSTR argv [])
  6.  
  7. {
  8.     DWORD i;
  9.     HANDLE hReadPipe, hWritePipe;
  10.  
  11.     SECURITY_ATTRIBUTES PipeSA = {sizeof (SECURITY_ATTRIBUTES), NULL, TRUE};
  12.             /* Init for inheritable handles. */
  13.     TCHAR outBuf[ ] = TEXT("a=2; plot sin(a*x)/x; pause mouse; plot exp(-a*x); pause mouse") ;    
  14.     TCHAR inBuf[80];
  15.     DWORD dwWritten, dwRead ;
  16.     BOOL  bSuccess = FALSE;
  17.     PROCESS_INFORMATION  ProcInfo2;
  18.     STARTUPINFO StartInfoCh2;
  19.  
  20.  
  21.     /* Startup info for the Gnuplot process. */
  22.  
  23.     GetStartupInfo (&StartInfoCh2);
  24.  
  25.     /* Create an anonymous pipe with default size.
  26.         The handles are inheritable. */
  27.  
  28.     bSuccess = CreatePipe (&hReadPipe, &hWritePipe, &PipeSA, 0);
  29.     if (bSuccess == TRUE) printf("pipe created\n");
  30.  
  31.     WriteFile(hWritePipe, outBuf, sizeof(outBuf), &dwWritten, NULL) ;    
  32.     printf("Wrote %d bytes to Gnuplot\n", dwWritten) ;
  33.  
  34.     CloseHandle (hWritePipe);
  35.  
  36.     /* Repeat (symmetrically) for the child process. */
  37.  
  38.     StartInfoCh2.hStdInput  = hReadPipe;
  39.     StartInfoCh2.hStdError  = GetStdHandle (STD_ERROR_HANDLE);
  40.     StartInfoCh2.hStdOutput = GetStdHandle (STD_OUTPUT_HANDLE);
  41.     StartInfoCh2.dwFlags = STARTF_USESTDHANDLES;
  42.     bSuccess = FALSE ;
  43.     bSuccess = CreateProcess ("c:\\gnuplot\\bin\\pgnuplot.exe", NULL, NULL, NULL,
  44.             TRUE,0, NULL, NULL, &StartInfoCh2, &ProcInfo2);
  45.     if (bSuccess == TRUE)
  46.       printf("Created Gnuplot Process\n" ) ;
  47.  
  48.     WaitForSingleObject (ProcInfo2.hProcess, INFINITE);
  49.     CloseHandle (ProcInfo2.hThread); 
  50.     CloseHandle (hReadPipe);
  51.  
  52.     /* Wait for Gnuplot process to complete.*/
  53.  
  54.     CloseHandle (ProcInfo2.hProcess);
  55.     return 0;
  56. }
  57.  
Mark Allyn
Feb 1 '12 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: John Harrison | last post by:
What are the rule concerning calling member functions from an initialiser list? Suppose I have class C : public B { public: C() : x(), y(f()), z() {} private: Y f(); X x;
5
by: Praveen Srinivasan | last post by:
Hi all, Are there any important issues that one should be aware of when calling C++ functions from C? In particular, I'm passing a function pointer to a C routine in a library, and in that...
6
by: Joseph Suprenant | last post by:
Hello all, I have a C++ program, it does some calculations on things and then prints out a file in the format in which GNUPLOT can use. So my question is how would i call GNUPLOT from my C++...
10
by: reynoldscraigr | last post by:
Hi All, I am trying to call 2 functions from the 1 onMouseOver event on an anchor tag, but I can't seem to get it to work. the following should demonstrate <a...
19
by: Ross A. Finlayson | last post by:
Hi, I hope you can help me understand the varargs facility. Say I am programming in ISO C including stdarg.h and I declare a function as so: void log_printf(const char* logfilename, const...
1
by: Gerald Klix | last post by:
I read the whol email thread carefully and could not find any sentence by Guido, which states that he does not accept ctypes for the standard library. He just declined to rewrite winreg. Did I miss...
46
by: Usenet User | last post by:
I need to be able to call the same API function from VB, which will reside in different DLLs. While all of the functions have the same signature and name, DLL file names are not known at compile...
4
by: Meya-awe | last post by:
Hi, What does C# has to offer for calling system functions from within the VS .NET C# project? I want to kickoff a script or a batch file from my code. thanks, BRAMOIN *** Sent via...
47
by: teju | last post by:
hi, i am trying 2 merge 2 projects into one project.One project is using c language and the other one is using c++ code. both are working very fine independently.But now i need to merge both...
6
by: teju | last post by:
hi, i have a code written in c now merged with c++ code i mean i am calling c functions in c++ using extern "C" but now i want to use some objects declared and defined in c++ to be available in...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.