Datasheet
API multiplatform from townet
82
};
USE OF MKAPI LIBRARY
To write a C program using MkApi you need to include the library when you compile your application, and yu need
to add the source file “MkApi.h” to ypur C sources.
You need to istantiate an object of the class “mikrotikboard”:
mikrotikboard mk;
Then you need to ask for a login:
AnsiString res = mk.login("192.168.1.1", "admin", "secret");
If there is an error you can signal it, and for example stop the application:
if (res !=0)
{ Application->MessageBox(res.c_str(), "Errore", MB_OK);
Application->Terminate();
};
Now it’s possible to execute commands:
AnsiString adrs = mk.command("/ip/address/print");
Application->MessageBox(adrs.c_str(), "Assigned addresses", MB_OK);
If you don’t want to receive a popup window in case of error you can use xcommand:
AnsiString adrs = mk.xcommand("/ip/address/print");
if (adrs.Pos("!trap") >0) { HANDLE THE ERROR };
Application->MessageBox(adrs.c_str(), "Assigned addresses", MB_OK);
You can handle interactive commands in this way:
start_icommand("/interface/wireless/scan");
int k;
for (k=0; k<10; k++)
{ AnsiString obj = icommand();
printf("NEIGHBORS: %s\n", obj.c_str());
};
end_icommand();
A routine that execute a command gives back a string containing the reply of the mikrotik board. This string is non
modified because the format is not standard, and can be slightly different from a command to another one. So we
write two functions: parse_par and parse_line, that can be used to read the parameters returned from a mikrotik board
without errors. Here wodn there is an example of code usable to read and write on the computer screen the ip
addresses assigned to a mikrotik board.
mikrotikboard mk;
AnsiString res=mk.login("192.168.1.1", "admin", "print");
if (res="")
{ AnsiString indirizzi =mk.command("/ip/address/print");
//This copy the ansistring on a buffer of chars.
char c[10000]; strcpy(c, indirizzi.c_str());
while (c[0]!=0)










