ofstream << LPCTSTR

This is a discussion on ofstream << LPCTSTR within the C++ Programming forums, part of the General Programming Boards category; I'm writing a hooking program through Microsoft's Detours. I've hooked the WINAPI call CreateFile and am calling my own function ...

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    2

    ofstream << LPCTSTR

    I'm writing a hooking program through Microsoft's Detours. I've hooked the WINAPI call CreateFile and am calling my own function before redirecting the information to kernel32.CreateFile. Here's what mine looks like:

    __declspec(dllexport) HANDLE WINAPI MyCreateFile(
    LPCTSTR lpFileName,
    DWORD dwDesiredAccess,
    DWORD dwShareMode,
    LPSECURITY_ATTRIBUTES lpSecurityAttributes,
    DWORD dwCreationDisposition,
    DWORD dwFlagsAndAttributes,
    HANDLE hTemplateFile)
    {
    myfile << lpFileName;
    return CreateFile(lpFileName,dwDesiredAccess,dwShareMode, lpSecurityAttributes,dwCreationDisposition,dwFlags AndAttributes,hTemplateFile);
    }

    it works, but the problem is that myfile (a pre-opened .txt) is filled with hex gibberish, like:

    "0012F1D87FFDFC007FFDFC007FFDFC007FFDFC007FFDF C"

    I'm assuming it's actually printing the address pointed to by lpFileName. I've tried dereferencing like so:

    myfile << *lpFileName;

    and the new output is numerical gibberish:

    "6868686868686868686868686868686868686"

    any ideas?

  2. #2
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    I would guess you're compiling with UNICODE defined (which isn't a bad thing)

    In that case, you should use std::wofstream, or alternatively convert the wide string to multibyte (look at the wcstombs function)
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    You should in fact use basic_ofstream<TCHAR>, so that it always matches the strings you use in the API.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #4
    Registered User
    Join Date
    Aug 2006
    Posts
    2
    perfect, thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. cout << SunTradingLLC << "is looking for C++ Software Developers" << endl;
    By sun trading in forum Projects and Job Recruitment
    Replies: 1
    Last Post: 04-02-2008, 11:48 AM
  2. ofstream and FILE behaviour
    By MrLucky in forum C++ Programming
    Replies: 7
    Last Post: 06-21-2007, 05:45 PM
  3. Capturing key presses
    By cgod in forum Windows Programming
    Replies: 6
    Last Post: 11-25-2004, 12:10 PM
  4. Using ofstream in private of a class-errors
    By loobian in forum C++ Programming
    Replies: 3
    Last Post: 12-13-2003, 09:06 PM
  5. ofstream and ifstream for searching and writing
    By johnnyd in forum C++ Programming
    Replies: 6
    Last Post: 03-17-2003, 07:34 AM

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