?
Solved

LPVOID conversion

Posted on 2007-11-18
7
Medium Priority
?
6,677 Views
Last Modified: 2010-04-21
How to convert a LPVOID variable to a string ?
0
Comment
Question by:walterwkh
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 
LVL 40

Expert Comment

by:evilrix
ID: 20307321
Examples...
// STL string
void foo(LPVOID lpVoid)
{
    // C++
    std::string *pString = reinterpret_cast<std::string *>(lpVoid);
    // C
    std::string *pString = (std::string *) lpVoid;
}
 
// C-String void foo(LPVOID lpVoid)
{
    // C++
    char *pString = reinterpret_cast<char *>(lpVoid);
 
    // C
    char *pString = (char *) lpVoid;
}

Open in new window

0
 
LVL 40

Accepted Solution

by:
evilrix earned 2000 total points
ID: 20307326
>> // C-String void foo(LPVOID lpVoid)

Typo; should be...

// C-String

void foo(LPVOID lpVoid)

// STL string
 
void foo(LPVOID lpVoid)
{
    // C++
    std::string *pString = reinterpret_cast<std::string *>(lpVoid);
    // C
    std::string *pString = (std::string *) lpVoid;
}
 
// C-String
 
void foo(LPVOID lpVoid)
{
    // C++
    char *pString = reinterpret_cast<char *>(lpVoid);
 
    // C
    char *pString = (char *) lpVoid;
}

Open in new window

0
 
LVL 40

Expert Comment

by:evilrix
ID: 20307334
In C++ you should use reinterpret_cast<>() as it is safer than C-style casting.
http://msdn2.microsoft.com/en-us/library/e0w9f63b(VS.80).aspx

Note, then you convert to a pointer of a different type the only safe thing you can do with that pointer is convert it back! Anything else is, at best, non-portable.

All styles of casting are discussed here...
http://msdn2.microsoft.com/en-us/library/x9wzb5es(VS.80).aspx

I hope this help.

-Rx.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 15

Expert Comment

by:efn
ID: 20309221
It depends on what the LPVOID addresses and what kind of string you mean.

If the LPVOID points to some kind of string, you can convert it to a pointer to that kind of string with a cast, as evilrix showed.  A static-cast will work as well as a reinterpret-cast.  If you want a different kind of string, you will have to construct a new string from what the pointer addresses.  This will also require a cast to turn the LPVOID into something you can use.

For example, if the LPVOID points to a C-style null-terminated string and you want a std::string, you can do this:

std::string exvoid(<static-cast>(const char *) lpVoid);
0
 

Author Closing Comment

by:walterwkh
ID: 31409791
thanks both evilrix and efn
0
 
LVL 40

Expert Comment

by:evilrix
ID: 20310784
>> std::string exvoid(<static-cast>(const char *) lpVoid);
std::string exvoid(static_cast<const char *>(lpVoid));
0
 
LVL 15

Expert Comment

by:efn
ID: 20311025
Right, thanks.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question