0

I have 2 data structures both in my injector and dll:

struct SubData_t
{
    int SubTest;
};

struct DataHolder_t
{
    int Test;
    SubData_t SubData;
};

This is how I assign the data structure values:

DataHolder_t *DataHolder = new DataHolder_t();
DataHolder->Test = 123;
DataHolder->SubData.SubTest = 456;

int ResponseCode = Inject(4321, "test.dll", DataHolder);

And this is a custom LoadLibrary function that passes the data to dll export:

int Inject(DWORD ProcessId, char *DLLFile, DataHolder_t *Data)
{
    // ...
    LoadRemoteLibraryR(hProcess, lpBuffer, dwLength, NULL, 0xd13f5171 /* dllexport EntryPoint hash */, (LPVOID)Data, (DWORD)(sizeof(struct DataHolder_t) + 1));
    // ...
}

Then I get the data in the dll export and assign the values from lpUserdata to the data structure:

DLLEXPORT BOOL EntryPoint(LPVOID lpUserdata, DWORD nUserdataLen)
{
    DataHolder_t *DataHolder = (struct DataHolder_t *)lpUserdata;

    char buffer[100];
    sprintf(buffer, "%d | %d", DataHolder->Test, DataHolder->SubData.SubTest);

    MessageBox(NULL, buffer, "Test", MB_OK);

    return TRUE;
}

However I'm new to lambdas and can't figure out how (if it's even possible) to pass lambda (functions) the same way to the dll export. Or is there any other way to pass functions/logic to the dll export, so that they can be called from the target process that the dll was injected to?

1 Answers1

0

Use type erasure, like std::function, or a reasonable facsimile, and pass the type-erased parameter as a reference, instead of passing by value, for best results.

Sam Varshavchik
  • 114,536
  • 5
  • 94
  • 148
  • Thanks for the reply. Do you mean something like this by any chance? http://stackoverflow.com/questions/23962019/how-to-initialize-stdfunction-with-a-member-function –  Mar 18 '16 at 12:45
  • You're on the right track. The answers in that other question show several ways of doing that, that you can adapt to your own case. – Sam Varshavchik Mar 18 '16 at 12:46
  • Good to know, `std::bind` seems interesting in that particular question. I'll try searching more and fiddling around. Thanks for a quick reply again! –  Mar 18 '16 at 12:49
  • I haven't made any significant progress so far, could you possibly assist me any further @SamVarshavchik? –  Mar 18 '16 at 14:00
  • I could create a map of callbacks by storing function pointers, but that would be useless as the map does not contain the callbacks themselves and therefore function pointers become invalid when passed to the dll, since I can't have the functions defined in both dll and injector, but only injector. Any idea? –  Mar 18 '16 at 15:00
  • If you have specific questions, you can always post another question. – Sam Varshavchik Mar 18 '16 at 17:35