[VB.NET]请教:vb.net调用vc写的dll出错:报尝试读取或写入受保护的内存

VB.NET源码-156个实用实例哦…… <script type="text/javascript"> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
请教:vb.net调用vc写的dll出错:报尝试读取或写入受保护的内存
vb,net调用dll中的unsigned char getbitchar(IN struct st cl,OUT unsigned char b[]),报尝试读取或写入受保护的内存。这通常指示其他内存已损坏。
这个函数在vb.net下应该怎么写?
__________________________________________________________________________
谢谢大家了
__________________________________________________________________________
传值/传址错了?
__________________________________________________________________________
这个函数声明的时候应该怎么写?
__________________________________________________________________________
1.首先你要定义好结构体

VB.NET code
_
Public Structure st
''INT型
Dim countAs Int32
''Char[]数组类型
_
Dim string() As Byte
End Structure



2.定义方法:

VB.NET code
Public Module TestModule
CallingConvention:=CallingConvention.Cdecl)> _
Public Function getbitchar(ByVal cl As st, ByRef b As Byte()) As Byte
End Function
End Module
__________________________________________________________________________
up一下,明天再试
__________________________________________________________________________
我把函数改了下,这样的应该怎么写呢
void SetStaticStruct(IN unsigned char szChar[17], OUT struct cardlayout *cl)
我按照whislly的写法,在vb.net写的调用函数定义的 Dim byte_ResultMsg(128) As Byte.
提示算术运算导致溢出.我这是写的:byte_ResultMsg(16)="1234567890123456";
void getbitchar(IN struct cardlayout *cl,OUT unsigned char b[])
这个函数提示: PInvoke 限制: 无法返回变量。
vb.net里面怎么写呢
谢谢
__________________________________________________________________________
我把函数改了下,这样的应该怎么写呢
void SetStaticStruct(IN unsigned char szChar[17], OUT struct cardlayout *cl)
我按照whislly的写法,在vb.net写的调用函数定义的 Dim byte_ResultMsg(128) As Byte.
提示算术运算导致溢出.我这是写的:byte_ResultMsg(16)="1234567890123456";
void getbitchar(IN struct cardlayout *cl,OUT unsigned char b[])
这个函数提示: PInvoke 限制: 无法返回变量。
vb.net里面怎么写呢
谢谢
__________________________________________________________________________
Public Shared Function SetStaticStruct(ByRef str() As Byte, ByRef cl As cardlayout)

End Function
Public Shared Function getbitchar(ByRef cl As cardlayout, ByRef str() As Byte)
End Function
应该可以
__________________________________________________________________________
_
Public Function getbitchar(ByRef cl As cardlayout, ByVal b As StringBuilder) As Byte
End Function
调用时,要先初始化结构体里的数组,并且要先给b分配内存空间,Dim b As StringBuilder = New StringBuilder(17)

_
Public Sub SetStaticStruct(ByVal szChar As string, ByRef cl As cardlayout)
End Function
如果在SetStaticStruct里要改变szChar的值,可以用StringBuilder
__________________________________________________________________________
最好把你在VC里写的函数和结构体的定义、和在VB里结构体及方法的定义,调用的代码贴上来,帮你看看什么地方写错了。
__________________________________________________________________________
Public Structure cardlayout
Dim a As Int16
Dim b As Int16
Dim c As Int16
Dim d As Int16
Dim e As Int16
Dim f As Int16
Dim g As Int16
Dim h As Int16
Dim i As Int16
Dim j As Int16
Dim k As Int16
Dim l As Int16
Dim m As Int16
Dim n As Integer
End Structure
struct UltralightStaticcardlayout
{
unsigned int cardBaseDateTime;
unsigned int KeyVersion;
unsigned int TokenDuration;
unsigned int TestFlag;
unsigned int LanguageBit;
unsigned int SaleStationCode;
unsigned int PhysicalTokenType;
unsigned int LogicalTokenType;
unsigned int FareTier;
unsigned int SouvenirFlag;
unsigned int AmountofTheTicket;
unsigned int BatchInitCode;
unsigned int IssueDate;
unsigned long TokenMAC1;
};
__________________________________________________________________________
调用没问题了,但是结果不一样,Dim string2() As Byte = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6}
Dim cl As cardlayout
UltralightSetStaticStruct(string2, cl)
传进去的字符数组我也是这样写的,Dim string2() As Byte = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6},不知道对不对,返回的结果和在vc下面不一样
__________________________________________________________________________
Public Structure cardlayout
Dim a As Int16 ===>int32
Dim b As Int16 ===>int32
Dim c As Int16 ===>int32
Dim d As Int16 ===>int32
Dim e As Int16 ===>int32
Dim f As Int16 ===>int32
Dim g As Int16 ===>int32
Dim h As Int16 ===>int32
Dim i As Int16 ===>int32
Dim j As Int16 ===>int32
Dim k As Int16 ===>int32
Dim l As Int16 ===>int32
Dim m As Int16 ===>int32
Dim n As Integer
End Structure


Public Shared Function SetStaticStruct(Byval str() As Byte, ByRef cl As cardlayout)

End Function
Public Shared Function getbitchar(ByRef cl As cardlayout, ByValstr() As Byte)
End Function
__________________________________________________________________________
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值