Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to vfscanf in Win32

14 views
Skip to first unread message

Emmanuel Mogenet

unread,
Jul 20, 1999, 3:00:00 AM7/20/99
to
 
For some reason, vfprintf is in the Windows API, but vfscanf isn't.

Quite annoying, especially if you consider that the call actually exists in msvcrt (it's called _input)

Unfortunately, it isn't exported.

However, there is a way, something along the lines of:

#ifdef WIN32
static uint32_t savedESP;
static uint32_t savedEAX;
static uint32_t savedRET;
static uint32_t savedThis;
static void     *truefscanf= (void*)fscanf;

class File
{
public:
                ...
                int     scanf(const char*,...);
                ...
private:
                void    *file;
};

/*
 * The things you gotta do on NT ...
 * No vfscanf, so got to trick the fscanf into
 * believing it's called directly.
 */
__declspec(naked) int File::scanf(
const char *,
...
)
{
    _asm
    {
        mov     dword ptr savedESP,esp          // Save esp in static

        mov     eax, dword ptr [esp]            // eax <- return address
        mov     dword ptr savedRET,eax          // Save return address in static

        mov     eax,dword ptr [esp+4]           // eax <- this
        mov     dword ptr savedThis,eax         // Save this in static

        add     esp, 8                          // esp+= 8 (crush retaddr,this)

        mov     eax,dword ptr [eax]             // eax <- this->filePtr
        push    eax                             // Push filePtr on argStack

        call    truefscanf                      // Call regular fscanf
        mov     dword ptr savedEAX,eax          // Save eax value in static

        mov     esp, dword ptr savedESP         // Restore esp

        mov     eax, dword ptr savedRET         // Restore return value
        mov     dword ptr [esp], eax

        mov     eax, dword ptr savedThis        // Restore this
        mov     dword ptr [esp+4], eax

        mov     eax, dword ptr savedEAX         // Restore eax

        ret
    }
}
#endif // WIN32
 
 

0 new messages