动态调用封装函数类的DLL (C++2008)

YFLK 2013-03-21 03:01:06
我有一个DLL,是外部语言开发的,DLL中的函数以类的形式进行了封装。导出时可通过类进行调用,这个DLL在外部语言中通过动态方式调入并已测试通过,所有功能都有可能“过类+函数名”的方式正常使用。
如类名 MyClass
类中函数 MyClass.Func1( ....)
MyClass.Func2( ....)
MyClass.Func3( ....)
MyClass.Func4( ....)



现在我想通过C++的LoadLibrary、GetProcAddress、FreeLibrary 来实现对这个DLL中类的(动态装入DLL,不要静态装入DLL的方法)使用,如何实现。
类中函数的接口说明已经知道,最好有实例代码。
...全文
155 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
笨Ben奔犇 2013-07-17
  • 打赏
  • 举报
回复
我是来挖坟的··这哥们遇到的问题貌似和我的类似···
YFLK 2013-03-26
  • 打赏
  • 举报
回复
不要这么快就沉下去。期待高人出现!
YFLK 2013-03-25
  • 打赏
  • 举报
回复
一楼的不是我要的,那不是使用DLL中的类。
YFLK 2013-03-24
  • 打赏
  • 举报
回复
试了不行呀。
YFLK 2013-03-22
  • 打赏
  • 举报
回复
看了楼上两位的代码,回家后研究研究。 不明白的是“*PFUNCTION”和“*MYPROC”是类的指针?
shen_wei 2013-03-21
  • 打赏
  • 举报
回复
#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"); 
}
shen_wei 2013-03-21
  • 打赏
  • 举报
回复
看看MSDN就明白了!!
lxpandsq 2013-03-21
  • 打赏
  • 举报
回复

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

using namespace std;
void main(void)
{
	typedef int (__stdcall   *PFUNCTION)( int k,int i);
        //声明参数类型,以后会用到PFUNCTION
	HMODULE hLib = ::LoadLibrary("xx.dll");
	if ( NULL == hLib )	
	{
		perror("装载DLL文件错误:");
	}
	else
	{

		PFUNCTION myAddFunction=myAddFunction=(PFUNCTION)::GetProcAddress(hLib,"ConnectEx");
//ConnectEx是动态库中的方法
		if ( NULL == myAddFunction)	
		{
			perror("装载的DLL文件中无对应的函数:");
		}
		else
		{
		        int k=myAddFunction(1,2);
			cout <<k <<endl;
		}
		::FreeLibrary(hLib);
	}
	
}

我学的是java,这是我研究出来的,

64,282

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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