动态库问题???
我用vc作了一个求加减乘除的动态库文件内容如下:
BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
extern "C" __declspec(dllexport) int add(int x, int y, char chr)
{
int z;
switch ( chr)
{
case '+':
z = x + y;
case '-':
z = x - y;
case '*':
z = x * y;
case '/':
z = x / y;
}
return z;
}
经过编译后没有错误,然后我用vb声明调用如下:
Private Declare Function add Lib "dll.dll" (ByVal x As Integer, ByVal y As Integer, ByVal char As String) As Integer
Private Sub Form_Load()
Dim lod As Long
Dim x, y As Integer
Dim chr As String
On Error Resume Next
x = y = 1
chr = "+"
lod = LoadLibrary("dll.dll")
Label1.Caption = Str(lod)
Label2.Caption = Str(add(x, y, chr))
End Sub
会出现编译错误,类型不匹配,请问该怎么解决?
谢谢
问题点数:10、回复次数:5Top
1 楼krh2001(边城浪子)回复于 2005-05-31 20:03:24 得分 3
add 函数 要声明成 __stdcall 才能被 VB 使用:
int __stdcall add(int x, int y, char chr)
{
...
}
* add 函数要通过 .def 文件导出:
EXPORTS
add @1 PRIVATE
* VB 里声明, 对应的数据类型要分清. C 的 int 实际相当于 VB 的 long
Private Declare Function add Lib "dll.dll" (ByVal x As Long, ByVal y As Long, ByVal char As Byte) As Long
* 调用, 不用 load 进来啊
Private Sub Form_Load()
' Dim lod As Long
Dim x, y As Long
Dim chr As Byte
On Error Resume Next
x = y = 1
chr = Asc("+");
' lod = LoadLibrary("dll.dll")
' Label1.Caption = Str(lod)
Label2.Caption = Str(add(x, y, chr))
End Sub
Top
2 楼ccas(西西)回复于 2005-05-31 20:11:17 得分 0
* add 函数要通过 .def 文件导出:
EXPORTS
add @1 PRIVATE
这个需要手动添加码Top
3 楼ccas(西西)回复于 2005-05-31 21:03:42 得分 0
我试了一下,VB给出提示"实时错误‘53’是什么原因Top
4 楼roscoe(草上飞)回复于 2005-05-31 21:15:59 得分 3
53是Access denies, 访问拒绝,把 EXPORTS中 @Private去掉试试Top
5 楼gohappy_1999(碧水蓝天)回复于 2005-05-31 21:41:14 得分 4
extern "C" __declspec(dllexport) int add(int x, int y, char chr)
这肯定不能被VB调用,调用约定不符合,改为extern "C" __declspec(dllexport) int __stdcall add(int x, int y, char chr)
并在DEF中到出函数名就可以了
EXPORTS
addTop




