Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Help! When use byVal or ByRef in declare DLL function

Posted on 1998-12-25
1
Medium Priority
?
311 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 300 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
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…
Suggested Courses

916 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