linux: call functions from a .so file lazarus / 8-Other ---------------------------------------------------------------------- kristof_v · 2006-11-21 08:59 hi all, say you have a simple .so file (the equivalent of a .dll in windows) How can you call the functions of that .so file in free pascal? can you please give a short example if possible? thx! ---------------------------------------------------------------------- SergKam · 2006-11-22 15:28 function mysystem(_para1:Pchar):longint;cdecl;external 'libc' name 'system'; mysystem('echo test'); it run function "system" in libc.so ---------------------------------------------------------------------- kristofv · 2006-12-28 14:52 thx a lot! this certainly got me going. i am now trying to call some functions from libpcap. this is the code that i have now: Code: [Select] unit Unit1; interface uses   Classes, SysUtils;     function lookupdevice(_para1:Pchar):Pchar;cdecl;external 'libpcap' name 'pcap_lookupdev'; implementation var   dev: Pchar;   errbuf: array[0..50] of char; begin   //get device name   dev := lookupdevice(errbuf);   WriteLn('DEVICE: ' + dev); end.                 this works great! but i want to take it a step further and actually grab a packet. this is the libpap function to capture a packet: Code: [Select] pcap_t *pcap_open_live(char *device,int snaplen, int prmisc,int to_ms,        char *ebuf)        snaplen - maximum size of packets to capture in bytes        promisc - set card in promiscuous mode?        to_ms   - time to wait for packets in miliseconds before read        times out        errbuf  - if something happens, place error string here then in C you would call it like this: Code: [Select] descr = pcap_open_live(dev,BUFSIZ,0,-1,errbuf); but i don't have a clue how to do this in free pascal, with the type of variables and everything. like what is pcap_t* descr in free pascal?? I would appreciate it VERY MUCH if somebody can help me on my way here! below i included a complete C example of capturing a packet using libpcap Code: [Select] #include #include #include /* if this gives you an error try pcap/pcap.h */ #include #include #include #include #include /* includes net/ethernet.h */ int main(int argc, char **argv) {     int i;     char *dev;     char errbuf[PCAP_ERRBUF_SIZE];     pcap_t* descr;     const u_char *packet;     struct pcap_pkthdr hdr;     /* pcap.h */     struct ether_header *eptr;  /* net/ethernet.h */     u_char *ptr; /* printing out hardware header info */     /* grab a device to peak into... */     dev = pcap_lookupdev(errbuf);     if(dev == NULL)     {         printf("%s\n",errbuf);         exit(1);     }     printf("DEV: %s\n",dev);     /* open the device for sniffing.        pcap_t *pcap_open_live(char *device,int snaplen, int prmisc,int to_ms,        char *ebuf)        snaplen - maximum size of packets to capture in bytes        promisc - set card in promiscuous mode?        to_ms   - time to wait for packets in miliseconds before read        times out        errbuf  - if something happens, place error string here        Note if you change "prmisc" param to anything other than zero, you will        get all packets your device sees, whether they are intendeed for you or        not!! Be sure you know the rules of the network you are running on        before you set your card in promiscuous mode!!     */     descr = pcap_open_live(dev,BUFSIZ,0,-1,errbuf);     if(descr == NULL)     {         printf("pcap_open_live(): %s\n",errbuf);         exit(1);     }     /*        grab a packet from descr (yay!)                          u_char *pcap_next(pcap_t *p,struct pcap_pkthdr *h)        so just pass in the descriptor we got from                our call to pcap_open_live and an allocated              struct pcap_pkthdr                                 */     packet = pcap_next(descr,&hdr);     if(packet == NULL)     {/* dinna work *sob* */         printf("Didn't grab packet\n");         exit(1);     }     /*  struct pcap_pkthdr {         struct timeval ts;   time stamp         bpf_u_int32 caplen;  length of portion present         bpf_u_int32;         lebgth this packet (off wire)         }      */     printf("Grabbed packet of length %d\n",hdr.len);     printf("Recieved at ..... %s\n",ctime((const time_t*)&hdr.ts.tv_sec));     printf("Ethernet address length is %d\n",ETHER_HDR_LEN);     /* lets start with the ether header... */     eptr = (struct ether_header *) packet;     /* Do a couple of checks to see what packet type we have..*/     if (ntohs (eptr->ether_type) == ETHERTYPE_IP)     {         printf("Ethernet type hex:%x dec:%d is an IP packet\n",                 ntohs(eptr->ether_type),                 ntohs(eptr->ether_type));     }else  if (ntohs (eptr->ether_type) == ETHERTYPE_ARP)     {         printf("Ethernet type hex:%x dec:%d is an ARP packet\n",                 ntohs(eptr->ether_type),                 ntohs(eptr->ether_type));     }else {         printf("Ethernet type %x not IP", ntohs(eptr->ether_type));         exit(1);     } grtz ---------------------------------------------------------------------- SergKam · 2006-12-29 11:14 all you need is h2pp or h2pas program (read manual). It convert C header *.h to *.pas. see into result and . or in short all   char *     in C is   PChar in pascal all ather blabla* is Pointer to some records(struct in C); balbal * a; is var  a1:^TBlabla; or  a:Pointer; ---------------------------------------------------------------------- kristofv · 2006-12-30 21:58 thx SergKam, but i don't know which .h file i have to convert to a .pp file to grab a package using libpcap. i call functions from libpcap.so like this: Code: [Select]function lookupdevice(_para1:Pchar):Pchar;cdecl;external 'libpcap' name 'pcap_lookupdev' which c header file do i have to convert to a pascal unit and how can i grab a packet from the generated pascal file? a simple example would be of invaluable help. thx!! * i transformed pcap.h into pcap.pp with h2pas and now i have this function declaration: Code: [Select]function pcap_open_live(_para1:Pchar; _para2:longint; _para3:longint; _para4:longint; _para5:Pchar):^pcap_t; so the function pcap_open_live returns a value of type ^pcap_t. now can i make a variable of type ^pcap_t in free pascal and how? also, do i have to continue to work with the libpcap.so or do i need to use the .pp file from now on? grtz ---------------------------------------------------------------------- SergKam · 2007-01-05 17:04 Quote * i transformed pcap.h into pcap.pp with h2pas and now i have this function declaration: Code: [Select]function pcap_open_live(_para1:Pchar; _para2:longint; _para3:longint; _para4:longint; _para5:Pchar):^pcap_t; so the function pcap_open_live returns a value of type ^pcap_t. now can i make a variable of type ^pcap_t in free pascal and how? also, do i have to continue to work with the libpcap.so or do i need to use the .pp file from now on? grtz pcap.pp it is all. var a:^pcap_t; begin      a:=pcap_open_live(... params...); end; in C a->field in Psacal a^.field ---------------------------------------------------------------------- Almindor · 2007-01-05 22:54 You can get a full working pcap pascal header here: http://members.chello.sk/ales/pcap.pp which reminds me that I wanted to add it to packages once...