atlbase.h
#include <string>
#pragma section("ATL$_a", read)
#pragma section("ATL$_z", read)
#pragma section("ATL$_m", read)
struct Student {
std::string name;
int age;
};
extern "C" {
__declspec(selectany) __declspec(allocate("ATL$_a")) Student* pst_begin = NULL;
__declspec(selectany) __declspec(allocate("ATL$_z")) Student* pst_end = NULL;
}
keke.h
#include "atlbase.h"
const Student st_xiaoming = { "xiaoming", 30 };
const Student st_xiaose = { "xiaose", 30 };
extern "C" __declspec(allocate("ATL$_m")) /*__declspec(selectany)*/ const Student* const pst_xiaose = &st_xiaose;
extern "C" __declspec(allocate("ATL$_m")) /*__declspec(selectany)*/ const Student* const pst_xiaoming = &st_xiaoming;
main
int _tmain(int argc, _TCHAR* argv[])
{
for (auto pp = &pst_begin + 1; pp != &pst_end; ++pp) {
if (*pp) {
std::cout << (*pp)->name << "\t" << (*pp)->age << "\n";
}
}
func();
return 0;
}
使用pragma section新建一个section,然后在里边放指针。
#pragma section("ATL$__a", read)
#pragma section("ATL$__z", read)
#pragma section("ATL$__m", read)
extern "C"
{
__declspec(selectany) __declspec(allocate("ATL$__a")) _ATL_OBJMAP_ENTRY_EX* __pobjMapEntryFirst = NULL;
__declspec(selectany) __declspec(allocate("ATL$__z")) _ATL_OBJMAP_ENTRY_EX* __pobjMapEntryLast = NULL;
}
#if !defined(_M_IA64)
#pragma comment(linker, "/merge:ATL=.rdata")
#endif
#define OBJECT_ENTRY_AUTO(clsid, class) \
__declspec(selectany) ATL::_ATL_OBJMAP_CACHE __objCache__##class = { NULL, 0 }; \
const ATL::_ATL_OBJMAP_ENTRY_EX __objMap_##class = {&clsid, class::UpdateRegistry, class::_ClassFactoryCreatorClass::CreateInstance, class::_CreatorClass::CreateInstance, &__objCache__##class, class::GetObjectDescription, class::GetCategoryMap, class::ObjectMain }; \
extern "C" __declspec(allocate("ATL$__m")) __declspec(selectany) const ATL::_ATL_OBJMAP_ENTRY_EX* const __pobjMap_##class = &__objMap_##class; \
OBJECT_ENTRY_PRAGMA(class)