假设我有2个dll。第一个,我从openbsd.org获取这个代码,关于使用strlcpy函数来避免缓冲区溢出,第二个是再次使用gtk-glib-2.0 dll导出函数来避免缓冲区溢出“g_snprintf”而不是标准调用c函数snprintf。当我想要返回缓冲区字符串值时出现问题,这样我就可以使这段代码成为dll并轻松地从python语言中调用这两个函数。这是片段:
#include <stdio.h>
#include "dlfcn.h"
#include <sys/types.h>
#include <string.h>
#define lib "strlcpy.dll"
#define func "strlcpy"
#define lib2 "libglib-2.0-0.dll"
#define func2 "g_snprintf"
char* returnMsg(char *buff, unsigned long n, char *msg)
{
int (*g_snprintf)(char *string, unsigned long n, char const *format,char *msg);
void *handle2;
int errorno;
handle2 = dlopen(lib2, RTLD_LAZY);
if (!handle2){
printf("\nerror opening second dll\n");
return 1;
printf("got it at second dll: %p",handle2);
g_snprintf = dlsym(handle2, func2);
if (!g_snprintf)
printf("error getting g_snprintf symbol..");
return 1;
if ((errorno= g_snprintf(buff, n, "%s",msg)) !=0)
printf("error cannot use g_snprintf..");
return 1;
dlclose(handle2);
return buff;
}
int main()
{
char iv[32];
char *msg,*l;
int k;
unsigned int g;
char buff[16];
size_t (*strlcpy)(char *dst, const char *src, size_t siz);
void *handle;
handle = dlopen (lib, RTLD_LAZY);
if (!handle) {
printf("error cannot open library..");
return 1;
}
printf("opening dll at %p\n", handle);
strlcpy = dlsym(handle, func);
if (!strlcpy) {
printf("error cannot find desired exported function..");
return 1;
}
printf("got it, strlcpy function at %p\n",strlcpy);
g = 16;
msg = "this is messages boy!";
memset(iv,0,sizeof(iv));
strlcpy(iv,msg,sizeof(iv));
for(k=0;k<strlen(msg);k++){
printf("%c",iv[k]);
}
printf("\n%ul",g);
printf(" and %d",sizeof(iv));
l = returnMsg(buff, sizeof(buff),msg);
printf("%s",l);
dlclose(handle);
}
答案 0 :(得分:3)
if ((errorno= g_snprintf(buff, n, "%s",msg)) !=0)
printf("error cannot use g_snprintf..");
return 1;
男孩,这不是python。您需要括号{}
。