Link to home
Start Free TrialLog in
Avatar of rkattan
rkattanFlag for Italy

asked on

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?

Code:
#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();
    }
    system(cmd);
    free(cmd);
  }
  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.

thanks.

ps:
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).
Avatar of Jase-Coder
Jase-Coder

Hi
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
Avatar of rkattan

ASKER

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..
Avatar of rkattan

ASKER

OMG:
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)?

thanks
ASKER CERTIFIED SOLUTION
Avatar of PaulCaswell
PaulCaswell
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial