• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 244
  • Last Modified:

Getting the path set upon startup

Hi,

I'm trying to begin my soujourn back into C++ from C# to add some much needed functionality to an existing program, but am having trouble getting the program to run in the first place.  It compiles, but I think it's expecting to be run from the command line, based on the fact that it expects two input parameters. ( see "if( argc == 2 )" below ).

So I decided I'd just programmatically give whatever input path it needed in order to find the dll, and be on my way.  But given that it's expecting an "unsigned char *", it won't allow me to assign the path explicitly.

How can I get this code to access a static path, such as "C:\MyProgram";?  I tried:

pathstr = "C:\MyProgram";

but export_dll_functions() is expect an unsigned char *.

Thanks,
Bob


int main(int argc, unsigned char **argv)
{      
    unsigned char *pathstr;
    char msgbuf[1024];
    BarcodeRecord Brec;
    BTDevList  devs[32];

    pbc   = &Brec;
    pdevs = &devs[0];
    pmsg  = msgbuf;

    if( argc == 2 )
        pathstr = argv[1];
    else  
        pathstr = 0;
   
    if ( export_dll_functions( pathstr ) )
       return -1;
.
.
.
0
ba272
Asked:
ba272
  • 3
  • 2
  • 2
  • +1
3 Solutions
 
AxterCommented:
>>How can I get this code to access a static path, such as "C:\MyProgram";?  I tried:

You need to use two characters \\ for every single one \

"C:\\MyProgram"
0
 
AxterCommented:
You can try the following:

if( argc == 2 )
        pathstr = (unsigned char *)argv[1];
0
 
ba272Author Commented:
Yes, I use two for each one.  I was just typing that request and was thinking in English, instead of synatx.  Toushe.

The problem I am having is that it's expecting two arguments, but I don't know what they are.  So when I change the value argc to "1" so that the assignment to pathstr takes place, the value is <bad ptr>.  So I just want to get past that all by assigning pathstr to a known path, and using it in the call to export_all_functions().  But I don't know how to convert "C:\\MyProgram" to an unsigned char*.

Do you know?

Thanks.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
jkrCommented:
>>But I don't know how to convert "C:\\MyProgram" to an unsigned char*.

You can just cast it like axter wrote or:

int main(int argc, char **argv)
{    
   unsigned char *pathstr;

//...

   if( argc == 2 )
        pathstr = static_cast<unsigned char*>(argv[1]);
   else  
       pathstr = 0;
   
    if ( export_dll_functions( pathstr ) )
       return -1;
0
 
AxterCommented:
FYI:
When you call this program, and the path has spaces, you need to make sure to call it with double quotes.

MyProgram.exe "C:\Program Files\Common Files\ODBC\Data Sources"

If you dont' use double quotes, then your program will parse the path into mulitple input arguments.
0
 
ba272Author Commented:
I found the answer.  I needed to set the command line properties, so the arguements were not empty.

Thanks.
0
 
jkrCommented:
>>I needed to set the command line properties

I'd rather suggest to test command line applications from the command line... *g*
0
 
itsmeandnobodyelseCommented:
>>>> I found the answer.  I needed to set the command line properties, so the arguements were not empty.

If you want to set a default path if the program wasn't started from the command line, you could do that:


int main(int argc, unsigned char **argv)
{    
    unsigned char *pathstr = (unsigned char *)"c:/myprogram" ;  // Note, most API take '/' instead of '\\'
    char msgbuf[1024];
    BarcodeRecord Brec;
    BTDevList  devs[32];

    pbc   = &Brec;
    pdevs = &devs[0];
    pmsg  = msgbuf;

    if( argc == 2 )    // Actually, only one argument was passed. 'argv[0]' always is the program path and file
                             // So, argv[0] should contain "c:\\programFiles\myProgram.exe" if that is your prog

        pathstr = (unsigned char*)argv[1];   // cast it to unsigned char*
    //else                     // You don't need the 'else' part as you have a 'default'
    //    pathstr = 0;
   
    if ( export_dll_functions( pathstr ) )
       return -1;


Note, you also could define pathstr as char* and do the cast to unsigned char* only when calling export_dll_functions( (unsigned char*) pathstr) or - even better - change the interface to  export_dll_functions( const char *).


Regards, Alex


0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 3
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now