[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Modify a C Program to compile with windows

Posted on 2006-04-18
5
Medium Priority
?
675 Views
Last Modified: 2012-08-14
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).
0
Comment
Question by:rkattan
  • 2
  • 2
5 Comments
 
LVL 11

Expert Comment

by:Jase-Coder
ID: 16476742
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);
0
 
LVL 11

Expert Comment

by:Jase-Coder
ID: 16476761
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
0
 
LVL 2

Author Comment

by:rkattan
ID: 16476999
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..
0
 
LVL 2

Author Comment

by:rkattan
ID: 16477219
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
0
 
LVL 16

Accepted Solution

by:
PaulCaswell earned 600 total points
ID: 16478183
>>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.
http://www.experts-exchange.com/Programming/Programming_Languages/C/Q_21814420.html

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.

Paul
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
Suggested Courses
Course of the Month19 days, 18 hours left to enroll

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question