读书人

VB调用DLL遇到的有关问题

发布时间: 2012-01-22 22:38:43 作者: rapoo

VB调用DLL,遇到的问题
这是源函数 DLL里面的。
void __stdcall ChannelPower_2(float I[], long Len_I, float Q[], long Len_Q,
float LeavelCorrection, short SamplingRatio, short SYNCDLID,
short ScramblingCodeID, short MaxmumUsers, short User, long FFTSize,
float *ChannelPower);
我在VB里面是这样申明的:
Option Explicit
Private Declare Function ChannelPower_2 Lib "TD Test.dll " (ByRef i() As Single, ByRef leni As Long, ByRef q() As Single, ByRef lenq As Long, ByRef level As Single, ByRef sampRa As Integer, ByRef SYN As Integer, ByRef CodeI As Integer, ByRef MaxU As Integer, ByRef user As Integer, ByRef FFTSize As Long, ByRef POWER As Single)
调用代码如下:

Dim i(25536) As Single
Dim q(25536) As Single
Dim path_i As String
Dim path_q As String
Dim POWER As Single
Private Sub Command1_Click()

path_i = "C:\\I.TXT "
'读取i.txt文件
Dim num As Long
Dim readfile As String
Open path_i For Input As #1
Do Until EOF(1)
Line Input #1, readfile
If EOF(1) Then Exit Do
i(num) = CSng(readfile)
num = num + 1
Loop
Close #1
'读取Q。TXT文件
num = 0
readfile = " "
path_q = "c:\\Q.txt "
Open path_q For Input As #2
Do Until EOF(2)
Line Input #2, readfile
If EOF(2) Then Exit Do
q(num) = CSng(readfile)
num = num + 1
Loop
Close #2
num = ChannelPower_2(i, 25536, q, 25536, 0, 24, 0, 0, 8, 1, 4096, POWER) '调用DLL函数
Text1.Text = CStr(POWER) '返回函数值
End Sub

执行程序时,提示VB遇到问题需要关闭。这是怎么回事,是我的函数申明错了吗


------解决方案--------------------


写法应该没有什么问题,这个类库内部是否有问题就很难说了。

==================================================================
博客空间:http://blog.csdn.net/lovingkiss
资源下载:http://download.csdn.net/user/lovingkiss
Email:loving-kiss@163.com
优惠接单开发,收费带初学者,组件控件定制开发,成品源代码批发
联系方式:Q64180940(请清楚注明业务还是技术咨询) 全天在线
==================================================================
[解决办法]
API用得比较少
会不会是ChannelPower_2 Lib "TD Test.dll " 这个地方的问题?dll的文件名有一个空格,没见过类似于这样的,不知道会不会是因为这个原因.^_^
[解决办法]
空格和byref应该引发不了该问题现象的,一般这种情况都是崩溃了才有的,调用不当,大多数只是执行有问题的阿~~
[解决办法]
参数定义有问题,
void __stdcall ChannelPower_2(float I[], long Len_I, float Q[], long Len_Q,
float LeavelCorrection, short SamplingRatio, short SYNCDLID,
short ScramblingCodeID, short MaxmumUsers, short User, long FFTSize,
float *ChannelPower);
vb不会 ,给个C#的
public extern static void ChannelPower_2( System.IntPtr I, int Len_I, System.IntPtr Q[], int Len_Q,
float LeavelCorrection, short SamplingRatio, short SYNCDLID,
short ScramblingCodeID, short MaxmumUsers, short User, int FFTSize,
System.IntPtr ChannelPower);
最后一个参数不知道是入参还是出参,所以给出个不完全的调用方案
float[] a = new float[10];
System.IntPtr p = Marshal.UnsafeAddrOfPinnedArrayElement( a , 0);
float[] b = new float[10];
System.IntPtr p1= .....//同上

ChannelPower_2( p , 10 , p1 , 10 , ......



[解决办法]
把Long改为Integer
如:Private Declare Function ChannelPower_2 Lib "TD Test.dll " (ByRef i() As Single, ByRef leni As Integer, ByRef q() As Single, ByRef lenq As Integer, ByRef level As Single, ByRef sampRa As Integer, ByRef SYN As Integer, ByRef CodeI As Integer, ByRef MaxU As Integer, ByRef user As Integer, ByRef FFTSize As Integer, ByRef POWER As Single)

读书人网 >VB Dotnet

热点推荐