Solved

LPVOID conversion

Posted on 2007-11-18
7
6,451 Views
Last Modified: 2010-04-21
How to convert a LPVOID variable to a string ?
0
Comment
Question by:walterwkh
  • 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
What Does This C# Code Block Do? 5 56
Need some help with mailto 16 20
C#.net split string and then check appropriate checkboxlist 4 26
linq, c# 8 22
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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

856 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