#include "factory.h" #include #include #include #include #include #include #include #include #include namespace fun { std::string ObjectArchive_c::GetClassText(const std::string& classname) { std::string text = ""; CreateFunction_t* func = FindCreateFunction(classname); if ( func ) { Serializable_c* obj = func(this); text = obj->GetText(); } return text; } ObjectArchive_c::CreateFunction_t* ObjectArchive_c::FindCreateFunction( const std::string& cClassName ) { std::string cSymName; int nDepth = 0; int nStart = 0; int nNameSpaceID = -1; // Create a mangled symbol name from the "outernamespace::innernamespace::classname" string for ( unsigned int i = 0 ; i <= cClassName.size() ; ++i ) { if ( i == cClassName.size() || cClassName[i] == ':' ) { const char* pzName = cClassName.c_str() + nStart; std::string cNamePart( pzName, i - nStart ); if ( nNameSpaceID == -1 && cNamePart == "fun" ) { nNameSpaceID = nDepth; } cSymName = cNamePart + "@" + cSymName; nStart = i +2; nDepth++; ++i; // Skip the second colon } } cSymName = "?Instantiate@" + cSymName + "@SAPAV"; for ( int i2 = 0 ; i2 < nDepth ; ++i2 ) { cSymName += '1' + i2; } if ( nNameSpaceID == -1 ) { cSymName += "@PAVObjectArchive_c@fun@@@Z"; } else { cSymName += "@PAVObjectArchive_c@"; cSymName += '0' + nDepth - nNameSpaceID; cSymName += "@@Z"; } std::cout << "The mangled name is \"" << cSymName << "\"\n"; HANDLE hSnapshot = CreateToolhelp32Snapshot( TH32CS_SNAPMODULE, 0 ); MODULEENTRY32 sEntry; sEntry.dwSize = sizeof(sEntry); for ( BOOL bLoop = Module32First( hSnapshot, &sEntry ) ; bLoop ; bLoop = Module32Next( hSnapshot, &sEntry ) ) { std::cout << "Searching in \"" << sEntry.szModule << "\"\n"; int nStrLen = strlen(sEntry.szModule); if( nStrLen > 4 && !_strnicmp(sEntry.szModule+nStrLen-4,".dll", 4) ) { CreateFunction_t* pFnc = (CreateFunction_t*) GetProcAddress( sEntry.hModule, cSymName.c_str() ); if ( pFnc != NULL ) { std::cout << "Found the function at " << (void*)pFnc << std::endl; CloseHandle( hSnapshot ); return pFnc; } } } CloseHandle( hSnapshot ); return 0; } }