MemoryModule icon indicating copy to clipboard operation
MemoryModule copied to clipboard

Didn't work with virtual functions

Open xkcd50 opened this issue 9 months ago • 0 comments

I'm using the latest version 5f83e41 and VS2022. to reproduce this issue,please add a file ISampleDLL.h in SampleDLL dir:

#pragma once

class ISampleDLL
{
public:
	virtual ~ISampleDLL() = default;
	virtual int addNumbers(int a,int b) = 0;
};

and replace SampleDLL.h

#include "ISampleDLL.h"
class SampleDLL: public ISampleDLL
{
public:
	static SampleDLL& Instance();
	
	virtual int addNumbers(int a,int b) override;
private:
	SampleDLL();
	~SampleDLL();
	SampleDLL(const SampleDLL&) = delete;
	SampleDLL& operator=(const SampleDLL&) = delete;
};

SampleDLL.cpp

#include "SampleDLL.h"

SampleDLL::SampleDLL()
{}

SampleDLL::~SampleDLL()
{}

SampleDLL& SampleDLL::Instance()
{
	static SampleDLL instance;
	return instance;
}

int SampleDLL::addNumbers(int a,int b)
{
	return a + b;
}

extern "C" __declspec(dllexport) ISampleDLL* CreateSampleDLL()
{
	return &SampleDLL::Instance();
}

edit DllLoader.cpp

...
#include "../SampleDLL/ISampleDLL.h"
...
void LoadFromMemory(void)
{
...
        using GetSampleDLL=ISampleDLL* (*)();
        //addNumber = (addNumberProc)MemoryGetProcAddress(handle, "addNumbers");
        GetSampleDLL getSampleDLL = (GetSampleDLL)MemoryGetProcAddress(handle,"CreateSampleDLL");
        ISampleDLL* sampleDLL = getSampleDLL();
        _tprintf(_T("From memory: %d\n"), sampleDLL->addNumbers(1, 2));
...
}

then variable getSampleDLL and sampleDLL have not null address,but the vftable of sampleDLL got adress 0x0.

ps. I have read issue#31 but it didn't solve my problem

xkcd50 avatar Mar 15 '25 13:03 xkcd50