Solved

Help! When use byVal or ByRef in declare DLL function

Posted on 1998-12-25
1
279 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
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.
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…
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…

708 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

17 Experts available now in Live!

Get 1:1 Help Now