• C

Modify a C Program to compile with windows

I have this simple C Program, I got it from (http://www.chooseopera.com/CDProject/portable.html) to make an autorun CD with opera. I compiled it, as explained in the article with BCC 5.5, but when it runs it opens a dos console for a moment, and I am trying to make it a windows executable by compiling it with Visual Studio 2005 (Student edition), but it fails, and as I am not a C programmer, I was not able to fix it. I was able to fix a couple of warnings (strcat with strcat_s), also changed getdisk into getdrive, but still gives error.

can someone tell me what modification need to be made to allow it to become a windows executable that works?

#include <dir.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char *argv[])
  char *cmd, *ptr;
  int i, len = 0;
  for (i = 1; i < argc; i++)
    len += 1 + strlen(argv[i]);
  cmd = (len > 0) ? malloc(len) : NULL;
  if (cmd != NULL)
    *cmd = '\0';
    for (i = 1; i < argc; i++)
      if (i != 1)
        strcat(cmd, " ");
      strcat(cmd, argv[i]);
    while ((ptr = strchr(cmd, '@')) != NULL)
      *ptr = 'A' + getdisk();
  return 0;

this code should make the when launched:
autorun Opera @:\folder\file.html

will change the @ into the current drive letter then launch the command.


the changes I made where:
remove dir.h, put direct.h, window.h
change getdisk() into getdrive()-1
change strcat insto strcat_s (but here i may have messed something, crashes when doing these).
Who is Participating?
>>How can i make a windows app that starts with no console at all, or a small Windows style window then execute a System(cmd)?
Take a look at this question. I am not familliar with the techniques suggested but it may help.

To achieve this with the minimal work, make a shortcut to the program, take properties of the shortcut, in the 'shortcut' tab choose Run:Minimized (Default is 'Normal Window'). From then on, launch the shortcut.

you have not allocated memory for the variables: char *cmd, *ptr;

if you want them to contain strings, in the declaration do:   char cmd[200], ptr[200] or use dynamic memory allocation for example:

char *cmd, *ptr;

cmd = (char *)malloc(sizeof(char) * STR_LEN);
ptr = (char *)malloc(sizeof(char) * STR_LEN);
actually I just noticed you used the malloc function, sorry! malloc returns a void * pointer so you have to cast the return value like:

cmd = (len > 0) ? (char *)malloc(len) : NULL;

also in your malloc statement do:

len + 1 so you have allocated memory for the null terminator
rkattanAuthor Commented:
the problem start to occur when, after the mods i made with the strcat_s, when i pass 1 parameter (for ex: abc) it will arrive till the end and printf("%s", cmd) works, but when i put the @ or a space execution stops..
rkattanAuthor Commented:
all what i was doing is in the wrong way:

I managed to get it to compile by putting the string directly in the c code instead of passing parameters, and this worked (no malloc, no sizes)..
BUT, even like this when i launch it, it will open for a moment a dos console before launching the command...

How can i make a windows app that starts with no console at all, or a small Windows style window then execute a System(cmd)?

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.