learnerX
learnerX

Reputation: 1082

How to convert argv[1] to LPCVOID?

I have a C++ utility that accepts argv[] arguments. One of the arguments is to be converted and used as a memory address by a Windows API call that required this address to be LPCVOID according to documentation. I have attempted the following:

int main(int argc, CHAR* argv[])

{
    if (argc < 3) {
        cout << "Arg1 and Arg2 are required.";
        exit(EXIT_FAILURE);
    }

    LPCVOID address = (LPCVOID)argv[1];
    cout << address;

    // some other stuff here
    return 0;
}

However, address is not used correctly and the cout statement above prints the address after conversion as 01297FFA when the argv[1] input is 161551C. Any help on fixing this is appreciated.

EDIT to include the command I executed:

myprog.exe 161551C

Upvotes: 0

Views: 717

Answers (1)

Andr&#233; Caceres
Andr&#233; Caceres

Reputation: 729

You have to convert the string pointed by argv[1] to an integer representation, and then cast the integer to an LPCVOID pointer.

Here it seems to solve the issue:

uintptr_t u_adress;   
std::stringstream ss;
ss << std::hex << argv[1];
ss >> u_adress;
LPCVOID address = (LPCVOID)u_adress;

Upvotes: 3

Related Questions