Changing a char pointer via DLL

This is a discussion on Changing a char pointer via DLL within the C Programming forums, part of the General Programming Boards category; I would like to pass 2 char pointers to my DLL (input_buffer,output_buffer) and obtain the output in my API via ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    7

    Changing a char pointer via DLL

    I would like to pass 2 char pointers to my DLL (input_buffer,output_buffer) and obtain the output in my API via the output buffer.

    Is this doable ? My working sample code below tells me otherwise. Is writing the output buffer to a file the only option.

    Source code
    ---------------
    [code]
    /* filename: mock_test.c */
    #include <stdio.h>
    char *var1="Hello World";
    int main()
    {
    int err = 0;
    printf ("Original value of var1 : %s\n", var1);
    err = DLL_API (var1);
    printf ("Altered value of var1 : %s\n", var1);
    return err;
    }
    [\code]

    DLL:
    Code:
    /* Mock DLL to modify a char array */
    /* filename: mock_dll.c*/
    #include <stdio.h>
    
    char *text="Ah.. Hey! there";
    
    int
    DLL_API(var1)
    {
      var1=text;
    
      printf ("DLL: var1 : %s\n", var1);
    
      return 0;
    }
    Output:
    ---------
    $ gcc -c mock_dll.c -g && gcc -Wall -shared -g -o mock_dll.dll mock_dll.o && gc
    c -o mock_test mock_test.c -g -L ./ -lmock_dll

    mock_dll.c: In function `DLL_API':

    mock_dll.c:9: warning: assignment makes integer from pointer without a cast

    $ ./mock_test
    Original value of var1 : Hello World
    DLL: var1 : Ah.. Hey! there
    Altered value of var1 : Hello World

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    To change anything, whehter it is a pointer, float, int, or some struct [1], you need to pass the address of it, so that the called function can store something back into the original variable.

    As your code works now, you pass var1 to the function, which gets it's OWN COPY of var1 (called var1, just so that we can confuse everyone to think that it's the same variable - but it isn't). You then change the function's var1. But when you get back, the copy has been lost (as you leave the function). The above solution with passing the address will solve that problem [And of course, it has absolutely nothing to do with DLL's - it would be exactly the same problem if the function was in the same file or linked statically to the executable].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    7
    Quote Originally Posted by matsp View Post
    The above solution with passing the address will solve that problem [And of course, it has absolutely nothing to do with DLL's - it would be exactly the same problem if the function was in the same file or linked statically to the executable].

    --
    Mats
    Thanks Mats. However, passing the address as "&var1" did not work either. This is a version that actually works

    Solution:
    Code:
    /* file: mock_test.c */
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    /*
    extern void changeBuff(char *, char *);
    */
    
    int main() {
           char *buffer;
    
           buffer = malloc (sizeof(char)*4);
           strcpy(buffer,"abc");
    
           printf ("original buffer value: %p %s\n",buffer,buffer);
           changeBuff(buffer,"DEFG");
           printf ("buffer value has beed changed to: %s\n",buffer,buffer); 
    }
    
    /* file: mock_dll.c */
    #include <stdio.h>
    #include <string.h>
    
    void changeBuff(char *buf, const char* newbuf) {
           printf ("changing buffer now %p %s\n",buf, buf);
           buf = realloc(buf, sizeof(char) * strlen(newbuf));
           strcpy (buf,newbuf);
    }

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You aren't understanding. Pointers are just like every other variable. They store a value. To keep changes made to a passed variable, you need a pointer to that variable, not just the variable itself.

    To change an int in a function, you need to have the function receive a pointer to the int. Not the int itself.
    Code:
    void fun1( int no );
    void fun2( int *yes );
    To change a pointer (the address the pointer holds) in a function so that it's updated outside of the function, you need to pass a pointer to the pointer. Not he pointer itself.
    Code:
    void fun1( int *no );
    void fun2( int **yes );

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Mar 2009
    Posts
    7
    Quote Originally Posted by quzah View Post
    To change a pointer (the address the pointer holds) in a function so that it's updated outside of the function, you need to pass a pointer to the pointer. Not he pointer itself.
    Code:
    void fun1( int *no );
    void fun2( int **yes );
    Quzah.
    Agreed. Even with a pointer to pointer referencing this original code does not succeed.

    Do you have an example of a working code that proves this concept? Other than the solution posted in my second comment..

  6. #6
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Quote Originally Posted by rockpaandi View Post
    Agreed. Even with a pointer to pointer referencing this original code does not succeed.
    show your attempt and we point to your errors
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 10:42 PM
  2. code condensing
    By bcianfrocca in forum C++ Programming
    Replies: 4
    Last Post: 09-07-2005, 10:22 AM
  3. Could somebody please help me with this C program
    By brett73 in forum C Programming
    Replies: 6
    Last Post: 11-25-2004, 02:19 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 08:48 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21