Solved

Help! When use byVal or ByRef in declare DLL function

Posted on 1998-12-25
1
296 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

710 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