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

前端之家收集整理的这篇文章主要介绍了[VB.NET]请教:vb.net调用vc写的dll出错:报尝试读取或写入受保护的内存前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

请教: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里面怎么写呢
谢谢
__________________________________________________________________________
我把函数改了下,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,1,6}
Dim cl As cardlayout
UltralightSetStaticStruct(string2,cl)
传进去的字符数组我也是这样写的,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 __________________________________________________________________________
原文链接:https://www.f2er.com/vb/264072.html

猜你在找的VB相关文章