-11
#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;
}
Peter Miehle
  • 5,984
  • 2
  • 38
  • 55

3 Answers3

3

one of the problem lies here:

msg = (char*)malloc(512*sizeof(char));  
sign = (char*)malloc(512*sizeof(char));  
...
msg = "Test of MSG";  
sign = "Test of Sign";  
Peter Miehle
  • 5,984
  • 2
  • 38
  • 55
1

If gcc tells you it dumped core, just have a look at it to find out what went wrong:

gdb <program> <core>

If you still have problems, post the output.

Axel
  • 13,939
  • 5
  • 50
  • 79
  • i have tried the above command but it seems non functional in Cygwin terminal.. anyway take a look of dump here – user2729405 Sep 02 '13 at 09:12
  • Exception: STATUS_ACCESS_VIOLATION at eip=6B681C95 eax=000009ED ebx=0022AC8C ecx=7C917DE9 edx=7C97B178 esi=612A7428 edi=0022CDB4 ebp=0022AC18 esp=0022ABD0 program=D:\home\sumit\sumit\test.exe, pid 5544, thread main – user2729405 Sep 02 '13 at 09:13
  • Did you enable debug infos when compiling? (You have to pass the `-g` switch to gcc.) – Axel Sep 02 '13 at 10:23
1

This code has sooo many problems, no one is surprised that it seg-faults.

Lets begin cleaning up this steaming pile of excrement:


main();

Main should be int main(void), and should not have a semi-colon after.


Don't cast the return value of malloc.
Use sizeof() to make sure you're allocating the proper amount of space:

Change this:

int *x = (int*)malloc(8);  

to this:

int *x = malloc(sizeof(int));


This is not how you do string assignments in C:
msg = "Test of MSG";  
sign = "Test of Sign"; 

Instead, do:

strcpy(msg, "Test of MSG");
strcpy(sign, "Test of Sign");

Note: strcpy has its own problems, and strcpy_s is preferred. But one step at a time.

Community
  • 1
  • 1
abelenky
  • 63,815
  • 23
  • 109
  • 159
  • 1
    +1 for summing up my comments and answers – Peter Miehle Aug 29 '13 at 15:14
  • Heh... I didn't even notice you said the same thing spread out. Kudos to you for being first. I was going to go on about the confusing use of `void* j`, and returning 0 after the program fails.... – abelenky Aug 29 '13 at 15:16
  • Thanks for reply. i have corrected my code as per above suggestion but still getting segment fault error. as per my understanding this error is related while calling to the external DLL function. refer below code line...[z = (int*)hhLib(j, msg, sign, y);] in this line of code external DLL function will try to modify the sign and y parameter...which is the reason of error...i guess.. – user2729405 Sep 02 '13 at 11:21
  • can you please explain how the 2 c programs share memory or memory location to read/write the variables if needed!!! – user2729405 Sep 02 '13 at 11:24