#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;
}
问问题
223 次
3 回答
3
问题之一在于:
msg = (char*)malloc(512*sizeof(char));
sign = (char*)malloc(512*sizeof(char));
...
msg = "Test of MSG";
sign = "Test of Sign";
于 2013-08-29T14:49:28.653 回答
1
如果 gcc 告诉您它已转储核心,请查看它以找出问题所在:
gdb <program> <core>
如果仍有问题,请发布输出。
于 2013-08-29T15:01:47.170 回答
1
这段代码有很多问题,没有人对它的段错误感到惊讶。
让我们开始清理这堆热气腾腾的粪便:
main();
Main 应该是int main(void)
,并且后面不应该有分号。
不要强制转换的返回值
malloc
。用于
sizeof()
确保您分配了适当的空间量:
改变这个:
int *x = (int*)malloc(8);
对此:
int *x = malloc(sizeof(int));
这不是您在 C 中进行字符串赋值的方式:
msg = "Test of MSG";
sign = "Test of Sign";
相反,请执行以下操作:
strcpy(msg, "Test of MSG");
strcpy(sign, "Test of Sign");
注意: strcpy有自己的问题,首选strcpy_s。而是一步一个脚印。
于 2013-08-29T15:03:57.587 回答