Você está na página 1de 3

MC Press Online

Visual Basic APIs to Work with Pointers


Contributed by Craig Pelkie Sunday, 30 April 1995 Last Updated Sunday, 30 April 1995

If youre working with the PC Support or Client Access/400 APIs to develop Visual Basic programs, you soon discover that you need to be able to pass and dereference pointers. For example, the data transform APIs, used to convert numeric values from ASCII to AS/400 packed and binary format, require that you supply a pointer as a function argument. Other APIs return a pointer to a value, which you must then dereference into a field that Visual Basic can work with.

Two relatively obscure API functions are provided with PC Support and Client Access. The APIs are EHNDT_GetPtr, to get a pointer to a variable, and EHNDT_MemCopy, to copy a value referenced by a pointer to a memory buffer that Visual Basic can work with. The APIs are not documented in the CA/400 for Windows 3.1 API and Technical Reference (SC41-3531), nor in any of the previous versions of that manual.

However, there are sample Visual Basic function declares and examples of the APIs in the Visual Basic sample programs provided with PCS or CA/400. In the QIWSTOOL folder, package WVBSAMP includes three sample projects. Project RQSAMP.MAK (Remote SQL sample) uses the EHNDT_GetPtr API. Project TFSAMP.MAK (file transfer sample) uses the EHNDT_MemCopy API.

The sample code shown here demonstrates how to use both APIs with the EHNDT_ASCIIToPacked API. The form shown in Figure 1 is used to get an input value that you enter. When you press the Calculate command button, routine cmdCalculate_Click runs. The routine prepares the input value for the API, calls the ASCII to packed conversion API, then displays the number, converted to packed format, in the Output value text box.

The EHNDT_ASCIIToPacked API requires pointers to the source and target strings. Those strings are the second and third parameters of the API. To get the

pointers, use the EHNDT_GetPtr API for both of those parameters. You can see this in the sample code shown in Figure 2, where EHNDT_GetPtr is called for the source and target strings before EHNDT_ASCIIToPacked.

The packed value produced by EHNDT_ASCIIToPacked is available to your program as a pointer. To get the value referenced by the pointer, use the EHNDT_MemCopy API. This is shown immediately after the EHNDT_ASCIIToPacked call. EHNDT_MemCopy copies the specified number of bytes from the memory location pointed to by the pointer, to the buffer specified as the target string.

To run this sample program, first check for the PCSMCPY.DLL in your PCS, CAWIN, or WINDOWSSYSTEM paths. If
http://www.mcpressonline.com Powered by Joomla! Generated: 28 August, 2008, 23:32

MC Press Online

the DLL is not on your disk, youll have to extract it from the WVBSAMP program package. The file is also available on the CA/400 Toolkit CD-ROM (SK2T-2829). Youll also need the EHNDTW.DLL, which is loaded onto your PC when you load the code for the Windows APIs.

You can either type this code directly into Visual Basic and create a form like this, or download the code from the AS/400 file area on the Midrange Computing BBS. (The number for the BBS is on page 2 of this issue.) You do not need an active router connection to run this program.

Figure 1: Test Form for Pointer APIs

(Note: long lines are indented on the following line, but are entered as one line in the VB editor) Option Explicit Declare Function EHNDT_ASCIIToPacked Lib "EHNDTW.DLL" (ByVal hWnd As

Integer, ByVal lpTarget As Long, ByVal lpSource As Long, ByVal length As Integer, ByVal decimal_position As Integer) As Integer Declare Sub EHNDT_GetPtr Lib "PCSMCPY.DLL" (lpVariable As Any, lpPtr As Long) Declare Sub EHNDT_MemCopy Lib "PCSMCPY.DLL" (ByVal lpbSource As Long, ByVal lpbTarget As String, ByVal uiLength As Integer) Sub cmdCalculate_Click () 'Demonstrate call to EHNDT_ASCIIToPacked API. 'Get numeric value as input from from. Use EHNDT_GetPtr 'APIs to prepare for Visual Basic call to EHNDT_ASCIIToPacked. 'Upon return from EHNDT_ASCIIToPacked, show hex value 'of packed number.

Dim APIReturnCode As Integer 'return code Dim DecPos As Integer 'decimal positions Dim I As Integer 'loop index Dim lpSourcePtr As Long 'long pointer to source Dim lpTargetPtr As Long 'long pointer to target Dim ParmLength As Long 'parameter length Dim SourceString As String 'source string (input)

Dim TargetString As String 'target string (output)

'initialize values for EHNDT_ASCIIToPacked API ParmLength = 5 DecPos = 0 SourceString = Format$(Val(txtInputValue), "0000000000") TargetString = Space$(ParmLength) 'Get pointers to source and target strings

http://www.mcpressonline.com

Powered by Joomla!

Generated: 28 August, 2008, 23:32

MC Press Online

Call EHNDT_GetPtr(ByVal SourceString, lpSourcePtr) Call EHNDT_GetPtr(ByVal TargetString, lpTargetPtr) 'convert ASCII to packed APIReturnCode = EHNDT_ASCIIToPacked(hWnd, lpTargetPtr, lpSourcePtr, ParmLength, DecPos) 'copy packed variable, pointed to by pointer returned from 'EHNDT_ASCIIToPacked, to Visual Basic string variable Call EHNDT_MemCopy(lpTargetPtr, TargetString, ParmLength) 'convert packed return variable to displayable output field txtOutputValue = ""

For I = 1 To ParmLength txtOutputValue = txtOutputValue + Format$(Hex$(Asc(Mid$(TargetString, I, 1))), "00") Next I End Sub Sub cmdExit_Click () End End Sub

Figure 2: Sample Visual Basic Code

http://www.mcpressonline.com

Powered by Joomla!

Generated: 28 August, 2008, 23:32

Você também pode gostar