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?)

When you say "injecting" do you mean LoadLibrary()? If not, then what exactly do you mean.

You need to post some code that illustrates the problem. What compiler are you using?

>>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';
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.