VB and VC Communication Preliminary Study (2)

zhaozj2021-02-16  47

In the process of implementing VB and VC through the DLL, the general simple communication process is easy to establish. In "VB and VC Communication (1)", I have described basic communication process, but sometimes, the development of the event is not always smooth, when we think it can easily implement VB and VC When communicating, this is often happening, or let's take a look at the example.

Program in the VC:

Extern "C" _Declspec (DLLEXPORT) BOOL ReadString (LPCSTR LPSTRING)

{

CHAR STR [] = "Hello World!";

IF (strcmp (lpstring, str) == 0)

Return True;

Else

Return False;

}

Programs in VB:

Private Declare Function ReadString Lib "commication.dll" (byval send as string) AS Boolean

........................

Dim Result As Boolean

DIM Send As String

Send = "Hello, World!"

Result = readstring (Send)

IF results

Msgbox "The return value is 'true'"

Else

Msgbox "The Return Value IS 'FALSE'"

END IF

You can run this program, the same, to realize the function of communication, but here there are several places should cause us to pay attention, first of all, when the string parameters passed in VB, according to regular understanding, it seems to be It is the pass address, but the method used here is byval, why? The reason is more complicated, but it can be analyzed simply. In VB, the string used is actually a BSTR type, which is a data type defined by automation (previously called Ole Automation). A BSTR consists of a head and a string, and the header contains the length information of the string, and the string can contain an embedded NULL value. Most of the BSTR is Unicode, that is, two bytes of each character. BSTR usually ends with two NULL characters of two bytes. The figure below shows a string of a BSTR type. (Prefix) ATEST / 0 head BSTR points to the first byte of the data On the other hand, most of the DLL processes (including all processes in the Windows 95 API) use the LPSTR type string, which is the end of the standard with NULL The C language string pointer, which is also known as the ASCIIZ string. LPSTR has no prefix. The figure below shows a LPSTR pointing to the ASCIIZ string. ATEST / 0 LPSTR points to a first byte of a string data ended by NULL through the simple analysis, it is not difficult to see, if you pass the parameters in the address, then the string in the VB will contain more Content, so, here, the parameters must be transmitted in a value, although it is dedicated to the parameters, but can be identified in the DLL, this is a string, and convert it into a string.

Ok, this problem we have solved, but our appetite is sure that has become a bit big, since we have implemented the string from VB to DLL, then what should I? Can you return a string to the VB program from the DLL? Through the above analysis we know, because the characters used between the two are not the same format, simple transmission is definitely not, then how should I solve it? In fact, after understanding the truth of our analysis above, then solve this problem is too easy, just make a conversion of the recovered string once, yes, this example is given, pay attention to the black body part Yes. Program in the VC:

Extern "C" _declspec (dllexport) BStr ReadString (LPCSTR LPSTRING)

{

Char str = "Hello, World!";

IF (strcmp (lpstring, str) == 0)

Return Sysallocstring ((BSTR) STR);

Else

Return Sysallocstring ((BSTR) lpstring;

}

Programs in VB:

Private Declare Function ReadString Lib "Commicle" (Byval Send As String) AS String

........................

Dim Result As String

DIM Send As String

Send = "Hello, World!"

Result = readstring (Send)

Msgbox Result

In order to continue to learn, I will continue to complete this series of articles, and I hope I can see that friends who are willing to learn this kind of knowledge will give me a comment, contact me, make progress together!

转载请注明原文地址:https://www.9cbs.com/read-24623.html

New Post(0)