Solved

LPVOID conversion

Posted on 2007-11-18
7
6,319 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
Comment Utility
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
Comment Utility
>> // 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
Comment Utility
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 15

Expert Comment

by:efn
Comment Utility
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
Comment Utility
thanks both evilrix and efn
0
 
LVL 40

Expert Comment

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

Expert Comment

by:efn
Comment Utility
Right, thanks.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
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…

728 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now