Probe into VB and VC Communication (1)

zhaozj2021-02-16  51

When using the VB to call the VC written DLL, some VBs cannot be implemented or difficult to implement, but because the data type between VB and VC does not always correspond to it, the parameter is transmitted. When you need to convert it. Below I will see how to achieve communication between VB and VC through a gradual advancement method.

First, look at a simple example.

Program in the VC:

Extern "C" _Declspec (DLLEXPORT) Void ReadString (Void)

{

// Function function

}

Compile this VC program into a DLL program for VB calls (assuming to generate commication.dll)

Programs in VB:

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

Private Declare Function ReadString LIB "CommCICITION.dll" ()

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

Call readstring

Here, we have implemented a simple communication, which writes a DLL in the VC, then call this DLL in VB, but in this communication, actually there is no data between them, only It is VB to call the DLL, so that the function readstring () function in Commicion.dll is executed. This communication implementation is very simple, so his function is very simple, but our requirements must not stop here.

Let us now implement an example of using the DLL to process data in the VB, and return the processing result to the VB program:

Program in the VC:

Extern "C" _Declspec (DLLEXPORT) BOOL ReadString (Bool Receive)

{

IF (Receive == True)

Return True;

Else

Return False;

}

Programs in VB:

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

Private Declare Function ReadString LIB "Commication.dll" (Byval Send As Boolean) AS Boolean

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

Dim Result As Boolean

Dim send as boolean

Send = false

Result = readstring (Send)

IF results

Msgbox "The return value is 'true'"

Else

Msgbox "The Return Value IS 'FALSE'"

END IF

During this communication process, we still have a very simple pass process. In the DLL, the received parameters return to the VB call program, although it is simple, but it has been implemented The data between VB and DLL is passed here. It is important to note that the data type between them, the keyword used by the Boolean data in the VC is Boolean, and the keyword used in the VC is Bool.

Since the data type between VB and VC does not have the same keyword, for convenience, the keywords used in VC and VB are listed below for convenience:

Conditions of declaration in the data type VB in C

Atom BYVAL VARIABLE AS INTEGER results for the INTEGER Type Bool Bool Bool Bool BYTE BYTE BYTE Type of Expression CHAR BYTE Type of Expression COLORREF BYVAL Variable as long result for long-type expression DWORD BYVAL VARIABLE AS long result is a long-type expression hWnd, HDC, HMENU BYVAL VARIABLE AS long result is a long-type expression, etc. The Windows handle Int, uint byval variable as long result is LONG Type Expression Long Byval Variable As Long Result For the LONG Type of Expression LPARM BYVAL VARIABLE AS Long Results For the LONG Type The LPDWORD VARIABLE As long result is the expression of the long type LPINT, LPUINT VARIABLE AS long result is a long type Expression LPRECT VARIABLE AS TYPE Customized Types Any variable LPSTR, LPCSTR BYVAL VARIABLE AS STRING Results for String Types LPVOID VARIABLE ANY Any variable (using Byval when delivering strings) LPWORD VARIABLE AS INTEGE r result is an expression of type Integer LRESULT ByVal variable As Long result is 0 or & VBNullString SHORT variable As Integer results ByVal Long NULL As Any type of expression or ByVal Nothing or ByVal variable As Long ByVal type Integer expression VOID Sub procedure Unavailable Word Byval Variable As Integer Results for Integer Types of Expression WPARAM BYVAL VARIABLE As Long Results for long-type expressions

Ok, now we have established a basic communication mode, and achieve two simple communication processes. This takes a break, then take a look at how to achieve more complex communications! (to be continued)

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

New Post(0)