最近做了个项目,获得cpu占用率,动态引用ntdll.dll里的函数,编译环境:visual studio 2008,但是调试时发现在LoadLibrary函数返回的值无法解析,这样在GetProcAddress时返回null,郁闷了一下午,终于在一个老外的论坛上找到答案,如下:
ask:
I'm using MS Visual Studio C++ 2008 Express. I've included all the paths to the DLL, "visa32.dll" but the LoadLibrary("visa32.dll") call returns the statement that can't convert character string to LPCSTR (pointer). I ran the same code with the Portland Group compiler (which I no longer have) and there was not a problem accessing the DLL. Is there some requirement for the C++ 2008 Express compiler that needs to be met?
answer:
Hello jterry,
If you compile as Unicode you need to pass Unicode strings to Win32 APIs which have LPTSTR or LPWSTR string. When you want to write code compilable for Unicode and Ansi just enclose all string constants in _T(). In your case:
LoadLibrary(_T("visa32.dll"))
You can look at this post on the microsoft discussion forums for more information. Have a great day.
PS:定义 _T() 的头文件tchar.h。
为什么这种问题都是在老外的帖子上找到答案呢,唉~~不专业。