1

I have following code:

int _tmain(int argc, char** argv) {
  bool g_graphics = true;
  palPhysics * pp = 0;
  #ifndef PAL_STATIC
  PF -> LoadPALfromDLL();
  #endif
  char a[] = "Bullet";
  std::string aa;
  aa = std::string(argv[1]);
  //PF->SelectEngine("Bullet");
  DebugBreak();
  PF -> SelectEngine(argv[1]);
  //PF->SelectEngine(aa);
  //debug
  //    assert(false);
  pp = PF -> CreatePhysics();
}

I am trying to read in the command line argument no. 1 in this line:

PF->SelectEngine(argv[1]);

However, I only get the first letter of the argument. I have also tried changing

int _tmain(int argc, char** argv)

to

int _tmain(int argc, TCHAR** argv), but then I get

error:

error C2664: 'palFactory::SelectEngine' : cannot convert parameter 1 from 'TCHAR *' to 'const PAL_STRING &'

PAL_STRING is just a std::string.

This might be a simple one, but I am not sure how to convert TCHAR to std::string, especially since TCHAR is something else depending on compiler /environment settings. Is anyone aware of an easy way to get the command-line arguments to work, such that I don't need to convert anything myself, i..e maybe by changing the tmain function?

Thanks!

C

Update: example of invoking on command line:

Yep. so the way I invoke this on command line is:

progname.exe arg1 arg2,

where arg1 is a physics engine I am trying to load, and arg2 is a dae(physics file with physics info), so I go, specifically:

progname.exe Bullet E:/a.dae

Stepping into the line "PF->SelectEngine(argv[1]);" gives the following code:



bool palFactory::SelectEngine(const PAL_STRING& name) {
#ifdef INTERNAL_DEBUG
    printf("palFactory::SelectEngine: this = %p\n", this);
#endif
    SetActiveGroup(name); // also calls RebuildRegistry
    return isClassRegistered("palPhysics");
}

, in this case, when debugging, I can see that const PAL_STRING& name, i.e. the string, is just "B", instead of what I would expect it to be, which is "Bullet", my command line argument I have passed in the command line.

Hans Fortmüller
  • 161
  • 1
  • 1
  • 7

1 Answers1

0

I've been plauged by this problem for years. The only solution I've been able to find is to NOT USE Visual Studio. I've had to fall back to using other compilers when I must be able to process command-line args. Specifically, I've been using the Digital Mars compiler successfully. It handles the command-line args correctly. I use the VS environment for intellisense and debugging, then compile with DMC to deploy.

---edit below---

Turns out, I just wasn't asking the right question. I finally asked the right question, and got the right answer! See link below.

What is the difference between _tmain() and main() in C++?

Community
  • 1
  • 1
  • You should use the comments under the question instead of writing answer that does not really respond to the question. – MaxiWheat May 02 '13 at 18:26
  • Sorry MaxiWheat; I should have been more clear:

    ANSWER: Don't use Visual Studio to build command-line programs. It doesn't work correctly.

    I'm looking forward to reading other's answers.
    – davidlqualls May 03 '13 at 19:19