Solved

Help! When use byVal or ByRef in declare DLL function

Posted on 1998-12-25
1
281 Views
Last Modified: 2012-06-27
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
 


   

0
Comment
Question by:Torus
1 Comment
 
LVL 8

Accepted Solution

by:
MikeP090797 earned 100 total points
ID: 1452453
You always need to use ByVal with strings. The problem with the declaration that in 32bit C++, int is 32bit, and in VB it is 16bit, so you should declare it As Long on the VB side. To prevent confusions, just use LONG everywhere in the dll
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now