我用2005创建了一个默认的动态库,(按照汪兵的《Windows CE嵌入式高级编程及其实例详解(用C++实现)》),但是把要导出的函数,写在另外一个单独的供外部使用的头文件中,就不行了,动态库倒是可以加载,但是函数总找不到入口点!
想请教大家的是:
1. 在 WM6.0+vs2005下,如何创建一个动态库(静态和动态链接MFC)?如果是默认的,就不用说了,导出的有类和函数,稍微复杂一些!
2. 链接动态库时,有什么方法?只能用LoadLibrary()么?然后在一个找到函数的入口点?这样是不是很麻烦?如果导出函数很多呢?能不能像平常我们写dll一样, 只需要添加头文件,然后input动态库,或者用#paraga comment(lib, ""),之类的呢?
好多天了,急求啊!!!
谢谢!
13 个解决方案
#1
1.对于第一个问题,没有什么可说的,就是按照默认的建工程,然后在.def文件里面声明要到处的函数就可以了
2.嫌到处多个函数太麻烦,你可以在dll里面定义一个struct,将要导出的函数都放里面,然后通过一个函数去获得所有的入口,这样就方便了。至于只添加头文件或#paraga comment(lib)这是针对静态库的吧
好运
2.嫌到处多个函数太麻烦,你可以在dll里面定义一个struct,将要导出的函数都放里面,然后通过一个函数去获得所有的入口,这样就方便了。至于只添加头文件或#paraga comment(lib)这是针对静态库的吧
好运
#2
一直用.net,帮顶
#3
说的对,我正是这样做的,只是把struct 换成了 class,写成接口,其他的实现类继承,导出的时候导出这个接口类,这样可以获取该接口类的所有函数,可是,在WM下面好像不行啊!!!同行,试过您说的那种方法么?能不能贴个例子?谢谢!
#4
关于问题2,添加头文件或#paraga comment(lib),可以这么做。
但前提是dll工程的接口没有变化,如果有变化,就需要重新编译调用dll的工程,其实也挺方便的。
关于用类和struct写接口,没有问题,wm上是可行的,你再试试吧
但前提是dll工程的接口没有变化,如果有变化,就需要重新编译调用dll的工程,其实也挺方便的。
关于用类和struct写接口,没有问题,wm上是可行的,你再试试吧
#5
可以,试一试!
#6
用loadlibaray()是动态调用,
#paraga comment(lib, "")这个是静态调用,需要编译dll时生成的lib
http://pcedu.pconline.com.cn/empolder/gj/vc/0509/698632.html
看完这个,估计就没什么问题
#paraga comment(lib, "")这个是静态调用,需要编译dll时生成的lib
http://pcedu.pconline.com.cn/empolder/gj/vc/0509/698632.html
看完这个,估计就没什么问题
#7
C++和C的函数调用约定不同,C++的函数编译后函数名会多了一堆@XS2AG之类的代码,而C的只是加个下划线。所以,要导出函数,最好还是用def文件吧。
#8
struct在wm上跑过,但class没跑过,struct确实可以,所以class没道理不行
#9
DLL工程:
.h文件
#ifndef TESTDLL_H
#define TESTDLL_H
int AddTwoNumber(int x, int y);
int MultiplyTwoNumber(int x, int y);
typedef int(* pfunc)(int, int);
typedef struct TESTFUNC
{
pfunc AddFunc;
pfunc MultiFunc;
}S_TESTFUNC, *PS_TESTFUNC;
#endif
.def文件
LIBRARY "TestDll"
EXPORTS
GetFuncList
AddTwoNumber
MultiplyTwoNumber
.cpp文件
#include "stdafx.h"
#include "TestDll.h"
#include <windows.h>
#include <commctrl.h>
S_TESTFUNC g_STestFunc = {
&AddTwoNumber,
&MultiplyTwoNumber
};
BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
return TRUE;
}
void GetFuncList(PS_TESTFUNC *psFunc)
{
*psFunc = &g_STestFunc;
}
int AddTwoNumber(int x, int y)
{
return(x+y);
}
int MultiplyTwoNumber(int x, int y)
{
return(x*y);
}
测试工程:
.h文件
typedef int(* pfunc)(int, int);
typedef struct TESTFUNC
{
pfunc AddFunc;
pfunc MultiFunc;
}S_TESTFUNC, *PS_TESTFUNC;
typedef int(* pGetfunc)(PS_TESTFUNC *psFunc);
.cpp文件
PS_TESTFUNC g_psGetFunc;
HINSTANCE hDll;
pGetfunc GetFucList = NULL;
hDll = LoadLibrary(L"\\storage card\\TestDll.dll");
if(hDll != NULL)
{
GetFucList = (pGetfunc)GetProcAddress(hDll, L"GetFuncList");
}
if (NULL != GetFucList)
{
GetFucList(&g_psGetFunc);
}
if (g_psGetFunc)
{
int ret = g_psGetFunc->AddFunc(3, 5);
printf("%d\n", ret);
ret = g_psGetFunc->MultiFunc(3, 5);
printf("%d\n", ret);
}
不知道为什么要用class,若真想用,将struct改为class即可,注意class的访问权限
.h文件
#ifndef TESTDLL_H
#define TESTDLL_H
int AddTwoNumber(int x, int y);
int MultiplyTwoNumber(int x, int y);
typedef int(* pfunc)(int, int);
typedef struct TESTFUNC
{
pfunc AddFunc;
pfunc MultiFunc;
}S_TESTFUNC, *PS_TESTFUNC;
#endif
.def文件
LIBRARY "TestDll"
EXPORTS
GetFuncList
AddTwoNumber
MultiplyTwoNumber
.cpp文件
#include "stdafx.h"
#include "TestDll.h"
#include <windows.h>
#include <commctrl.h>
S_TESTFUNC g_STestFunc = {
&AddTwoNumber,
&MultiplyTwoNumber
};
BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
return TRUE;
}
void GetFuncList(PS_TESTFUNC *psFunc)
{
*psFunc = &g_STestFunc;
}
int AddTwoNumber(int x, int y)
{
return(x+y);
}
int MultiplyTwoNumber(int x, int y)
{
return(x*y);
}
测试工程:
.h文件
typedef int(* pfunc)(int, int);
typedef struct TESTFUNC
{
pfunc AddFunc;
pfunc MultiFunc;
}S_TESTFUNC, *PS_TESTFUNC;
typedef int(* pGetfunc)(PS_TESTFUNC *psFunc);
.cpp文件
PS_TESTFUNC g_psGetFunc;
HINSTANCE hDll;
pGetfunc GetFucList = NULL;
hDll = LoadLibrary(L"\\storage card\\TestDll.dll");
if(hDll != NULL)
{
GetFucList = (pGetfunc)GetProcAddress(hDll, L"GetFuncList");
}
if (NULL != GetFucList)
{
GetFucList(&g_psGetFunc);
}
if (g_psGetFunc)
{
int ret = g_psGetFunc->AddFunc(3, 5);
printf("%d\n", ret);
ret = g_psGetFunc->MultiFunc(3, 5);
printf("%d\n", ret);
}
不知道为什么要用class,若真想用,将struct改为class即可,注意class的访问权限
#10
学习学习~~
#11
谢谢各位了!回去学习一下,回来给分!
#12
顶顶..学习...
#13
#1
1.对于第一个问题,没有什么可说的,就是按照默认的建工程,然后在.def文件里面声明要到处的函数就可以了
2.嫌到处多个函数太麻烦,你可以在dll里面定义一个struct,将要导出的函数都放里面,然后通过一个函数去获得所有的入口,这样就方便了。至于只添加头文件或#paraga comment(lib)这是针对静态库的吧
好运
2.嫌到处多个函数太麻烦,你可以在dll里面定义一个struct,将要导出的函数都放里面,然后通过一个函数去获得所有的入口,这样就方便了。至于只添加头文件或#paraga comment(lib)这是针对静态库的吧
好运
#2
一直用.net,帮顶
#3
说的对,我正是这样做的,只是把struct 换成了 class,写成接口,其他的实现类继承,导出的时候导出这个接口类,这样可以获取该接口类的所有函数,可是,在WM下面好像不行啊!!!同行,试过您说的那种方法么?能不能贴个例子?谢谢!
#4
关于问题2,添加头文件或#paraga comment(lib),可以这么做。
但前提是dll工程的接口没有变化,如果有变化,就需要重新编译调用dll的工程,其实也挺方便的。
关于用类和struct写接口,没有问题,wm上是可行的,你再试试吧
但前提是dll工程的接口没有变化,如果有变化,就需要重新编译调用dll的工程,其实也挺方便的。
关于用类和struct写接口,没有问题,wm上是可行的,你再试试吧
#5
可以,试一试!
#6
用loadlibaray()是动态调用,
#paraga comment(lib, "")这个是静态调用,需要编译dll时生成的lib
http://pcedu.pconline.com.cn/empolder/gj/vc/0509/698632.html
看完这个,估计就没什么问题
#paraga comment(lib, "")这个是静态调用,需要编译dll时生成的lib
http://pcedu.pconline.com.cn/empolder/gj/vc/0509/698632.html
看完这个,估计就没什么问题
#7
C++和C的函数调用约定不同,C++的函数编译后函数名会多了一堆@XS2AG之类的代码,而C的只是加个下划线。所以,要导出函数,最好还是用def文件吧。
#8
struct在wm上跑过,但class没跑过,struct确实可以,所以class没道理不行
#9
DLL工程:
.h文件
#ifndef TESTDLL_H
#define TESTDLL_H
int AddTwoNumber(int x, int y);
int MultiplyTwoNumber(int x, int y);
typedef int(* pfunc)(int, int);
typedef struct TESTFUNC
{
pfunc AddFunc;
pfunc MultiFunc;
}S_TESTFUNC, *PS_TESTFUNC;
#endif
.def文件
LIBRARY "TestDll"
EXPORTS
GetFuncList
AddTwoNumber
MultiplyTwoNumber
.cpp文件
#include "stdafx.h"
#include "TestDll.h"
#include <windows.h>
#include <commctrl.h>
S_TESTFUNC g_STestFunc = {
&AddTwoNumber,
&MultiplyTwoNumber
};
BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
return TRUE;
}
void GetFuncList(PS_TESTFUNC *psFunc)
{
*psFunc = &g_STestFunc;
}
int AddTwoNumber(int x, int y)
{
return(x+y);
}
int MultiplyTwoNumber(int x, int y)
{
return(x*y);
}
测试工程:
.h文件
typedef int(* pfunc)(int, int);
typedef struct TESTFUNC
{
pfunc AddFunc;
pfunc MultiFunc;
}S_TESTFUNC, *PS_TESTFUNC;
typedef int(* pGetfunc)(PS_TESTFUNC *psFunc);
.cpp文件
PS_TESTFUNC g_psGetFunc;
HINSTANCE hDll;
pGetfunc GetFucList = NULL;
hDll = LoadLibrary(L"\\storage card\\TestDll.dll");
if(hDll != NULL)
{
GetFucList = (pGetfunc)GetProcAddress(hDll, L"GetFuncList");
}
if (NULL != GetFucList)
{
GetFucList(&g_psGetFunc);
}
if (g_psGetFunc)
{
int ret = g_psGetFunc->AddFunc(3, 5);
printf("%d\n", ret);
ret = g_psGetFunc->MultiFunc(3, 5);
printf("%d\n", ret);
}
不知道为什么要用class,若真想用,将struct改为class即可,注意class的访问权限
.h文件
#ifndef TESTDLL_H
#define TESTDLL_H
int AddTwoNumber(int x, int y);
int MultiplyTwoNumber(int x, int y);
typedef int(* pfunc)(int, int);
typedef struct TESTFUNC
{
pfunc AddFunc;
pfunc MultiFunc;
}S_TESTFUNC, *PS_TESTFUNC;
#endif
.def文件
LIBRARY "TestDll"
EXPORTS
GetFuncList
AddTwoNumber
MultiplyTwoNumber
.cpp文件
#include "stdafx.h"
#include "TestDll.h"
#include <windows.h>
#include <commctrl.h>
S_TESTFUNC g_STestFunc = {
&AddTwoNumber,
&MultiplyTwoNumber
};
BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
return TRUE;
}
void GetFuncList(PS_TESTFUNC *psFunc)
{
*psFunc = &g_STestFunc;
}
int AddTwoNumber(int x, int y)
{
return(x+y);
}
int MultiplyTwoNumber(int x, int y)
{
return(x*y);
}
测试工程:
.h文件
typedef int(* pfunc)(int, int);
typedef struct TESTFUNC
{
pfunc AddFunc;
pfunc MultiFunc;
}S_TESTFUNC, *PS_TESTFUNC;
typedef int(* pGetfunc)(PS_TESTFUNC *psFunc);
.cpp文件
PS_TESTFUNC g_psGetFunc;
HINSTANCE hDll;
pGetfunc GetFucList = NULL;
hDll = LoadLibrary(L"\\storage card\\TestDll.dll");
if(hDll != NULL)
{
GetFucList = (pGetfunc)GetProcAddress(hDll, L"GetFuncList");
}
if (NULL != GetFucList)
{
GetFucList(&g_psGetFunc);
}
if (g_psGetFunc)
{
int ret = g_psGetFunc->AddFunc(3, 5);
printf("%d\n", ret);
ret = g_psGetFunc->MultiFunc(3, 5);
printf("%d\n", ret);
}
不知道为什么要用class,若真想用,将struct改为class即可,注意class的访问权限
#10
学习学习~~
#11
谢谢各位了!回去学习一下,回来给分!
#12
顶顶..学习...