MikroTik API in C Win32 platform

Hello all,
I have one big problem. I have a bachelor work with MikroTik RB433 and i should make an application in C like WinBox but very simple. My problem is that I really dont know how to do it. Maine problem is that as I saw here many codes are here but as a newbie I need to know which libraries I need, how to connect and how to communicate with the device. I read a wiki and see the examples but these are used in linux but the main problem are header files like:
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
which I cant find anywhere or can but these includes another headers and another so I m confused with it. In my opinion the main problem at all is sample client which is written on linux board but on windows platform can include another headers which are supported in C and can be used to communicate with device without downloading another headers but which? Can somebody give me some sample how to do it? MikroTik wiki include md5.c +.h and mikrotik-api.c+h which i need for communication and how to do a client? Thanks very much to all which can help me.

you will have to look for this information on programming forums as all MikroTik is providing is protocol how to talk to API in RouterOS.

Yes all the night i trying to do it and bingo opened socket with succesfull connect. But problem is that I dont understand the communication in it there is a code of mine:

**#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <windowsx.h>
#include <commctrl.h>
#include <tchar.h>
#include <string.h>
#include <winsock.h>
#include “md5.h”
#pragma comment (lib, “Ws2_32.lib”)

#define DEFAULT_BUFLEN 512


void WriteWord(char *strWord,int slot)
{
int iResult = send(slot,strWord,(int)strlen(strWord),0);
}

void main()
{

struct sockaddr_in address;
int connectResult;
int addressSize;
int fdSock;
char *strIpAddress = “192.168.1.1”;
int port = 8728;

WORD wVersionRequested = MAKEWORD(2,2);
WSADATA wsaData;
WSAStartup(wVersionRequested, &wsaData);

fdSock = socket(AF_INET, SOCK_STREAM,0);

address.sin_family = AF_INET;
address.sin_addr.s_addr = inet_addr(strIpAddress);
address.sin_port = htons(port);
addressSize = sizeof(address);
printf(“configuration : %s, %d, %d\n”,strIpAddress,port,fdSock);
printf(“Connecting to %s\n”, strIpAddress);

connectResult = connect(fdSock, (struct sockaddr *)&address, addressSize);

if(connectResult==-1) {
perror (“Connection problem”);
printf(“Closing socket\n”);
closesocket(fdSock);
fdSock = -1;
} else {
printf(“Successfully connected to %s\n”, strIpAddress);
}
system(“pause”);
char tmp[DEFAULT_BUFLEN]=“”;
char *buffsend = “/login”;

WriteWord(buffsend,fdSock);
WriteWord(“\x00”,fdSock);
recv(fdSock,tmp,DEFAULT_BUFLEN,0);
printf(“%s”,tmp);
system(“pause”);
}**

And I need an exaple how to send data to router and receive data from router because maybe it send but nothing happed or there is another converting fail could you help me? Or give me some exaple? Thx

Check this
http://wiki.mikrotik.com/wiki/API_in_C

Hi,
so i have tried to send a message like this as I saw it in manual:


**void WriteWord(int slot,char *strWord)
{
char *iLen = (char )malloc(strlen(strWord));

if ((int)strlen(strWord)<0x80)
send(slot,iLen,(int)strlen(strWord),0);
}

char buffsend = “/login”;

WriteWord(fdSock,buffsend);
WriteWord(fdSock,“”);
recv(fdSock,&cFirstChar,1,0);
printf(“byte1 = %#x\n”, cFirstChar) ;

It returns me 0xFFFFFFCC and its not right in my oppinion is this good?

Hi again,
I used a code from C from manual and seems to work. Of course it was hell to rewrite it to form for compiling and using on windows platform but everything is now ok BUT! I am not so good in C and need your help. There is a code with problem :

void addSentenceToBlock(struct Block *stBlock, struct Sentence *stSentence)
{
int iNewLength;
iNewLength = stBlock->iLength + 1;

DEBUG ? printf(“addSentenceToBlock iNewLength=%d\n”, iNewLength) : 0;

// allocate mem for the new Sentence position
if (stBlock->iLength == 0)
{
stBlock->stSentence = malloc(1 * sizeof stBlock->stSentence);
}
else
{
stBlock->stSentence = realloc(stBlock->stSentence, iNewLength * sizeof stBlock->stSentence + 1);
}


// allocate mem for the full sentence struct
stBlock->stSentence[stBlock->iLength] = malloc(sizeof *stSentence);

// copy actual sentence struct to the block position
memcpy(stBlock->stSentence[stBlock->iLength], stSentence, sizeof *stSentence);

// update iLength
stBlock->iLength = iNewLength;

DEBUG ? printf(“addSentenceToBlock stBlock->iLength=%d\n”, stBlock->iLength) : 0;
}

now u see the problem which is that i NEED to retype a malloc function which returns void* to Sentence ** but dont know HOW. This code is from manual in C so u can see a structure of Block. Could someone help me with this? Thx.

Hi Joziff, Have you managed to finish your project? I am working on the same project as you. Could you please send me your code? I don’t know how to rewrite the sample code to the form for compiling and using on windows platform. I would really appreciate your help.

Hi,
We can discuss and also I can send it to you but I am in the U.S. right now in Iowa and 18.9. I will return back to Czech Republic so I can send it. Its because I have it on another laptop.