Solved

LPVOID conversion

Posted on 2007-11-18
7
6,506 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 500 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
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!

 
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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Suggested Solutions

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

733 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