Search code examples
csystemdjgpp

Opening generic files with system() in DJGPP C


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.


Solution

  • 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.