请问C++中 如何Load Dll,或者说成如何用LoadLibrary 这个函数

xinen8721 2012-01-18 12:53:53
我用C++ Console完成了程序, 现在目标把它装成DLL,我的程序里创建了CPlayerDatabase这个类,已经把其中的必要的函数标记成了 dllexport 。我按照MSDN 上的 tutorial page of DLL 完成了搭建并且成功。问题是我的头儿希望我用Loadlibrary 这个函数,所以请各位朋友告诉我基本的步骤,我在这里散分了。
...全文
348 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
Eleven 2012-01-18
  • 打赏
  • 举报
回复
// 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");
}

MSDN上的例子代码~
xinen8721 2012-01-18
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 visualeleven 的回复:]

引用 4 楼 xinen8721 的回复:

引用 1 楼 visualeleven 的回复:

C/C++ code
// A simple program that uses LoadLibrary and
// GetProcAddress to access myPuts from Myputs.dll.

#include <windows.h>
#include <……
[/Quote]
Thanks dude.
I am replying you with the NO-Chinese-Input computer in my Company. I hope you don't mind.
My problem is:
I define two Class Types in my DLL, CPlayerDatabase and CPlayer, now I am creating another project to call the .dll file, when the .dll file loaded successfully, I want to create the Class Instance for CPlayerDatabase, now the error is still showing CPlayerDatabase undeclared.

Can you tell me why?
Eleven 2012-01-18
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 xinen8721 的回复:]

引用 1 楼 visualeleven 的回复:

C/C++ code
// A simple program that uses LoadLibrary and
// GetProcAddress to access myPuts from Myputs.dll.

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

typedef int (……
[/Quote]

HMODULE WINAPI LoadLibrary(
__in LPCTSTR lpFileName
);

Parameters
lpFileName
The name of the executable module (either a .dll or .exe file). The name specified is the file name of the module and is not related to the name stored in the library module itself, as specified by the LIBRARY keyword in the module-definition (.def) file.

If the string specifies a path but the file does not exist in the specified directory, the function fails. When specifying a path, be sure to use backslashes (\), not forward slashes (/).

If the string does not specify a path, the function uses a standard search strategy to find the file. See the Remarks for more information.
xinen8721 2012-01-18
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 visualeleven 的回复:]

C/C++ code
// 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);
……
[/Quote]
Thank you for your reply. Can you tell what is "myPuts" in Myputs.dll??
I think it is a function name,right??
酒红色的泪 2012-01-18
  • 打赏
  • 举报
回复
必要时候,请阅读MSDN
oyljerry 2012-01-18
  • 打赏
  • 举报
回复
LoadLibrary加载DLL
GetProcAddress获取函数地址,赋值给函数指针
调用函数指针

15,471

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 进程/线程/DLL
社区管理员
  • 进程/线程/DLL社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧