首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

动态调用打包函数类的DLL (C++2008)

2013-04-07 
动态调用封装函数类的DLL (C++2008)我有一个DLL,是外部语言开发的,DLL中的函数以类的形式进行了封装。导出

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



现在我想通过C++的LoadLibrary、GetProcAddress、FreeLibrary 来实现对这个DLL中类的(动态装入DLL,不要静态装入DLL的方法)使用,如何实现。
类中函数的接口说明已经知道,最好有实例代码。
dll c++ 语言
[解决办法]


#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,这是我研究出来的,
[解决办法]
#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"); 
}

热点排行