Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ C
Hi,
 
How can we run a C program on clicking a button in any GUI?
Posted 15-Feb-13 17:50pm
Comments
Sergey Alexandrovich Kryukov at 16-Feb-13 0:59am
   
Not clear. If you want to run a program, who cares how it was written, in what language?
Perhaps you don't even have and idea of compilation, linking, loading and running. And what is "any GUI"?
—SA
jakr13 at 16-Feb-13 1:15am
   
Better be specific with the question,so that everyone can understand and clear your doubts.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

In the click handler for the control, add something that starts the C program. How to do this is platform specific, and you didn't specify which platform you are interested in. For Windows, there are several ways of activating something:
 
WinExec() [don't use this any more]
CreateProcess() [and related]
ShellExecute()
ShellExecuteEx()
 
Google is your friend for these. I assume you know what to do...
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

1. Are you thinking of running C-code (automatic compile and link) or are you talking about compiled application ?

I asssume you mean writing Code in C that displays a GUI with a button ?
 
a. Are you searching for a way of programming a button independent from Operation System ?
You should use a library such as wxwindows, opengl or something like that.
 
b. You can call system() from stdlib.h independent from OS.
Depending on the Operation System you have to call different System-Calls.
For Unix you can call fork()/exec() for Windows you can use CreateProcess().
  Permalink  
v3
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

In addition to the Windows specific calls that were provided in Solution 1, Microsoft does implement some POSIX calls that perform application launching (exec, spawn, system).
 
See here:
http://msdn.microsoft.com/en-us/library/431x4c1w(v=vs.110).aspx[^]
http://msdn.microsoft.com/en-us/library/20y988d2.aspx[^]
http://msdn.microsoft.com/en-us/library/277bwbdz.aspx[^]
 
Do note that these calls are not C/C++ native, they are OS specific as stated in the other solution. This means that they may act slightly different in different platforms and will likely be defined in different headers in each OS.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 509
1 Nirav Prabtani 298
2 OriginalGriff 215
3 _Amy 165
4 Abhinav S 160
0 OriginalGriff 7,715
1 Sergey Alexandrovich Kryukov 6,911
2 Maciej Los 3,954
3 Peter Leow 3,698
4 CHill60 2,742


Advertise | Privacy | Mobile
Web04 | 2.8.140721.1 | Last Updated 16 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100