Like Tree12Likes
  • 1 Post By msh
  • 4 Post By laserlight
  • 1 Post By msh
  • 1 Post By King Mir
  • 2 Post By laserlight
  • 2 Post By iMalc
  • 1 Post By Elysia

ReadFile with a C++ string

This is a discussion on ReadFile with a C++ string within the C++ Programming forums, part of the General Programming Boards category; Win32 API function ReadFile()'s second argument takes an LPVOID. Is there a way to use a C++ string directly for ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    812

    ReadFile with a C++ string

    Win32 API function ReadFile()'s second argument takes an LPVOID.
    Is there a way to use a C++ string directly for the second argument?


    Code:
    string strBuf;
    ReadFile(h,  reinterpret_cast<LPVOID>strBuf.c_str(), size, &dwRead, NULL);
    Compiler MSVC++ 2013 with Code::Blocks.

  2. #2
    msh
    msh is offline
    Novice
    Join Date
    Jul 2009
    Posts
    568
    No. Second parameter is a buffer that receives data, but `c_str()` returns `const char*`.
    Ducky likes this.
    Disclaimer: This post shows my ignorance at the time of its making. I claim ownership of but not responsibility for all errors in it. Reference at your own peril.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    812
    So I should pass the string to a char* first, that defeats the purpose of using a string.
    The inventor of C++ should have thought about Win32 API functions too when he was writing the language.
    Compiler MSVC++ 2013 with Code::Blocks.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,265
    Quote Originally Posted by Ducky
    Is there a way to use a C++ string directly for the second argument?
    You can pass &strBuf[0] as the argument, but of course, you must have set the std::string object to have a large enough size, possibly including a null terminator if applicable.

    Quote Originally Posted by Ducky
    The inventor of C++ should have thought about Win32 API functions too when he was writing the language.
    The inventor of the Win32 API functions should have thought about C++ when he was designing the API (although it is in C ).
    Ducky, msh, Elkvis and 1 others like this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    812
    Since when we have to set the size of a C++ string? I thought that, that was the point of C++ strings (no need to set a size).

    Haha, wasn't it the Win32 API that came first? :-)
    Compiler MSVC++ 2013 with Code::Blocks.

  6. #6
    msh
    msh is offline
    Novice
    Join Date
    Jul 2009
    Posts
    568
    We don't, but we can with `std::string::reserve()`.
    Ducky likes this.
    Disclaimer: This post shows my ignorance at the time of its making. I claim ownership of but not responsibility for all errors in it. Reference at your own peril.

  7. #7
    Registered User
    Join Date
    Apr 2006
    Posts
    2,053
    Quote Originally Posted by Ducky View Post
    So I should pass the string to a char* first, that defeats the purpose of using a string.
    The inventor of C++ should have thought about Win32 API functions too when he was writing the language.
    You can't pass a string to a dll, because the dll's implementation of string may be different than what your program uses.

    Also, it's the library writer's responsibility to make their library easy to use. The C++ committee does tailor the language to specific libraries, libraries must tailor themselves to the language.
    Ducky likes this.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,265
    Quote Originally Posted by Ducky
    Since when we have to set the size of a C++ string?
    You decided to use an API that has no notion of a C++ string, so it is up to you to do what is necessary.

    Quote Originally Posted by Ducky
    I thought that, that was the point of C++ strings (no need to set a size).
    In that case, you should use std::getline, or some other API that provides such a convenience.

    Quote Originally Posted by Ducky
    Haha, wasn't it the Win32 API that came first? :-)
    They had enough time to change their API, so it is obviously the Win32 API designers' fault. Okay, seriously: you're using an OS specific API and asking why a general purpose programming language's standard library does not directly support it as part of the standard

    Quote Originally Posted by msh
    We don't, but we can with `std::string::reserve()`.
    That would be wrong: reserve would reserve capacity, but here the elements should actually exist.
    Elysia and Ducky like this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Dec 2007
    Posts
    812
    Ok, that clears it up. It's just better to forget about C++ with Win32 API.
    Compiler MSVC++ 2013 with Code::Blocks.

  10. #10
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Quote Originally Posted by Ducky View Post
    Ok, that clears it up. It's just better to forget about C++ with Win32 API.
    Well not entirely. It's better to use or write your own wrapper functions and classes that make the API functions nice to deal with. Something like MFC or ATL.
    Ducky and Elysia like this.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,007
    Quote Originally Posted by Ducky View Post
    Ok, that clears it up. It's just better to forget about C++ with Win32 API.
    Just because one little API is giving you trouble, you decide to throw in the towel? Yes, that is going to get you far...
    I mean, it can't be helped. The library is C, and C requires you to have a sufficient buffer. It expects this. No possible language in the world could possibly change this. You must have a buffer of sufficient size.
    So just reserve space, then pass the buffer and its size to the function. Hard? Not very.
    Oh, you don't like doing that? Then why are you using a C API? There are alternatives, you know...
    Or you could write a wrapper around these functions with a similar interface to "tame" them. How do you think the C++ standard library is implemented?
    The real world sucks, and we have to live with it. Throwing in the towel because a little mishap is just not going to work. You need to tame the world to your needs.
    Ducky likes this.
    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.

  12. #12
    Registered User
    Join Date
    Dec 2007
    Posts
    812
    Well its not just one little API function, all the API functions take C style strings.
    And if I have to reserve space with C++ strings that kinda defeats the purpose of using it.
    Why complicate things when you can have it simple?
    Compiler MSVC++ 2013 with Code::Blocks.

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,007
    It is the price you pay for using low level functions. And besides, strings require no manual memory management.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. ReadFile Help
    By Anddos in forum Windows Programming
    Replies: 1
    Last Post: 12-12-2011, 01:10 PM
  2. ReadFile();
    By Queatrix in forum Windows Programming
    Replies: 8
    Last Post: 05-02-2005, 03:42 PM
  3. readfile
    By ZeroG in forum Windows Programming
    Replies: 3
    Last Post: 09-21-2004, 09:20 AM
  4. Readfile() - Newbi help
    By pittuck in forum C++ Programming
    Replies: 4
    Last Post: 11-28-2003, 06:29 PM
  5. ReadFile?
    By SyntaxBubble in forum Windows Programming
    Replies: 2
    Last Post: 10-23-2001, 02:13 AM

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