c00000fd c00000fd - 27 days ago 9
C++ Question

How to convert a #define definition into a string in VS2008?

Say, if I have an exported function defined as such in

VS2008
:

#define myExportedFunction fn1

extern "C" __declspec(dllexport) BOOL WINAPIV myExportedFunction(int val)
{
return val == 2;
}


So how do I create a string out of
myExportedFunction
preprocessor definition to use as such?

BOOL(WINAPIV *pfn_myExportedFunction)(int val);

HMODULE hDll = ::LoadLibrary(strMyDllPath);
(FARPROC&)pfn_myExportedFunction = ::GetProcAddress(hDll, myExportedFunction); //Causes: error C2065: 'fn1' : undeclared identifier

Answer

Just use prepocessor operator:

#define myExportedFunction fn1
#define TO_STR_(X) #X
#define TO_STR(X) TO_STR_(X)

extern "C" __declspec(dllexport) BOOL WINAPIV myExportedFunction(int val)
{
    return val == 2;
}

BOOL(WINAPIV *pfn_myExportedFunction)(int val);

HMODULE hDll = ::LoadLibrary(strMyDllPath);
(FARPROC&)pfn_myExportedFunction = ::GetProcAddress(hDll, TO_STR(myExportedFunction));
Comments