string to LPCVOID

This is a discussion on string to LPCVOID within the Windows Programming forums, part of the Platform Specific Boards category; can we do "string to LPCVOID" ??...

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    44

    string to LPCVOID

    can we do "string to LPCVOID" ??

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,702
    Assuming std::string, then it's just "str.c_str()".

    Some information on the context in which this is being done would be helpful - including the actual type of "string".

    gg

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    44
    thanks for reply but I tried this I get error :S

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,027
    What error?
    Are you using unicode?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    string -> C++ -> no automatic conversion to/from void *, so perhaps all you need is:
    Code:
    reinterpret_cast<void *>(str.c_str())
    If that doesn't work, we need to see the code, and the error message, because we can guess what is wrong for quite a while before getting it right.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,027
    No... any type can be implicitly converted to void*, but it cannot be implicitly converted from void*.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    May 2008
    Posts
    44
    this is my code

    Code:
        if(!WriteProcessMemory(hProcess, str, lpszDllPath.c_str(), lpszDllPath.length() + 1, 0))
    and this is my error

    Code:
    error C2664: 'WriteProcessMemory' : cannot convert parameter 3 from 'const char *' to 'void *'
    and "lpszDllPath" is string.. thanks for reply and sory my english..

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,027
    In this case, it is safe to convert to void*.
    It doesn't work because c_str() returns const char*, and the function takes void* (thus you disregard the const specifier).
    Try:
    Code:
        if(!WriteProcessMemory(hProcess, str, const_cast<char*>( lpszDllPath.c_str() ), lpszDllPath.length() + 1, 0))
    Last edited by Elysia; 05-24-2008 at 07:26 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    May 2008
    Posts
    44
    I tried it but I got another error.. It is

    Code:
    error C2440: 'reinterpret_cast' : cannot convert from 'const char *' to 'void *'

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,027
    I checked out the situation and edited the post. It should work.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,189
    Quote Originally Posted by Elysia View Post
    Code:
        if(!WriteProcessMemory(hProcess, str, const_cast<char*>( lpszDllPath.c_str() ), lpszDllPath.length() + 1, 0))
    try:
    Code:
    if(!WriteProcessMemory(hProcess, str, (void*)( lpszDllPath.c_str() ), lpszDllPath.length() + 1, 0))
    the function expects void*, not const char *
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,027
    Irrelevant. Char* can be implicitly converted to void*.
    However, const char* cannot be implicitly converted to void* because it's const. Const void* works.

    Therefore, we must cast away the const, so const char* -> char* -> void*.
    It works. I tried it.
    But if you want to do it the C-way, be my guest.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,702
    Quote Originally Posted by koyboy
    error C2664: 'WriteProcessMemory' : cannot convert parameter 3 from 'const char *' to 'void *'
    Quote Originally Posted by MSDN
    Code:
    BOOL WriteProcessMemory(HANDLE hProcess,
                            LPVOID lpBaseAddress,
                            LPCVOID lpBuffer,
                            SIZE_T nSize,
                            SIZE_T* lpNumberOfBytesWritten);
    You should update your Platform SDK. See this thread for links: Windows programming links
    In the meantime, open up your <winbase.h> and change the 3rd parameter type to LPCVOID.

    gg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 05:30 PM
  2. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 07:02 AM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 07:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21