LPVOID to char *

This is a discussion on LPVOID to char * within the C++ Programming forums, part of the General Programming Boards category; Howdy, I have the following code that takes an LPVOID* variable as an argument. Code: #include <iostream> #include <windows.h> using ...

  1. #1
    Banned nickname_changed's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    986

    LPVOID to char *

    Howdy,

    I have the following code that takes an LPVOID* variable as an argument.

    Code:
    #include <iostream>
    #include <windows.h>
    
    using namespace std;
    
    int Foo(LPVOID *Bar)
    {
        *Bar = new char[100];
    
        *Bar = "Hello";
        cout << "Bar: " << (char *)*Bar << endl;
        return 0;
    }
    
    int main()
    {
        char * Word = "bye";
        Foo((LPVOID *)&Word);
    
        cout << "Word: " << Word << endl; 
        return 0;
    }
    The objective of this code is to set main()'s Word from 'bye' to 'Hello' using the Foo() function. Its working fine, but theres a problem.

    Foo() is actually a function that will be loaded from a DLL. In the code above it is declared as "Foo(LPVOID * Bar)", but in the DLL it is "Foo(LPVOID Bar)".

    Somehow I need to change the code for Foo so that it can take an LPVOID and change its contents. My understanding of pointers is very poor and so thats why I need help. If anyone can tell me what needs to be done or help me understand the process, I would be most grateful!

  2. #2
    *******argv[] - hu? darksaidin's Avatar
    Join Date
    Jul 2003
    Posts
    314

    Re: LPVOID to char *

    Code:
    int Foo(LPVOID *Bar)
    {
        *Bar = new char[100];
    
        *Bar = "Hello";
        cout << "Bar: " << (char *)*Bar << endl;
        return 0;
    }
    LPVOID is already a pointer. So by using (LPVOID*) you are working with a pointer to a void pointer. Is this what you wanted to do ?

    Next thing: In the above code ( *Bar= new char[100] ) you alloc 100 bytes and fill it with a cstr in the next line. So why does Foo take *Bar as a parameter at all if it's not used?

    Out of curiosity: Is the DLL you are talking about a DX DLL?
    Last edited by darksaidin; 10-01-2003 at 03:14 AM.
    [code]

    your code here....

    [/code]

  3. #3
    Banned nickname_changed's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    986
    I know LPVOID is already a pointer, and I don't want it to be LPVOID *, just LPVOID.

    All I know is LPVOID* works (when run like this example) but doesn't work with the DLL (because it expects the argument to be LPVOID. Due to my limited pointer understanding I can't figure this out on my own.

    The DLL is an ISAPI DLL, and Foo(LPVOID Bar) is a dumbed-down version of a function pointer I put into a structure to send as information to the DLL. I didn't want to go into details because as I learned from experience, people here don't seem to know much about ISAPI.

    All I need is a way to change the value of Word in main using the function
    Foo(LPVOID Bar).

    I know its possible somehow because Bar is a void *. I also read somewhere that before assigning to a void * you must convert it to another type (char * in this case). Can you help?

    Edit: The Foo() functions job is to set the value of a char * or char str[] to a different string ('Hello' in this case).
    Last edited by nickname_changed; 10-01-2003 at 03:23 AM.

  4. #4
    lyx
    lyx is offline
    Amateur
    Join Date
    Sep 2003
    Posts
    228
    I think you should be doing more works on the language itself rather than trying to do more complex things like your ISAPI thing.

    For your question. Well, you need first not to pass a void * but a char *. Secondly, there are two types of modifying functions:
    1. Ones like you built.
    2. Ones you wnat to do.

    The main difference is that, in the first ones, you allocate a new char [] and in the second, you change an existing char []. To change the char [], use the strcpy() function. But the buffer must be huge enough to hold the entire string you're going to put in.

  5. #5
    Banned nickname_changed's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    986
    Thanks lyx, I actually consider myself to be very strong with C++, pointers and some very advanced things are my only weakness.

    For your question. Well, you need first not to pass a void * but a char *. Secondly, there are two types of modifying functions:
    If by this you mean making the function:
    bool Foo(char * Bar)
    I can't do that, the type of Bar MUST be LPVOID (its part of the DLL).

    The main difference is that, in the first ones, you allocate a new char [] and in the second, you change an existing char [].
    But it doesn't exist as a char * Unless I should do something like this:
    strcpy ((char *) Bar, "hello");

    Using this, I've managed to make this, which is much more what I want:
    Code:
    int Foo(LPVOID Bar)
    {
        //*Bar = new char[100];
    
        strcpy((char *)Bar , "Hello");
        cout << "Bar: " << (char *)Bar << endl;
        return 0;
    }
    
    int main()
    {
        char Word[10] = "bye";
        Foo(&Word);
    
        cout << "Word: " << Word << endl; 
        return 0;
    }
    This sets Word to 'Hello' perfectly. The trouble is, when
    char Word[10] = "bye";
    is changed to
    char * Word = "bye";
    I get illegal operations everywhere. I'm not sure how the DLL sends strings (I assume they would be the first way though, because thats safer.. right?), so I would like to be able to handle both.

  6. #6
    lyx
    lyx is offline
    Amateur
    Join Date
    Sep 2003
    Posts
    228
    Thanks lyx, I actually consider myself to be very strong with C++, pointers and some very advanced things are my only weakness.
    Then, I must be "stronger than all". (just a joke, it's the song I'm listening to ^^) Actually I have weaknesses too, for example, I could never figure out how to declare a friend template function inside of my class, maybe do you know? (besides, using MSVC6 for a long time, I couldn't experience that many things with templates)

    In fact, I know some stuffs about the language, that's all. ^^

    Just to know, you haven't done C before your C++ courses, have you? And what are your weaknesses anyway? ^^

    I know, I'm a very curious boy.

    This sets Word to 'Hello' perfectly. The trouble is, when
    char Word[10] = "bye";
    is changed to
    char * Word = "bye";
    I get illegal operations everywhere. I'm not sure how the DLL sends strings (I assume they would be the first way though, because thats safer.. right?), so I would like to be able to handle both.
    DLL use of strings depends on the language you're using to compile and call the DLL.
    If you get illegal operations it is because:
    Code:
    char word[10] = "bye";  // creates an array of 10 chars then fill it with the sequence "bye"
    char *word = "bye";  // creates an array of 4 chars and fill it with the string, then makes the pointer points to it
    
    // conclusion: the second buffer isn't big enough to hold the string "hello"

  7. #7
    Banned nickname_changed's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    986
    Then, I must be "stronger than all". (just a joke, it's the song I'm listening to ^^) Actually I have weaknesses too, for example, I could never figure out how to declare a friend template function inside of my class, maybe do you know? (besides, using MSVC6 for a long time, I couldn't experience that many things with templates)
    Hehe nope...

    Just to know, you haven't done C before your C++ courses, have you?
    Unfortunatly im self taught for 3 years on C++, no formal C/C++ couses, which is why my pointer understanding is very limited (if none at all). I was told its a bad idea to learn C and then C++, and easier to learn C++ on its own (you don't have to unlearn things).

    And what are your weaknesses anyway?
    Kryptonite j/k, nah being self taught means I've only ever learned things that I have needed to use. I only got MSCV++ this year, and previous to that I didn't even know the standard C++! I used to #include <iostream.H>, no "using namespace std" stuff, and STL was undeard of.

    I have looked at templates but never had a use for them in my projects, which is why I don't know much about them. If I was learning this at school at least I'd be told to "write a program that uses a template for....", to at least play with them. I do, however, make use of the STL quite a lot, especially maps.

    // conclusion: the second buffer isn't big enough to hold the string "hello"
    I transformed it to this then:
    char * Word = "byeeeeeeeeeeeeeeeeeeeee";
    And still got the Illegal Operation (Memory could not be 'read') when it tried to print Word.
    Pointers ........ me off!

  8. #8
    Registered User
    Join Date
    May 2003
    Posts
    148
    Originally posted by lyx

    char *word = "bye"; // creates an array of 4 chars and fill it with the string, then makes the pointer points to it

    // conclusion: the second buffer isn't big enough to hold the string "hello"[/code]
    No,read http://www.eskimo.com/~scs/C-faq/q16.6.html

  9. #9
    *******argv[] - hu? darksaidin's Avatar
    Join Date
    Jul 2003
    Posts
    314
    I still have no clue what you want to do.

    Could you give me a few hints?
    You said you had this function:

    Code:
    void Foo(LPVOID Bar);
    What exactly do you want to do with it ? Is Bar a "buffer" (is it used as a "return value"?) or does Foo just read the data supplied via Bar ?

    Do you want to hand over a string to Foo ?
    [code]

    your code here....

    [/code]

  10. #10
    lyx
    lyx is offline
    Amateur
    Join Date
    Sep 2003
    Posts
    228
    To Wledge. Well, didn't know of such a thing, thanks. Anyway, I just can't get a hand on th K&R, sad.
    And, what I said was correct though.

    [edit]But wait, there's something odd to me. Why something declared as modifiable isnt? I mean, when you declare a char *, you're declaring a pointer to a modifiable char... Why is there such a strange thing in the standard.

    [edit]I see, so it was ancient versions of the standard that defined the litterals as non-constant. Sorry for the stupid question.
    Last edited by lyx; 10-01-2003 at 05:51 AM.

  11. #11
    Banned nickname_changed's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    986
    Yes, bar acts sort of as a return value but is a string that is set by Foo so that the calling function can get a value from Foo. Like this:

    Code:
    char WhatFooSays[100];
    cout << "What does Foo think of Paul" << endl;
    cout << Foo(&WhatFooSays) << endl;
    
    // Foo set the variable WhatFooSays to "Paul is a legend!!"
    This is a dumbed down version of the ISAPI GetServerVariable function which actually uses one variable called lpszVariableName to ask for a server variable (like REQUEST_METHOD), and uses another paramater as an LPVOID called lpvBuffer, which is set to the value that the server should give for the "REQUEST_METHOD" CGI variable (eg POST or GET).

  12. #12
    *******argv[] - hu? darksaidin's Avatar
    Join Date
    Jul 2003
    Posts
    314
    Ok, I understand

    This is what you want to do:

    Code:
    [...]
    char	szSomeBuffer[1024];
    DWORD	dwSize= sizeof(szSomeBuffer);
    
    GetServerVariable(
      hSomeConID,
      "SERVER_VARIABLE_NAME",
      szSomeBuffer, 
      &dwSize
    );
    szSomeBuffer should contain the data after the function was called. You might want to use a WCHAR if your connection returns multibyte strings. dwSize was changed to reflect the actual size of the return data.
    [code]

    your code here....

    [/code]

  13. #13
    Banned nickname_changed's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    986
    lol no no Darksaidin, you've got me all wrong. This is for a web server and as such I have to WRITE the GetServerVariable function, which I then put into the EXTENSION_CONTROL_BLOCK structure. Thats why I need to be able to edit the value of lpvBuffer, which is actually a variable in the PHP DLL that was loaded by the server. Get me now?

    Its impossible to find any information about writing an ISAPI server, but theres heaps of stuff around for extensions/filters. I've been forced to figure all this out on my own

  14. #14
    *******argv[] - hu? darksaidin's Avatar
    Join Date
    Jul 2003
    Posts
    314
    Originally posted by stovellp
    lol no no Darksaidin, you've got me all wrong.
    You're not an easy candidate

    Why don't you just declare a chararray for your return data and memcpy the data to the address specified in the parameter? That way you can also control the maximum bytecount so you don't cause a buffer overflow.

    Anyway, I probably misunderstood you again and I think your business is too complicated for me
    [code]

    your code here....

    [/code]

  15. #15
    Banned nickname_changed's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    986
    When writing the server side, its the servers job to create the ReadClient(), WriteClient(), GetServerVariable() and ServerSupportFunction()'s, so that the client DLL may use them with the EXTENSION_CONTROL_BLOCK they are given at the start of execution.

    I cannot change the definition of GetServerVariable(), and so I must make it work without changing the way the function looks to the client. Thats why I must find a way to make it work with an LPVOID rather than any other type of function.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  3. Sorting Linked Lists
    By DKING89 in forum C Programming
    Replies: 6
    Last Post: 04-09-2008, 07:36 AM
  4. Passing structures... I can't get it right.
    By j0hnb in forum C Programming
    Replies: 6
    Last Post: 01-26-2003, 10:55 AM
  5. String sorthing, file opening and saving.
    By j0hnb in forum C Programming
    Replies: 9
    Last Post: 01-23-2003, 12:18 AM

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