如何在 VS2008 中将#define 定义转换为字符串?
How to convert a #define definition into a string in VS2008?
比如说,如果我在 VS2008
中定义了一个导出函数:
#define myExportedFunction fn1
extern "C" __declspec(dllexport) BOOL WINAPIV myExportedFunction(int val)
{
return val == 2;
}
那么如何根据 myExportedFunction
预处理器定义创建一个字符串来使用?
BOOL(WINAPIV *pfn_myExportedFunction)(int val);
HMODULE hDll = ::LoadLibrary(strMyDllPath);
(FARPROC&)pfn_myExportedFunction = ::GetProcAddress(hDll, myExportedFunction); //Causes: error C2065: 'fn1' : undeclared identifier
只需使用预处理运算符:
#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));
比如说,如果我在 VS2008
中定义了一个导出函数:
#define myExportedFunction fn1
extern "C" __declspec(dllexport) BOOL WINAPIV myExportedFunction(int val)
{
return val == 2;
}
那么如何根据 myExportedFunction
预处理器定义创建一个字符串来使用?
BOOL(WINAPIV *pfn_myExportedFunction)(int val);
HMODULE hDll = ::LoadLibrary(strMyDllPath);
(FARPROC&)pfn_myExportedFunction = ::GetProcAddress(hDll, myExportedFunction); //Causes: error C2065: 'fn1' : undeclared identifier
只需使用预处理运算符:
#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));