Page 1 of 1

Help with C, CGI and IIS 5

Posted: Wed Aug 18, 2010 6:26 am
by Igor_Kavinski
I am experimenting with CGI programming using C. Using IIS 5 as a webserver, everything seems to work fine in the browser. But I want to access resources outside the browser, like create files and directories and do a fair amount of folder management involving moving files etc between those folders. I thought I would simply use the system() function to execute console commands like copy, move etc. But it's not working. When I try to find the current drive, it tells me it's drive C. But when I try to get the current directory, it simply returns null. Using _mkdir() creates directories in the root of C drive. But I can't copy any files to that directory using the copy command. Creating another directory within the newly created directory is possible again with _mkdir. Are there any C functions that mimic the functions of the copy, move console commands?

Re: Help with C, CGI and IIS 5

Posted: Wed Aug 18, 2010 7:47 am
by notfred
I'm not a windows developer, but in POSIX land you would use the rename() call to do a mv whilst for a copy simply open the new file for write, the old file for read and loop through it. I wonder if you are running in to permissions issues more than anything else. Check the permissions set up in your webserver and make sure that the directories you are trying to access allow access by that user.

Re: Help with C, CGI and IIS 5

Posted: Wed Aug 18, 2010 11:11 am
by Igor_Kavinski
I'm pretty sure it's not permissions related because I can create as many directories as I want. But the console based commands all fail. Only library functions seem to work. Does that mean I have to call the system() function using some sort of privilege elevating function? How do I even go about doing that?

Re: Help with C, CGI and IIS 5

Posted: Wed Aug 18, 2010 12:10 pm
by morphine
Three things:

a) Your permissions are those of the webserver user. So you should set permissions on your folders accordingly: KB entry.
b) Messing with folders outside your web document directory is usually a Bad Idea.
c) If you already know some C, just use PHP (or Perl) and save yourself the hassle of compiling stuff, and the slowness of running it under CGI. Oh, that, and those languages are a much better fit for scripting than C is.

Re: Help with C, CGI and IIS 5

Posted: Wed Aug 18, 2010 1:37 pm
by notfred
morphine wrote:
c) If you already know some C, just use PHP (or Perl) and save yourself the hassle of compiling stuff, and the slowness of running it under CGI.
I'm confused by the "slowness of running it under CGI" part. Can you explain a bit more?

Re: Help with C, CGI and IIS 5

Posted: Wed Aug 18, 2010 2:51 pm
by morphine
notfred wrote:
morphine wrote:
c) If you already know some C, just use PHP (or Perl) and save yourself the hassle of compiling stuff, and the slowness of running it under CGI.
I'm confused by the "slowness of running it under CGI" part. Can you explain a bit more?

I should have written "as CGI" instead of "under CGI", which doesn't make much sense.

When a scripting language is run via CGI, a new Apache/IIS process has to be created. When they're run via the respective webserver modules this overhead can be avoided. AFAIK FastCGI is a lot better, but a module is still preferable.

Even if performance isn't something to worry about in this specific case, there's little reason for the OP to have to run the rigmarole of writing / compiling / deploying a C program when a much more convenient script equivalent would do.

Re: Help with C, CGI and IIS 5

Posted: Wed Aug 18, 2010 7:40 pm
by notfred
OK, understand it now.
Thanks!

Re: Help with C, CGI and IIS 5

Posted: Thu Aug 19, 2010 7:21 am
by Igor_Kavinski
I was finally able to get the file and folder moving functionality working by using the win32 CopyFile() and DeleteFile() functions. MoveFile() doesn't seem to work in Visual Studio 6. Hmmm...

There are some really fascinating C/C++ web programming frameworks out there like the Wt web toolkit and TNT webserver. Unfortunately, I find learning to be a very painful and time-consuming process. So for the time being, I will have to keep hacking at C code using CGI. Sigh.

The speed issues of CGI won't matter much to me since only a handful of people will be using my web app in an office LAN environment.

Re: Help with C, CGI and IIS 5

Posted: Thu Aug 19, 2010 9:02 am
by morphine
<?php

$src= 'c:/something';
$target= 'c:/otherthing';

rename( $src, $target );

unlink( $src.'/some_file' );
?>

It doesn't get much easier than this :)