How to Emulate MKI$ and CVI in VB Using Windows HMemCpyLast reviewed: June 21, 1995Article ID: Q87970 |
The information in this article applies to:
SUMMARYVisual Basic for Windows does not support the MKx$ and CVx family of conversion functions found in earlier versions of Microsoft QuickBasic and Basic Professional Development System (PDS) for MS-DOS. However, you can write functions that provide this support using the hmemcpy API routine provided by Windows version 3.1. This article provides example routines that simulate the MKI$, MKL$, MKS$, MKD$, CVI, CVL, CVS, and CVD functions.
MORE INFORMATIONThe MKx$ functions convert numeric values to strings by placing the ASCII value of each byte that represents the numeric value into a string.
Function Description MKI$ Converts an integer to a 2-byte string MKL$ Converts a long-integer to a 4-byte string MKS$ Converts a single precision variable to a 4-byte string MKD$ Converts a double-precision variable to an 8-byte stringThe CVx functions convert strings created with the MKx$ functions back into numeric values.
Function Description CVI Converts a 2-byte string created with MKI$ to an integer CVL Converts a 4-byte string created with MKL$ to a long integer CVS Converts a 4-byte string created with MKS$ to a single- precision number CVD Converts an 8-byte string created with MKD$ to a double- precision numberThe hmemcpy API function can be used to emulate these functions as demonstrated in the example below. Note that the hmemcpy API function is not provided with Windows version 3.0, so the example below requires Windows version 3.1. The hmemcpy routine copies bytes from a source buffer to a destination buffer. You can use this routine to copy the value of each byte in a numeric value to a corresponding byte in a string to emulate the MKx$ functions. Similarly, you can use the same technique to copy the bytes from a string to a numeric value, to emulate the CVx functions. NOTE: The hmemcpy routine requires the addresses pointing to the actual location of the data to be copied from and written to. Therefore, it is necessary to pass strings by value (ByVal) in order to pass the location of the string data, as opposed to passing the location of the string descriptor. Similarly, it is necessary to initialize the string size by assigning the string to an appropriate number of characters. To use the following routines in your Visual Basic for Windows application, you must Declare the hmemcpy routine. Add the following code to the general declarations section of the form:
' Enter the following Declare statement on one, single line. Declare Sub hmemcpy Lib "kernel" (hpvDest As Any, hpvSource As Any, ByVal cbCopy As Long) Function MKI$ (x As Integer) temp$ = Space$(2) hmemcpy ByVal temp$, x%, 2 MKI$ = temp$ End Function Function CVI (x As String) As Integer If Len(x) <> 2 Then MsgBox "Illegal Function Call" Stop End If hmemcpy temp%, ByVal x, 2 CVI = temp% End Function Function MKL$ (x As Long) temp$ = Space$(4) hmemcpy ByVal temp$, x&, 4 MKL$ = temp$ End Function Function CVL (x As String) As Long If Len(x) <> 4 Then MsgBox "Illegal Function Call" Stop End If hmemcpy temp&, ByVal x, 4 CVL = temp& End Function Function MKS$ (x As Single) temp$ = Space$(4) hmemcpy ByVal temp$, x!, 4 MKS$ = temp$ End Function Function CVS (x As String) As Single If Len(x) <> 4 Then MsgBox "Illegal Function Call" Stop End If hmemcpy temp!, ByVal x, 4 CVS = temp! End Function Function MKD$ (x As Double) temp$ = Space$(8) hmemcpy ByVal temp$, x, 8 MKD$ = temp$ End Function Function CVD (x As String) As Double If Len(x) <> 8 Then MsgBox "Illegal Function Call" Stop End If hmemcpy temp#, ByVal x, 8 CVD = temp# End Function REFERENCES"Microsoft Windows SDK: Programmer's Reference," Volume 2: Functions," version 3.1
|
Additional reference words: 1.00 2.00 3.00
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |