I am trying to create a dll from a simple C program to be used with a nim application. I tried using visual c++ compiler and gcc(mingw), but the I couldn't load the resulting dll using dynlib. I have successfully used the same nim function with various other dlls. Is there some special flags to be used while compiling the C program? Any help regarding this would be great.
var liblibfoo = loadLib("hello_nim.dll")
if liblibfoo != nil :
echo("loaded")
else:
echo("failed")
And here is the c program
#include <stdlib.h>
#include <stdio.h>
#include "hello_nim.h"
void print_buffer(buffer_ptr buf) {
int i;
for(i=0;i<buf->fill;i++) {
printf("%02x ",buf->data[i]);
}
printf("\n");
}
int hello_1(char *out, int outlen, const char *in, int inlen){
buffer_t inbuf;
buffer_t outbuf;
inbuf.data = in;
inbuf.size = inlen;
inbuf.fill = inlen;
outbuf.data = out;
outbuf.size = outlen;
outbuf.fill = 0;
return hello_2(&outbuf,&inbuf);
}
int hello_2(buffer_ptr out, const buffer_ptr in) {
int i;
printf("Received :");
print_buffer(in);
if(out->size<in->fill) {
fprintf(stderr,"Output buffer too small.\n");
return 2;
}
char *ptr = malloc(in->fill*sizeof(char));
if(NULL==ptr) {
fprintf(stderr,"Allocation of memory failed");
return 1;
}
for(i=0;i<in->fill;i++) {
ptr[i] = in->data[i];
}
out->fill = in->fill;
for(i=0;i<in->fill;i++) {
out->data[i] = ptr[i];
}
printf("Return :");
print_buffer(out);
free(ptr);
}