如题,在VC中的声明为extern "C" __declspec( dllexport ) short WINAPI Optimize( void* Interface , char **fileName ),fileName为数组,有fileName[0],fileName[1],fileName[2]
我在VB里调用的时候是这么写的Private Declare Function Optimize Lib "Diaoyong.dll" (ByRef Interface As Long, ByRef FileName() As String) As long
可是调用的时候老是VB直接崩溃啊,请大神赐教怎么调用啊 谢谢
我在VB里调用的时候是这么写的Private Declare Function Optimize Lib "Diaoyong.dll" (ByRef Interface As Long, ByRef FileName() As String) As long
可是调用的时候老是VB直接崩溃啊,请大神赐教怎么调用啊 谢谢
Private Declare Function Optimize Lib "Diaoyong.dll" (ByVal Interface As Long, byval FileName() As string) As integer
试试这个
2楼的方法我也尝试过的,但是 byval FileName() As string这么写的话,VB6.0直接报错,说是不允许
byval FileName() As string,必须是byref FileName() As string
虽然繁琐一点,但肯定可以搞定。
Private Declare Function OptimizerSTART Lib "Sakura.dll" (ByVal Interface As Long, ByVal FileName As Long) As IntegerPrivate Sub Optimize_Click() Dim errorcount As Integer
Dim InputFile(2) As String
Dim TestFile(2) As Long
InputFile(0) = "C:\temp\test.txt"
InputFile(1) = "C:\temp\ok.txt"
InputFile(2) = "C:\temp\log.txt"
TestFile(0) = VarPtr(InputFile(0))
TestFile(1) = VarPtr(InputFile(1))
TestFile(2) = VarPtr(InputFile(2))
sakuraInterface = CreateOptimizer
errorcount = Estimate(Interface, TestFile(0))
End Sub
A)如4楼所言,在 VB 中做,不过需要自己处理指针和内存。
B)用 VC 再做一个 DLL 对函数进行封装,变成 3 个 char* 参数,代码简单。
A)方法贴切实际,能提供代码吗
B)方法的话,因为这个DLL我没有代码,无法重新封装
VB代码 调用 封装函数,封装函数 调用 原始函数。
汗,给你说的 StrConv() 函数没用到呢…………
再说,你的那个 .dll 中的函数,应该是ANSI版本的,你也不能直接取VB中的字符串指针来使用。
你参考一下这个代码试试:
Private Declare Function OptimizerSTART Lib "Sakura.dll" (ByVal Interface As Long, ByVal FileName As Long) As IntegerPrivate Sub Optimize_Click() Dim errorcount As Integer
Dim InputFile(2) As String
Dim TestFile(2) As Long
Dim strBufferA() As Byte ' 加上这3个
Dim strBufferB() As Byte
Dim strBufferC() As Byte
InputFile(0) = "C:\temp\test.txt"
InputFile(1) = "C:\temp\ok.txt"
InputFile(2) = "C:\temp\log.txt"' TestFile(0) = VarPtr(InputFile(0))
' TestFile(1) = VarPtr(InputFile(1))
' TestFile(2) = VarPtr(InputFile(2))
' 应该这样进行转换处理:
strBufferA = StrConv(InputFile(i) & vbNullChar, vbFromUnicode)
strBufferB = StrConv(InputFile(i) & vbNullChar, vbFromUnicode)
strBufferC = StrConv(InputFile(i) & vbNullChar, vbFromUnicode)
TestFile(0) = VarPtr(strBufferA(0))
TestFile(1) = VarPtr(strBufferB(0))
TestFile(2) = VarPtr(strBufferC(0))
' 没看到你调用 OptimizerSTART() 这个函数 -_-!
' Interface 这个参数你得正确处理……
Call OptimizerSTART(Interface, VarPtr(TestFile(0))) sakuraInterface = CreateOptimizer
errorcount = Estimate(Interface, TestFile(0))
End Sub
strBufferA = StrConv(InputFile(0) & vbNullChar, vbFromUnicode)
strBufferB = StrConv(InputFile(1) & vbNullChar, vbFromUnicode)
strBufferC = StrConv(InputFile(2) & vbNullChar, vbFromUnicode)
VB会自动吧UNICODE抓成ANSI传出去的
dim interface as string'假设是字串
Optimize strptr(interface),filename由于第一参数定义为long类型,所以获取的指针是unicode编码,不一定能获得正确数据,可以使用
Optimize byval strptr(strconv(interface,vbfromunicode)),filename
这里的参数有问题,传递数组时应该是数组名(数组的第一个下标)