如何在c中返回这个缓冲区值?

如何在c中返回这个缓冲区值?,c,C,假设我有两个dll。第一个,我从openbsd.org获得了关于使用strlcpy函数避免缓冲区溢出的代码,第二个是再次使用gtk-glib-2.0 dll导出函数避免缓冲区溢出“g_snprintf”,而不是标准调用c函数snprintf。当我想要返回缓冲区字符串值时,问题就出现了,这样我就可以将此代码设置为dll,并轻松地从python语言调用这两个函数。以下是片段: #include <stdio.h> #include "dlfcn.h" #include <sys/t

假设我有两个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);
} 
#包括
#包括“dlfcn.h”
#包括
#包括
#定义库“strlcpy.dll”
#定义func“strlcpy”
#定义lib2“libglib-2.0-0.dll”
#定义功能2“g_snprintf”
char*returnMsg(char*buff,无符号长n,char*msg)
{
int(*g_snprintf)(字符*字符串,无符号长n,字符常量*格式,字符*消息);
无效*手柄2;
国际恐怖主义;
handle2=dlopen(lib2,RTLD_);
如果(!handle2){
printf(“\n打开第二个dll时出错\n”);
返回1;
printf(“在第二个dll获得它:%p”,handle2);
g_snprintf=dlsym(手柄2,功能2);
如果(!g_snprintf)
printf(“获取g_snprintf符号时出错”);
返回1;
如果((errorno=g_snprintf(buff,n,“%s”,msg))!=0)
printf(“错误不能使用g_snprintf..”;
返回1;
dlclose(handle2);
返回buff;
}
int main()
{
char iv[32];
char*msg,*l;
int k;
无符号整数g;
字符buff[16];
大小(*strlcpy)(字符*dst,常量字符*src,大小大小);
无效*手柄;
handle=dlopen(lib,RTLD_-LAZY);
如果(!句柄){
printf(“错误无法打开库…”);
返回1;
}
printf(“在%p\n打开dll”,句柄);
strlcpy=dlsym(句柄,函数);
if(!strlcpy){
printf(“错误无法找到所需的导出函数…”);
返回1;
}
printf(“得到它,strlcpy函数位于%p\n”,strlcpy);
g=16;
msg=“这是男孩!”;
memset(iv,0,sizeof(iv));
strlcpy(iv,msg,sizeof(iv));
对于(k=0;k
男孩,这不是python。你需要括号
{}


孩子,这不是python。你需要括号
{}

哦,那是我的错。那么如何使用你瞄准的指针呢?哦,那是我的错。那么如何使用你瞄准的指针呢?
if ((errorno= g_snprintf(buff, n, "%s",msg)) !=0)
    printf("error cannot use g_snprintf..");
    return 1;