soui 5.0.0.1
Soui5 Doc
 
Loading...
Searching...
No Matches
com-loader.hpp
1#ifndef _COM_LOADER_H_
2#define _COM_LOADER_H_
3
4#include <windows.h>
5#include <interface/obj-ref-i.h>
6#include <string/tstring.h>
7
8namespace SOUI {
9//加载组件辅助类
10//组件需要提供SCreateInstance接口。接口定义必须是funSCreateInstance
11class SComLoader
12{
13 typedef BOOL (*funSCreateInstance)(IObjRef **);
14public:
15 SComLoader()
16 :m_hMod(0)
17 ,m_funCreateInst(NULL)
18 {
19 m_szDllPath[0]=0;
20 }
21 ~SComLoader()
22 {
23 if(m_hMod) FreeLibrary(m_hMod);
24 }
25
26 BOOL CreateInstance(LPCTSTR pszDllPath,IObjRef **ppObj,LPCSTR pszFnName = "SCreateInstance")
27 {
28 SStringT strPath(pszDllPath);
29 if(!m_funCreateInst)
30 {
31 #ifdef _WIN32
32 strPath+=_T(".dll");
33 #else
34 strPath+=_T(".so");
35 #endif
36 m_hMod=LoadLibrary(strPath);
37 if (!m_hMod) {
38#ifndef _WIN32
39 const char * err = dlerror();
40 printf("load so failed, err=%s\n", err);
41#endif
42 return FALSE;
43 }
44 m_funCreateInst=(funSCreateInstance)GetProcAddress(m_hMod,pszFnName);
45 if(!m_funCreateInst)
46 {
47 FreeLibrary(m_hMod);
48 return FALSE;
49 }
50 _tcscpy(m_szDllPath,strPath);
51 }
52 return m_funCreateInst(ppObj);
53 }
54
55 HMODULE GetModule(){
56 return m_hMod;
57 }
58protected:
59 HMODULE m_hMod;
60 funSCreateInstance m_funCreateInst;
61 TCHAR m_szDllPath[MAX_PATH];
62};
63
64}//end of soui
65
66#endif//_COM_LOADER_H_