Industry guru Dave Taylor offers tech support on technical and business topics, including iPhone, iPod, Microsoft Windows, Sony PSP, cellphones, online advertising, CSS, Web design, business, Unix, Linux, SEO, Mac OS X, and shell script programming.     


Porting C programs from Windows to Unix?

Dave, I read your books Teach Yourself Unix in 24 Hours and Wicked Cool Shell Scripts and since then I can't imagine working again on a system like windows!

My problem is that I've started to make my C files Unix-compatible but I can't find a command for C in Windows that does the same thing as system("cmd") for Unix. Any suggestions?


Dave's Answer:

This was a tough question, actually, because I haven't done any Windows-level C programming in, well, more years than I want to talk about. Let's just say it's been a rather long time. :-)

I do remember that the wonderful Unix system() call that makes it a breeze to invoke any other Unix command (or sequence of commands) from within a C program has no direct analog in the Windows programming world because of the fundamental differences in OS architecture.

To figure out how to best address this, I asked a couple of colleagues who are better versed in C / Windows programming, and here's what they had to say:


The system function executes an internal operating system command, or an .EXE, .COM (.CMD in Windows NT) or .BAT file from within a C program rather than from the command line.

The system function finds the command interpreter, which is typically CMD.EXE in the Windows NT operating system or COMMAND.COM


Your options for an analog to the Unix system call depend on how much work you want to undertake, and what you want to achieve. For total control, use the CreateProcess system call. It is low level, closer to the fork/exec level in UNIX. WinExec is a higher level function that is closer to system. You can also call system, but you have no control over console windows, window states, etc.
I hope those are helpful to you. If I bump into a better or more definitive answer, I'll post it as a comment, and, of course, it's quite possible that some other reader might have a better response that they can add here too!

Thanks to Ray Lischner, JP Morgenthal and Wayne Freeze for their assistance

More Useful Unix and Linux Help Articles:
✔   Copy and Paste from the Mac OS X Command Line?
I am constantly running commands in Terminal.app on my MacBook and then copying and pasting the results into email messages or documents. Yes,...
✔   Shell script to convert lowercase to title case?
As part of a project I'm working on, I find myself deep in a Linux shell script, needing to have a subroutine that...
✔   Can I script renaming files based on an XML data map?
I have a folder full of files which are named with four digits and a file extension e.g. 0312.file and an XML-file describing...
✔   Test for valid numbers in a Bash shell script?
In a different discussion on this site [see Redirecting input in a shell script] a visitor commented that "I was too busy trying...
✔   Review: iSSH for the iPad/iPhone
If you're running an online business like I am, there are times when you need to connect and log in to the server...

Let's stay in touch!
Sign up for my weekly AskDaveTaylor Newsletter and you'll receive even more tech and gadget help right to your inbox, along with exclusive news and industry updates. It's good stuff. I promise!
    Enter your name: and your email addr:  









Reader Comments To Date: 11

Martin said, on April 2, 2005 2:09 PM:

Something is not very clear to me.
You are converting Windows C programs to Unix and want to know about how to implement a certain C functionality of Unix in the Windows environment.
Isn't that the other way around?

Dave Taylor said, on April 2, 2005 2:57 PM:

I kinda wondered about that myself, Martin. My conclusion is that he's trying to convert his C programs within the Windows environment before he ports them to Unix.

Todd said, on April 11, 2005 4:54 PM:

Another possibility is that he's looking for the shell command ("cmd" in Windows) which, depending on the shells available will be one of "sh", "csh", "ksh", "tcsh", etc. (Bourne, C-Shell, Korn, etc)

This matches the example that was provided.
Hope this helps.

BPM said, on July 1, 2005 9:20 PM:

I have existing GUI SDK/C program in windows.
How can I port to unix environment specially to solaris and linux.
Can you please guide me if you have any examples or tool available which do the same things.

Thanks
BPM

capry said, on August 28, 2007 8:53 PM:

i have a c program which is running under linux operating system . i wish to run the same c program under windows operating system

what the steps i need to follow to run the same c code on windows platform .

i am using windows xp

the program is connecting the medical device to pc and reading the data onto the server

thanks
capry

capry said, on August 28, 2007 9:02 PM:

i have c program which is running on linux operating system want to run on windows operating system

the program is successfully executing the code on linux operating system


steps to follow is fine enough

thanks
capry

capry said, on August 28, 2007 9:04 PM:

i have c program which is running on linux operating system want to run on windows operating system

the program is successfully executing the code on linux operating system


steps to follow is fine enough

thanks
capry

Marc said, on November 9, 2007 7:37 PM:

Hello Dave and Team.
First off, I'd like to say thanks and congrats for all the help and info you and your team has/are providing for everyone to benefit freely. Sites such as this are, hummmm, lacking more and more to say the least. And I for one, really apreciate it all.

In the past I've found many helpful articles on this site, nad have past along the wealth to many friends. I don't really have a technical question per-say, but more along the lines of looking for an opinion. Some time ago I found another project I'd like to have your thoughts on. UNIX's WINE project have started some time ago a new project called ReactOS, for a new Operating System. You can find it @: www.reactos.org

I'd be currious to read your thoughts on the project, if of course your time allows it.

Thx muchly in advance for your time and thoughts on the matter.

Cheers, and keep up the great work.
Marc.

PS: When I complete my website, I'll most certainly add a link to yours.

Barbara Shi said, on January 9, 2008 3:01 PM:

I have linux c file which uses "system(cmdline)" and now I need to port it to Windows VC/C++.
How could I do it?

Thanks

Barbara

vipul patel said, on February 27, 2008 12:11 AM:

i have c program which is running on linux operating system want to run on windows operating system

the program is successfully executing the code on linux operating system

leslie said, on April 2, 2009 11:30 PM:

i have c program which is running on windows operating system, i want to run on linux operating system.
how to do that?

the program is successfully executing the code on windows operating system

Starbucks coffee cup I do have a lot to say, and questions of my own for that matter, but first I'd like to say thank you, Dave, for all your helpful information by buying you a cup of coffee!

I do have a comment, now that you mention it!











I will never send you any unsolicited email. Ever.






Check This Out Too...

 
Look for Answers
Need Help? Ask Dave Taylor!
Powered By
Linux Journal: Free Issue!


Follow Me on Pinterest

Find Me on Google+
ADT on G+
© 2002 - 2013 by Dave Taylor. All Rights Reserved.

Note: This web site is for the purpose of disseminating information for educational purposes, free of charge, for the benefit of all visitors. We take great care to provide quality information. However, we do not guarantee, and accept no legal liability whatsoever arising from or connected to, the accuracy, reliability, currency or completeness of any material contained on this web site or on any linked site. Further, please note that by submitting a question or comment you're agreeing to my terms of service, which are: you relinquish any subsequent rights of ownership to your material by submitting it on this site. My lawyer says "Thanks".
"Ask Dave Taylor®" is a registered trademark of Intuitive Systems, LLC.