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

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

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


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.