Home > Software engineering >  About C do dynamic library function called out the problem
About C do dynamic library function called out the problem

Time:09-15

Example: the dynamic library is int ABC (int a, b unsigned char *, unsigned char * c, int * d)

Into ginseng is a, b 11 bytes (agreement) and is c, d key is out and did not say how many bytes, d value is cc number of bytes ()

I am such statement
Public Declare Function JM Lib "TZhuZhanDLL. DLL" (ByVal a As Long, _
ByRef b As Byte, _
ByRef c As Byte, _
ByRef d As Long As Long)

Use it, how how to define the d, I know the address, the d is also dim forD as long like this?

Dim aa as long
Dim bb (10) as byte 'convention is 11 bytes
Dim cc () as byte '
without the agreementDim dd as long 'not agreed, this value is that the cc number of bytes ()

Aa=1
For I=0 to 10
Bb (I)=I + 10 'I literally got
Next
Bb (
The select case JM ((0) aa, bb, cc (0), dd)
Case 0
'success
Case1
'failure
End the select

Now another run to VB tore rout out of here, my monitor can monitor, I don't know is that in a dynamic library without showing the number of bytes refs, how to value, how to define the d, use byref?

CodePudding user response:

According to the "prototype", your API in VB6, the statement and the corresponding call way is no problem in theory,


But the parameter c is for the use of "output", its function description, there should be a "need to how much space",
How much space or how to "get" instructions (such as c to NULL) yeah,


On the other hand, however, your VB code, cc () is a dynamic array.
When you call API "before using" did not assign data space,
Should be "subscript crossing the line" a runtime error to ah, how is the process of "crash"!
Really a little don't understand...

In addition, in the current "is not clear how much", according to your application scenario, should also not use a lot of space,
Then you can be allocated a "very rich" space to give it a try...
Before calling the API, such as redim cc (1023) or redim cc (4095), and so on,
And see if I can perform well,

If you can run normally, and see how it is used in space;
Is "fixed size" or change (what do you want to use different "input number" test),
If the fixed allocation according to the actual needs; If change, the use of a "than the maximum slightly larger number of space,

CodePudding user response:

Array cc not initialized

CodePudding user response:

refer to the second floor chewinggum response:
array caused by cc not initialize


But in general, it is should prompt "subscript crossing the line" to ah,

CodePudding user response:

reference 3/f a toast to invite the bright moon response:
Quote: refer to the second floor chewinggum response:

Array caused by cc not initialize


But in general, it is should prompt "subscript crossing the line" to ah,



Not VB own use, API directly is written in the address 0 or what content, skip the VB error captured, should be like this

CodePudding user response:

reference 5 floor widely spread-and deeply felt he reply:
Quote: refer to the third floor a toast to invite the bright moon response:

Quote: refer to the second floor chewinggum response:

Array caused by cc not initialize


But in general, it is should prompt "subscript crossing the line" to ah,



Not VB own use, API directly is written in the address 0 or what content, skip the VB error captured, should be

The horse back?
I rarely come here recently,

You take a closer look at the building code, he put the API parameters statement as "Byte type reference";
Variable cc () is a dynamic array, JM () before the call, not for "Redim"!
In this way, the execution of the select case JM ((0) aa, bb, cc (0), dd) this sentence,
First of all should be cc (0) lead to "subscript crossing the line" here, unable to enter the API to perform,

CodePudding user response:

This process of collapse, there is only one may occur:
Compile time, choose the "phased array bounds checking,
And to run the compiled exe (IDE, will always be to check an array),
And to also have no place within the API "invalid pointer" check, will collapse caused by illegal memory access process,


Words... The original poster is missing, what should we say also...

CodePudding user response:

May be a function declaration is wrong;

C language function declaration:
Int ABC (int a, unsigned char * b, unsigned char * c, int * d)

VB6 function declaration:
Public Declare Function JM Lib "TZhuZhanDLL. DLL" (ByVal a As Long, _
ByRef b As String, _
ByRef c As String, _
ByRef d As Long As Long)

Reference:
https://blog.csdn.net/milaoshu1020/article/details/89057300

CodePudding user response:

Update, should use the ByVal type String;

C language function declaration:
Int ABC (int a, unsigned char * b, unsigned char * c, int * d)

VB6 function declaration:
Public Declare Function JM Lib "TZhuZhanDLL. DLL" (ByVal a As Long, _
As String, ByVal b _
As String, ByVal c _
ByRef d As Long As Long)

Reference:
https://blog.csdn.net/milaoshu1020/article/details/89057300
  •  Tags:  
  • API
  • Related