|
-
Oct 5th, 2007, 01:35 AM
#1
Thread Starter
Lively Member
[RESOLVED] Conversion of C Function to VB Function
Hi All,
I had one declaration from a dll built in C as:
typedef struct
{
char sName[10];
}NAME;
NAME arrNames[20];
==================================================
Function declaration:
long FindNames(long Records, NAME *rNames)
After calling this function by passing the array pointer to arrNames
it fills up the arrNames.
==================================================
The converted function declaration in VB as follows:
Dim arrNames(20) as String
Declare Function FindNames(ByVal Records as Long,ByRef rNames() as String) As Long
But when I called this function the application gets crash, I suppose the conversion is not proper and thats why I am not getting expected result.
Can someone help me out to get rid from this problem?
-
Oct 5th, 2007, 01:52 AM
#2
Re: Conversion of C Function to VB Function
It would help to post your code you are using in the function and how you are calling it. You leave EVERYTHING open to speculation...
-
Oct 5th, 2007, 02:28 AM
#3
Thread Starter
Lively Member
Re: Conversion of C Function to VB Function
Hi Randem,
As I use third party Dll I have only the function definations
and the declaration for the function FindNames in C is as bellow:
typedef struct
{
char sName[10];
}NAME;
NAME arrNames[20];
==================================================
Function declaration from a worker dll:
long FindNames(long Records, NAME *rNames)
After calling this function by passing the array pointer to arrNames
it fills up the arrNames.
==================================================
I converted the Definations of this function for VB6 and called in my
application as follows:
Dim arrNames(20) as String
Private Declare Function FindNames Lib "Worker.dll" _
(ByVal Records as Long,ByRef rNames() as String) As Long
Private Sub cmdGetNames_Click()
Dim retValue as long
'After calling the function application gets crashed with vb6 error
retValue=FindNames(10,arrNames())
'==================================================
'Add Returned Names to List Box
Dim iCnt as Integer
For iCnt = Lbound(arrNames) To Ubound(arrNames)
List1.AddItem arrNames(iCnt)
Next
End Sub
-
Oct 5th, 2007, 02:40 AM
#4
Re: Conversion of C Function to VB Function
Please post you code using the code tags to make it more readable...
The definition are not the same for your second parameter. You have a fixed length parameter for the C call and a variable length parameter for th eVB call. Are you sure the first parameter is passed ByVal and not ByRef?
You probably need something like this for the second parameter:
Code:
Private Type NAME
sName(10) as Byte ' or sName as String(10) I believe this will work
End Type
Dim arrNames(20) as Name
-
Oct 5th, 2007, 09:31 AM
#5
Frenzied Member
Re: Conversion of C Function to VB Function
There are several problems with your converted code.
Dim arrNames(20) as String actually creates an array of 21 (0 to 20) BSTRs, not strings. A BSTR is a 4-byte 'pointer' to a string that is located somewhere else in memory. Change the declaration to Dim arrNames(20) as String * 10 and the strings will actually reside in contiguous memory beginning at arrNames(0). arrNames will then look like a C string array.
In the C code, rNames is a pointer of type NAMES and a pointer is just a Long that contains the address of the variable that it is pointing to.
The declaration for Function FindNames is typical for many C functions in that there is an address (rNames) for some data type and a variable (Records) giving the number of occurances. You should be okay there as long as you don't pass anything greater than UBound(arrNames). However, the second parameter needs some work. I would try something like the following:
Code:
Declare Function FindNames(ByVal Records As Long, rNames As Any) As Long
Dim rNames As Long
rNames = VarPtr(arrNames(0))
retValue = FindNames(10, ByVal rNames)
I believe this should work. I'm not as familiar with the inner workings of VB as I am with C, so it's possible that I have missed something.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|