i am injecting a dll into oneo my process's and i want it to read the bytes of the file and write those specified bytes to a file on hard disk. The problem is my .dll compiles, but when the .dll isi njected, it never creates the file and writes to it :( -thx (if i use C file operations will this bypass this problem?)

Edited 5 Years Ago by maybnxtseasn: n/a

>>it never creates the file and writes to it
You should have put some error checking into the program. I suspect either LoadLibrary() failed or GetProcAddress() failed. I had a similar problem until I passed GetProcAccress() the mangled function name.

Here is a dll that I just wrote and it works ok with the driver *.exe program that calls the function using LoadLibrary() and GetProcAddress(). The second parameter to GetProcAddress(), which is the name of the function to be called, as derived by using dumpbin.exe on the dll then copying the function's mangled name into the second parameter. In my example the mangled name was "?ReadFile@@YG_NPBD@Z"

This is the dll function.

#include <fstream>
#include <Windows.h>
#include "testdll.h"

using std::ifstream;
using std::ofstream;

TESTDLL_API bool WINAPI ReadFile(const char* filename)
{
    char c;
    ifstream in(filename);
    if( !in.is_open() )
    {
        return false;
    }
    ofstream out("out.txt");
    if( !out.is_open() )
    {
        in.close();
        return false;
    }
    while(  in.get(c) )
    {
        out << c;
    }
    return true;
}

Here is the driver program. I just hard-coded the input file name.

#include <Windows.h>
#include <iostream>
using std::cin;
using std::cout;

typedef void (WINAPI *PGNSI)(const char*);

int main()
{
    char buf[255]= {0};
    DWORD dwError = 0;
    HMODULE h = LoadLibrary("C:\\dvlp\\testdll\\Debug\\testdll.dll");
    if( h == NULL)
    {
        dwError = GetLastError();
        FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,0,dwError,0,buf,sizeof(buf),0);
        cout << "LoadLibrary() failed\n";
        cout << buf << '\n';
        return 1;
    }

    PGNSI pGNSI = (PGNSI) GetProcAddress(h,"?ReadFile@@YG_NPBD@Z");
    if( pGNSI != NULL)
    {
        pGNSI("stdafx.h");
    }
    else
    {
        dwError = GetLastError();
        FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,0,dwError,0,buf,sizeof(buf),0);
        cout << "GetProcAccress() failed\n";
        cout << buf << '\n';
    }
}

Edited 5 Years Ago by Ancient Dragon: n/a

This article has been dead for over six months. Start a new discussion instead.