SDK Question oapiGetNavDescr

JMW

Aspiring Addon Developer
Joined
Aug 5, 2008
Messages
670
Reaction score
89
Points
43
Location
Happy Wherever
Got another elusive query (for me):blush::

Can't find anything in threads on oapiGetNavDescr.

Why shouldn't this work?

global
Code:
char *descr2;

Code:
bool InputTarget(void *id, char *bstr, void *user_data)
{
.......................
VESSEL3 *vessel = (VESSEL3*)oapiGetFocusInterface();
int maxlen = 256;
NAVHANDLE n = vessel->GetNavSource(0);


oapiGetNavDescr (n, descr2, maxlen);
		sprintf(oapiDebugString(),"NAV ILS  %s ",descr2);

If I make int maxlen global and give it NULL, prints as "NULL".
If compile as code above, it ctd.

What am I doing wrong please?
 
The descr2 is just a pointer to char, and Orbiter doesn't allocate the buffer for you, but you must provide an address to already allocated writable memory as 2nd argument of oapiGetNavDescr, i.e. you need to either allocate the memory for character buffer pointed by descr2 pointer, or change it to be a character array and not a pointer.

So either:
  1. Code:
    descr2 = new char [maxlen];
    oapiGetNavDescr (n, descr2, maxlen);
    sprintf (oapiDebugString (), "NAV ILS  %s ", descr2);
    delete [] descr2;
  2. or
    Code:
    char descr2 [256];
    oapiGetNavDescr (n, descr2, sizeof (descr2));
    sprintf (oapiDebugString (), "NAV ILS  %s ", descr2);
 
I would go with the second option so you don't have to deallocate the memory yourself.
 
Back
Top