vb 动态调用DLL
各位vb达人,哥们搞了个vb的dll,现在想动态调一下,正常静态引用该dll没有异常,先贴出来dll代码
dll名称为testclass ,类名为class1
Public Function UpdateMethod(ApplyNo As String) As Integer
Dim aaa As String
Dim bbb As Integer
Dim ccc As MethodForHi.Method
aaa = Trim(ApplyNo)
bbb = ccc.UpdateRecord(aaa, 12)
MsgBox aaa
UpdateMethod = bbb
End Function
注: dll里就这一个方法 ,methodforhi.method是另外一个类
动态引用时,就报错,以下是动态引用的代码
Private Sub Command1_Click()
On Error GoTo errorHandle:
Dim obj As Object
Dim ret As String
Dim fu As Integer
Set obj = CreateObject("testclass.class1")
fu = obj.UpdateMethod("123")
ret = CStr(fu)
MsgBox ret
Set obj = Nothing
Exit Sub
errorHandle:
MsgBox Err.Description & " --"
Err.Clear
End Sub
报的错误信息是:未设置对象变量或 with block 变量--
各位VB大牛,能否指点一二
[解决办法]
ccc有实例化么?
[解决办法]
建议你把错误处理语句先去掉,然后单步调试一下,看看问题出在哪一句上?
dll名称为testclass ,类名为class1
Public Function UpdateMethod(ApplyNo As String) As Integer
Dim aaa As String
Dim bbb As Integer
Dim ccc As MethodForHi.Method
aaa = Trim(ApplyNo)
bbb = ccc.UpdateRecord(aaa, 12)
MsgBox aaa
UpdateMethod = bbb
End Function
Private Sub Command1_Click()
'On Error GoTo errorHandle:
Dim obj As Object
Dim ret As String
Dim fu As Integer
Set obj = CreateObject("testclass.class1")
fu = obj.UpdateMethod("123")
ret = CStr(fu)
MsgBox ret
Set obj = Nothing
Exit Sub
errorHandle:
MsgBox Err.Description & " --"
Err.Clear
End Sub
[解决办法]
Set obj = CreateObject("testclass.class1")
请确认你的声明对不对,格式: 类工程名.类名
[解决办法]
Set obj = CreateObject("MethodForHi.Method")
[解决办法]
Set obj = CreateObject("testclass.class1")
'之后,测试一下
if not obj is nothing then
....
end if
[解决办法]
我也遇到类似问题,不过有不同,同时做了两个动态引用类,第一个正常,第二个出现报错。
关注
------解决方案--------------------
能这么用就不是类,动态引用肯定会错
Dim ccc As MethodForHi.Method
aaa = Trim(ApplyNo)
bbb = ccc.UpdateRecord(aaa, 12)
MsgBox aaa
UpdateMethod = bbb
[解决办法]
dll名称为testclass ,类名为class1
Public Function UpdateMethod(ApplyNo As String) As Integer
Dim aaa As String
Dim bbb As Integer
Dim ccc As MethodForHi.Method
set ccc=new MethodForHi.Method
'ccc可能需要设置参数,如果需要则设置相关参数
aaa = Trim(ApplyNo)
bbb = ccc.UpdateRecord(aaa, 12)
MsgBox aaa
UpdateMethod = bbb
End Function
[解决办法]
Set obj = CreateObject("MethodForHi.Method")
这个才是类名
[解决办法]
看你的代码方面是没问题的.
引用能正常工作,说明DLL及相关库是正常注册了的.
建议你编码初期先不要使用错误捕捉语句,而将错误直接暴露出来,方便除错.
如果平时就加入,那么很多本来简单的错误也会被引得很远,比如拼写错误这些.
从代码来看,错误应该是由fu = obj.UpdateMethod("123")这句引起的.
而到底是因为obj为空(即未成功创建对象),还是因为DLL里面MethodForHi类或其他条件异常造成的,这就看不出来了.
[解决办法]
我也遇到类似的问题!
[解决办法]
Dim ccc As new MethodForHi.Method