请教: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
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里面怎么写呢
谢谢
__________________________________________________________________________
End Function
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
End Function