Thread: win32: how use LPVOID?

  1. #1
    Registered User
    Join Date
    Aug 2013
    Posts
    451

    win32: how use LPVOID?

    for apoiting a LPVOID we use:
    Code:
    string strText="hello world";
    LPVOID lpText=static_cast<LPVOID>(&strtext)
    until here fine.
    for change the strText value we use:
    Code:
    *((string*)(lpText)) = "hello new world";
    now imagine that we don't know that it's a string. how can i do it?

  2. #2
    Registered User
    Join Date
    Dec 2010
    Location
    Trinidad, CO (log cabin in middle of nowhere)
    Posts
    148
    That looks extremely strange to me. Could you specify rather what you are attempting to accomplish? I fear you may be going about something in the wrong, or at least, a very awkward manner.

  3. #3
    Registered User
    Join Date
    Dec 2010
    Location
    Trinidad, CO (log cabin in middle of nowhere)
    Posts
    148
    For example, this...

    Code:
    LPVOID lpText=static_cast<LPVOID>(&strtext)
    If your intent there is to cast the address of a C++ Standard Library string object to an untyped void pointer, I can assure you that there is no function within the Windows Api that can make any use of that object. Windows Api functions that operate on character string data exclusively use pointers to null terminated strings, or a special case of those for COM Apis - BSTRs. If you have data in C++ Standard Library string objects, you need to use the string::c_str() member to return a pointer to the underlying null terminated string maintained by the string object.

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    LPVOID is a typedef to void*. You can implicitly convert any pointer to void* in C++, and implicitly convert between any pointer types in C. If you have a function that takes an LPVOID as a parameter, just pass it your pointer, with no cast, and it should work just fine.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  5. #5
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    Ditto what was said above. But to answer the other part of your question you could do something like this to know the type.

    This code isn't compilable but here's the idea.
    Code:
    enum Types
    {
        String,
        Int
    }
    
    struct TypeHolder
    {
        void* Data;
        Types Type;
    }
    
    int value = 10;
    TypeHolder holder;
    holder.Data = &value;
    holder.Type = Int;
    
    LPVOID param = &holder;

  6. #6
    Registered User
    Join Date
    Aug 2013
    Posts
    451
    Quote Originally Posted by prog-bman View Post
    Ditto what was said above. But to answer the other part of your question you could do something like this to know the type.

    This code isn't compilable but here's the idea.
    Code:
    enum Types
    {
        String,
        Int
    }
    
    struct TypeHolder
    {
        void* Data;
        Types Type;
    }
    
    int value = 10;
    TypeHolder holder;
    holder.Data = &value;
    holder.Type = Int;
    
    LPVOID param = &holder;
    seems that i miss these thread. sorry.
    yes i did something like that. and now works great
    and thanks for your enum idea.. it's great

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Win32 API, can it inFile and outFile, like Win32 console?
    By neutral87 in forum Windows Programming
    Replies: 6
    Last Post: 05-04-2013, 07:19 AM
  2. Win32 Console Application event handlers, or Win32 Project?
    By Joewbarber in forum C++ Programming
    Replies: 1
    Last Post: 03-29-2013, 07:02 AM
  3. CreateWindow LPVOID
    By Mithoric in forum Windows Programming
    Replies: 1
    Last Post: 04-13-2004, 09:46 PM
  4. LPVOID to char *
    By nickname_changed in forum C++ Programming
    Replies: 18
    Last Post: 10-01-2003, 09:51 AM
  5. pass several arguments to LPVOID param of CreateThread
    By Echidna in forum Windows Programming
    Replies: 4
    Last Post: 05-05-2002, 10:18 AM