Pointer references corrupted via DLL (Cygwin)
All, Here is a working sample code of my problem area with my DLL application. I am so close to finish, except that I am required to provide a string buffer back to the calling function.
However, the DLL seems to obtain a memcpy of this string and not the pointer address. Please let me know if I should be doing anything different in passing this pointer.
Thanks,
Sriram
Code:
Source code for DLL
-----
$ cat -n mock_DLL.c
1 /* Mock DLL to modify a char array */
2 #include <stdio.h>
3
4 int
5 DLL_API( char *var1)
6 {
7
8 printf ("Inside DLL: Address of var1 %u",&var1);
9
10 return 0;
11 }
12$ gcc -o mock_test mock_test.c -L./ -lmock_dll
$ gcc -shared -o mock_dll.dll mock_dll.o
Source code for test file
-----
$ cat -n mock_test.c
1 #include <stdio.h>
2 static char *var1 = "Foobar";
3 int main()
4 {
5 printf ("Address of var1 : %u\n", &var1);
6 return DLL_API (var1);
7 }
$ gcc -o mock_test mock_test.c -L./ -lmock_dll
$ ./mock_test
Address of var1 : 4206592
Inside DLL: Address of var1 2280640
$
Changing value at pointer
Quote:
Originally Posted by
brewbuck
What you were doing is printing the address of the pointer, not the pointer itself. When you pass the pointer as a parameter, clearly the address of where this pointer exists is going to be different. The pointer itself (the address it points to) should be the same.
Do free or malloc operations on this address affect the original pointer. For instance:
Code:
$ cat -n mock_DLL.c
1 /* Mock DLL to modify a char array */
2 #include <stdio.h>
3
4 const char *text="Hi! there";
5
6 int
7 DLL_API( char *var1)
8 {
9 printf ("Inside DLL: Address of var1 %p\n",var1);
10
11 var1 = text;
12 return 0;
13 }
$ cat -n mock_test.c
1 #include <stdio.h>
2 static char *var1 = "Foobar";
3 int main()
4 {
5 int err = 0;
6
7 printf ("Address of var1 : %p\n", var1);
8 err = DLL_API (var1);
9
10 printf ("Altered value of var1 : %s\n", var1);
11
12 return err;
13 }
If not, how can I change the value referenced by the pointer in my DLL.