Link to home
Start Free TrialLog in
Avatar of Torus
Torus

asked on

Help! When use byVal or ByRef in declare DLL function

I have wrote a DLL in C. It has only one function.

Long APIENTRY EncodeData(unsigned char *buffer, char *result, int iActualLen, int EncodeLen)
int I;

for(I = 0; I < EncodeLen; I++){
  If (I < iActualLen){
      // do something
  }
  else{
     // do another thing
  }      
}


What I declare in VB6 is
Private Declare Function EncodeData Lib "MyDLL" (ByVal Encodebuff As String, ByVal Returnbuf As String, ByVal iActualLen As Integer, ByVal iEncodeLen As Integer) As Long

and I wrote a function to call the DLL Encodedata

sub VbEncodeData(EncodeBuff as string, ResultBuf as string)
{
  Dim iActualLen as integer
  ResultBuf= String(1000, 0)  
  iActualLen = Len(EncodeBuff)
  'Then insert more data to Encodebuff  
  Call EncodeData(Encodebuff, Decodebuff, iActualLen, Len(Encodebuf))
End sub

If I do so, the result is wrong! However, if I changed to

Private Declare Function EncodeData Lib "MyDLL" (ByVal Encodebuff As String, ByVal Returnbuf As String,  iActualLen As Integer, ByVal iEncodeLen As Integer) As Long

Without byval in front of iActualLen. There is no problem.

Moreover, some I don't understand if I do the following
(use first style to declare the DLL)

sub PrepareData()
Dim iActualSize  
  iActualSize = Len(Encodebuff)
  VbEncodeData Encodebuff, Resultbuff, iActualSize
end sub

sub VbEncodeData(EncodeBuff as string, ResultBuf as string, iActualLen as integer)
{
  ResultBuf= String(1000, 0)  
  'Then insert more data to Encodebuff  
  Call EncodeData(Encodebuff, Decodebuff, iActualLen, Len(Encodebuf))
End sub

In this case, it doesn't return correct result to me also.
But if I added a Byval in front of the iActualLen parameter
(VbEncodeData) and that's ok.

It really make me confusing. Is it abnormal behaviour or I have do something wrong ? When should I use Byval or Byref
in delaring function in DLL?

Thanks
 


   

ASKER CERTIFIED SOLUTION
Avatar of MikeP090797
MikeP090797

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial