读书人

WinBase.h 里头定义的这个看不懂啊 #

发布时间: 2013-03-14 10:33:15 作者: rapoo

WinBase.h 里面定义的这个看不懂啊 #ifdef UNICODE
大家好 WinBase.h 里面定义:

WINAPI
LoadLibraryW(
__in LPCWSTR lpLibFileName
);
#ifdef UNICODE
#define LoadLibrary LoadLibraryW
#else
#define LoadLibrary LoadLibraryA
#endif // !UNICODE


LoadLibrary 是一个函数吗?还是变量?LoadLibrary 的定义在哪儿?

多谢啦
[解决办法]
这是个函数,这里使用了宏定义,为了方便两种编码(unicode和ASCII)兼容。

#ifdef UNICODE
#define LoadLibrary LoadLibraryW //如果是UNICODE编码就使用LoadLibraryW
#else
#define LoadLibrary LoadLibraryA,//如果不是UNICODE编码就使用LoadLibraryA

在你自己写的代码中直接可以用LoadLibrary,由编译器负责使用哪个具体函数。
[解决办法]
WINAPI LoadLibraryW(
__in LPCWSTR lpLibFileName
);
前面的WINAPI就是意思为windows的函数,括号里为参数,一般用于驱动调库吧。
[解决办法]
LoadLibrary 一个API函数,目的是加载你的动态库
[解决办法]
Maps the specified executable module into the address space of the calling process.

For additional load options, use the LoadLibraryEx function.


HMODULE WINAPI LoadLibrary(
__in LPCTSTR lpFileName
);


查看MSDN是一种有效的学习方法
// A simple program that uses LoadLibrary and 
// GetProcAddress to access myPuts from Myputs.dll.

#include <windows.h>
#include <stdio.h>

typedef int (__cdecl *MYPROC)(LPWSTR);

VOID main(VOID)
{
HINSTANCE hinstLib;
MYPROC ProcAdd;
BOOL fFreeResult, fRunTimeLinkSuccess = FALSE;

// Get a handle to the DLL module.

hinstLib = LoadLibrary(TEXT("myputs"));

// If the handle is valid, try to get the function address.

if (hinstLib != NULL)
{
ProcAdd = (MYPROC) GetProcAddress(hinstLib, "myPuts");

// If the function address is valid, call the function.

if (NULL != ProcAdd)
{
fRunTimeLinkSuccess = TRUE;
(ProcAdd) (L"Message sent to the DLL function\n");
}

// Free the DLL module.



fFreeResult = FreeLibrary(hinstLib);
}

// If unable to call the DLL function, use an alternative.

if (! fRunTimeLinkSuccess)
printf("Message printed from executable\n");
}

读书人网 >C++

热点推荐