Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
GNUC编程:调用另一个C dll函数时出错(分段错误(内核转储))_C - Fatal编程技术网

GNUC编程:调用另一个C dll函数时出错(分段错误(内核转储))

GNUC编程:调用另一个C dll函数时出错(分段错误(内核转储)),c,C,问题之一在于: #include <stdio.h>; #include <windows.h>; #include <malloc.h>; typedef int (__cdecl *MYPROC)( void *epcs, char *message, char *sign,unsigned int *sig_len ); main(); { HINSTANCE hh; MYPROC hhLib; void *j; int

问题之一在于:

#include <stdio.h>;
#include <windows.h>;
#include <malloc.h>;
typedef int (__cdecl *MYPROC)( void *epcs, char *message, char *sign,unsigned int *sig_len );
main();
{  
  HINSTANCE hh;  
  MYPROC hhLib;  
  void *j;  
  int *x = (int*)malloc(8);  
  unsigned int *y = (unsigned int*)malloc(8);  
  int *z = (int*)malloc(8);  
  char *msg, *sign ;  
  msg = (char*)malloc(512*sizeof(char));  
  sign = (char*)malloc(512*sizeof(char));  
  *x = 2541;  
  *y = 10;  
  *z = 0;  
  j = (int*)*x;  
  msg = "Test of MSG";  
  sign = "Test of Sign";  
  hh = LoadLibrary("epcs.dll");  
  if (hh == NULL)   {    
    printf("Unable to load epcs shared library\n");    
    return 0;  
  }  
  hhLib = (MYPROC)GetProcAddress(hh, "epcs_test");  
  if (hhLib==NULL)   {     
    printf("Unable to point shared library function (epcs_test).. \n");     
    return 0;  
  }  
  z = hhLib(j, msg, sign, y);  
  printf("%d \n",*x);  
  printf("%d \n",j);  
  printf("%d \n",*y); 
  printf("%d \n",*z);
  printf("%s \n",msg);
  printf("%s \n",sign);
  return 0;
}

如果gcc告诉您它转储了core,只需查看一下就可以发现出了什么问题:

gdb


如果仍然存在问题,请发布输出。

此代码有太多问题,没有人会对它的seg故障感到惊讶

让我们开始清理这堆热气腾腾的粪便:

Main应该是int mainvoid,后面不应该有分号。 . 使用sizeof确保分配了适当的空间:

更改此项:

main();
为此:

int *x = (int*)malloc(8);  
这不是在C中执行字符串赋值的方式:

int *x = malloc(sizeof(int));
相反,要:

msg = "Test of MSG";  
sign = "Test of Sign"; 

注意:strcpy有它自己的问题,strcpy_是首选。但是一步一个脚印。

欢迎来到StackOverflow。请改进您问题中的代码格式,请参阅以获取帮助。这是的应用程序吗?我发现主herzallerliebst后面有分号。malloc的结果不应被强制转换。+1用于总结我的评论和回答。。。我甚至没注意到你说了同样的话。你是第一位,真是太好了。我将继续讨论void*j的混乱用法,并在程序失败后返回0…感谢您的回复。我已经按照上述建议更正了我的代码,但仍然得到了段错误。据我所知,此错误与调用外部DLL函数有关。请参阅下面的代码行…[z=int*hhLibj,msg,sign,y;]在这行代码中,外部DLL函数将尝试修改sign和y参数…这是错误的原因…我猜..请解释2个c程序如何共享内存或内存位置,以便在需要时读取/写入变量!!!我已经尝试了上面的命令,但在Cygwin终端中它似乎不起作用。。无论如何,请查看此处的转储异常:eip=6B681C95 eax=000009ED ebx=002AC8C ecx=7C917DE9 edx=7C97B178 esi=612A7428 edi=002CDB4 ebp=002AC18 esp=002ABD0 program=D:\home\sumit\sumit\test.exe,pid 5544,thread Main编译时是否启用了调试信息?您必须将-g开关传递给gcc。
msg = "Test of MSG";  
sign = "Test of Sign"; 
strcpy(msg, "Test of MSG");
strcpy(sign, "Test of Sign");