【发布时间】:2014-08-02 14:35:45
【问题描述】:
如下面的代码,我声明了一个dll来使用它的函数,成功时返回0,否则返回0。
我需要函数生成的宽度、高度、步幅、长度、字节数,所以我使用 ByRef 来声明它们。当我启动它时它运行良好,但在它显示 3 msgbox 后,Visual Basic 崩溃。
我2天前刚开始vb,我知道这一定是一个菜鸟问题,但我什至不知道问题是什么,所以......帮帮我。
有什么让你们困惑的地方请告诉我,我会马上改正的。
这是 DLL 的纪录片
sn 字符
宽度 int (IN/OUT)
高度 Int (IN/OUT)
字节 char* (IN/OUT)
跨步(进/出)
len int(输入/输出)
Private Declare Function Ict_WMK_ISV Lib "C:\acd.dll" (ByVal PrinterModel As String, ByRef width As Long, ByRef height As Long, ByRef bytes As Variant, ByRef stride As Long, ByRef leng As Long) As Integer
Private Sub Command1_Click()
'Ict_WMK_ISV (Fax)
Dim width As Long
Dim height As Long
Dim stride As Long
Dim leng As Long
Dim bytes As Variant
Dim printmodel As String
Dim iResult As Integer
printmodel = "Samsung SCX-6x45 Series PCL6"
If IsNull(bytes) Then
MsgBox ("null")
Else
MsgBox ("not null")
End If
bytes = Empty
iResult = Ict_WMK_ISV(printmodel, width, height, bytes, stride, leng)
MsgBox (Str(iResult))
End Sub
【问题讨论】:
-
对于代码 'bytes = Empty' 是 'Empty' 在别处声明的变量。我在这里看不到它是一个 VB 关键字。 msdn.microsoft.com/en-gb/library/dd409611.aspx
-
您是否收到任何错误消息?
-
尝试添加一个简单的错误处理程序(参见here - 只需添加
on error goto、标签和Msgbox(err.message)) -
bytes不太可能是Variant,更常见的是返回类型是Long而不是Integer。但是如果没有关于函数Ict_WMK_ISV的文档,那么这些都是猜测。 dll 是否带有文档或头文件? -
我们还有
err.lastdllerror。请注意,您必须On Error Resume Next才能使用它。 LastDLLError 属性仅适用于从 Visual Basic 代码进行的 DLL 调用。当进行这样的调用时,被调用的函数通常会返回一个指示成功或失败的代码,并填充 LastDLLError 属性。检查 DLL 函数的文档以确定指示成功或失败的返回值。每当返回失败代码时,Visual Basic 应用程序应立即检查 LastDLLError 属性。有关详细信息,请参阅 API 调用 SetLastError。
标签: visual-studio vba vb6 basic