Home > Software engineering >  Standard DLL, VB6 dynamic invocation returns the code and converted to the correct result.
Standard DLL, VB6 dynamic invocation returns the code and converted to the correct result.

Time:10-08

Such as: the DLL file, I have some standards, to dynamically loading in vb6, and obtain the return value correctly, and for processing,
Found on the Internet a method to return a string, but the returned is garbled, has carried on the transformation using CopyMemory, but total collapse, don't know why, so please eldest brother, eldest sister-in-law, the great god give some directions, thank you,
QQ: 2574155801
I don't know how to upload attachments, can add my qq to you preach, thanks everyone a great god,
Posting for the first time, did not see where to set the scores, if the problem settled, I will add some more points,

CodePudding user response:

Gibberish, should be a coding problem,
CopyMemory () after the program crashes, is that you failed to apply the API (mainly parameter wrong),

CodePudding user response:

 Option Explicit 
'call standard DLL function, and returns a value to the caller
Private Declare Function GetModuleHandle Lib "kernel32" Alias "GetModuleHandleA" (ByVal lpModuleName As String) As Long
Private Declare Function LoadLibrary Lib "kernel32" Alias "LoadLibraryA" (ByVal lpLibFileName As String) As Long
Private Declare Function GetProcAddress call Lib "kernel32" (ByVal hModule As Long, ByVal lpProcName As String) As Long
Private Declare Function FreeLibrary Lib "kernel32" (ByVal hLibModule As Long) As Long
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any Source, As Any, ByVal Length As Long)
Private Declare Sub ZeroMemory Lib "kernel32" Alias "RtlZeroMemory" (ByVal dest As Any, ByVal numbytes As Long)

'function: dynamic invocation standard DLL function, and obtain the return value
'parameters:
'strPluginPathFileName: DLL file path
Are called 'strFunctionName: DLL function name
Are called 'strArg1 ~ strArg5: DLL function parameters (the example for the string type)
': string (return standard DLL function return value)
The Public Function GetPluginInfo (ByVal strPluginPathFileName As String, ByVal strFunctionName As String, Optional ByRef strArg1 As String="", Optional ByRef strArg2 As String=" ", Optional ByRef strArg3 As String="", Optional ByRef strArg4 As String=" ", Optional ByRef strArg5 As String="") As String
Dim hModule As Long 'module handles
Dim pFunction As Long 'function address
Dim pContent As Long 'plug-in return a pointer to the content
Dim tmpContent temporary store of content As the String '

'On the Error Resume Next
HModule=LoadLibrary (strPluginPathFileName)
PFunction=GetProcAddress call (hModule, strFunctionName)
Dim tmpinfo As New FunctionPtr
Dim tmpFunction As Object
If strArg1="And" strArg2="" And strArg3=And strArg4=" "" "strArg5="" Then
The Set tmpFunction=tmpinfo. Create (pFunction vbString)
CopyMemory tmpContent, StrPtr (tmpFunction Invoke ()), 4
The Else
The Set tmpFunction=tmpinfo. Create (pFunction vbString, vbString, vbString, vbString, vbString, vbString)
CopyMemory tmpContent, StrPtr (tmpFunction. Invoke (strArg1 strArg2, strArg3, strArg4, strArg5)), 4
The Debug. Print tmpContent
End the If
FreeLibrary hModule
GetPluginInfo=tmpContent
The Set tmpinfo=Nothing
The Set tmpFunction=Nothing
End the Function

CodePudding user response:

But, just don't know how to convert, saw & lt; In accordance with the USES, but is not, don't know the problem,

CodePudding user response:

And return of the gibberish, not directly to assign a value to a string variable (hint overflow), nor use strconv transform (also prompt overflow)

CodePudding user response:

I was using a mobile phone, the code is not convenient, more can't test your code,
You said you watch the "tricks", obviously didn't read it!
According to your CopyMemory function declarations, kao string, with it must be:
CopyMemory byval STRPTR (DD), byval STRPTR (SS), the number of bytes
DD is the type String variables in VB6, must according to "the number of bytes before CopyMemory ascribed enough length value (generally use String () or space ()),
Has enough length can not when the value of the handle (see requirements),
If SS is in the API function return values, it mostly ANSI code,
Then again with Strconv () to convert a it down to other variables, or output,
No conversion will be garbled,

You said Strconv () is also "overflow",
I can only say, brothers, you hit a ghost...

CodePudding user response:

The length of the string can't find it out, has long have short
But your CopyMemory byval STRPTR (DD), byval STRPTR (SS), bytes, I also tried, failed, brother,

CodePudding user response:

Is in accordance with the right method and exactly like the one he can his line, I can't, the only difference is that my string is a DLL returned gibberish, he is himself the input text,
Below is the test code links, thank you understand friend see,
http://yunpan.cn/QiiCMEuEwLM6J access password 132 b

CodePudding user response:

Only the arithmetic results "overflow",
String manipulation, which to overflow the truth!

Most meeting prompt "insufficient memory" (or similar this meaning), illegal operation issues such as,

CodePudding user response:

By the way, I call the DLL is easy language to write, he returns the text type
, he is not as easy language as the "pinyin. DLL" to return a string into memory, then there is no question of call in vb

CodePudding user response:

How could the length of the string "no"?
You use copymemory, don't you will pass the third parameter?
Its value is the number of bytes, according to its value as a string length, allocation of space not less!

Your problem I don't want to say more, just see if you can really understand,

CodePudding user response:

Sorry, was hoping to catch a figure to look at you, and the results not out of the overflow error now, and strconv is directly to hang up,
But before the tip is overflow error,

CodePudding user response:

Ah,, doesn't it is four bytes long and string, and I will direct transfer 4 bytes,, but more than 4 bytes programs are also hang up directly

CodePudding user response:

Yes, I also according to return to the length of the string to test, but are directly hang up, don't know what's wrong, and len and lenb also tried, hang up, ah ~ ~ ~ no way can only change method,

CodePudding user response:

Very few people with VB

CodePudding user response:

Well, less of choose and employ persons, but there are people in use,
Find a personal look at the, said in his run there is no problem, also don't collapse, it's strange, but I use is not
.

CodePudding user response:

Tip is out of memory or a string of space, that would be too long,, but also didn't return to how the problem is,

CodePudding user response:

nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related