2

I'm missing something obvious here. I am using the DJGPP C compiler. I can successfully open executables, but not files in their default programs.

#include <stdio.h>

int main(void) {
    char sys_cmd[100] = "C:\\WINDOWS\\system32\\Calc.exe";
    system(sys_cmd);

    system("\"C:\\TextFile.txt\"");

    system("\"www.bbc.co.uk\"");
    getch();

    return 0;
}

Running this code causes the calculator to start up. Upon closing the calculator it reports:

Bad command of file name   
Bad command of file name    

So there is an issue with the other two system calls. I have tried adding "START " to the system call but that doesn't fix it. Having just searched my Windows 7 Pro system it doesn't have a START.EXE. Adding "cmd.exe " just starts cmd, so it's as if no arguments are sent.

I can type "C:\TextFile.txt" into cmd normally and it will open notepad.exe with TextFile.txt openned inside.

Ideally I'd use something safer than system() to do this anyway, but I haven't seen any examples of ShellExecute in DJGPP.

user1228123
  • 424
  • 4
  • 15
  • 'system' does not return until the contained command exits. Therefore, the second and third call to system are not within the scope of the execution of calc.exe. Suggest, for the second call to system, execute an editor with a parameter of the file to edit. Similarly with the third call to system – user3629249 Jul 06 '15 at 13:39

1 Answers1

2

You won't find ShellExecute in djgpp because it is DOS only and does not create real Windows applications and thus does not have access to a windows Api call.

ShellExecute would be able to use Windows registry to find out which application needs to be used for opening a certain file extension.

I don't know if exec() would be able to do this but I have doubts.

For very old versions a cross compiler existed that allowed to call windows api from djgpp compiled programs. But as this was based on the 2.9 version of the compiler: http://www.delorie.com/djgpp/v2faq/faq3_6.html this can be considered a dead end.

Like described in the comment below you could insert a static mapping from .txt to notepad. If you have to support many extension you have basically two choices:

  • switch compiler to a compiler that supports windows api (there is visual studio besides others)
  • execute ftype and assoc to find out which extension maps to which program and keep your dos compiler

Please note that djgpp is quite outdated and does not receive the same updates Gcc does

A "hack" would be to create a temporary .cmd or .bat from your C program and delegate starting of the file you need to that batch.

Marged
  • 10,577
  • 10
  • 57
  • 99
  • 1
    In case OP doesn't fully grasp this, it's also worth noting that he/she is attempting to execute a file as if it were a binary. To achieve the above, the code would have to be something like `path_to_notepad.exe inputfile.txt`, and `path_to_firefox.exe URL_TO_OPEN`. – Cloud Jul 05 '15 at 13:07
  • @Dogbert Do note that with the windows apis you can "execute" files that way, and windows launches the default program for the file ending you're trying to execute. (Or in the case of URLs, runs the URL handlers, though the OP would need to use `"http://www.bbc.co.uk"` instead of just "www.bbc.co.uk" to make that happen) – nos Jul 05 '15 at 13:24
  • I'll try exec(). Why does cmd.exe get run but without the arguments? – user1228123 Jul 05 '15 at 14:04
  • Thanks for the help. I tried exec() but that didn't work. Sounds like I need to move on from DJGPP which is a shame as I like it. – user1228123 Jul 05 '15 at 14:19
  • @user1228123 I used djgpp for 16 years to develop for DOS but it is getting harder and harder to keep it running. If you don't need to support real dos with a dos extender you are better off with a real compiler for windows. You have a much better tool chain for Windows. – Marged Jul 05 '15 at 14:25
  • @nos Does windows have hooks that capture the `system(...)` call and has it call ShellExecute? I'm pretty sure OP needs to use a call other than `system()` or `exec()`. – Cloud Jul 05 '15 at 14:51
  • `system("C:\\WINDOWS\\system32\\notepad.exe \"C:\\TextFile.txt\"");` does work but as noted, I do want to support all sorts of different file types. How do I use `ftype` and `assoc` to look up the default application for a given file type? – user1228123 Jul 05 '15 at 14:58
  • @Dogbert system() works fine for that in win32. But as others have mentioned, the OP is not programming to the windows API, but using DOS - which certainly do not do that, even when you run the exe files on windows. – nos Jul 05 '15 at 15:25
  • @user1228123 try google or just run assoc and ftype, you will grasp the concept easily. Here is just one of the hits presented by Google: http://commandwindows.com/assoc.htm – Marged Jul 05 '15 at 15:44
  • @Marged Thanks, I thought they were C functions. Sadly both `system("assoc .txt");` and `system("ftype txtfile");` return `Bad command or file name`. They both worked fine natively in cmd though. – user1228123 Jul 05 '15 at 16:31
  • @nos Yes, but I'm explicitly noting that `system(const char* cmd)` will only accept an executable if you expect it to work, and need to use `ShellExecute()` (http://stackoverflow.com/questions/3037088/how-to-open-the-default-web-browser-in-windows-in-c) to have the command use the default shell handling behavior (ie: open www links in browser, text files in default text editor, etc). What I was asking was if in Windows, just using the `system()` call in C, does the the call get passed on to `ShellExecute()`? – Cloud Jul 06 '15 at 00:47
  • 1
    @Dogbert system() does not call ShellExecute(). In win32, system("c:\\temp\\test.txt") runs "cmd.exe c:\\temp\\test.txt". This still allows you to "run" files that are not exe files. It will open .txt files in the default editor for .txt files. It will open .pdf files in the default .pdf viewer. Though it will not handle URLs directly, to do that you'd have to do e.g. `system("start http://www.google.com");` (And; system() in DOS/DJGPP is not in any way the same as system() in win32.) – nos Jul 06 '15 at 08:15
  • @nos Thanks for the info. – Cloud Jul 06 '15 at 11:10