Data type conversions for API calls from Visual Basic

转贴自 http://www.codingdomain.com/visualbasic/win32api/datatypes/

Introduction

Visual Basic and the Windows API functions use different names for certain data types. This page provides a brief overview of those differences, and how to convert these types.

Summary of types

This is a list of the most common data types found in API declarations. Most of these names are aliases for standard integer types. In Visual Basic, you need to include the the ByRef or ByVal keyword explicitly in the declaration. The aliases used in the orginal C-style declarations already have this set.

C data type Declare as Description
BYTE, CHAR ByVal variable As Byte A single byte in the memory
BOOL ByVal variable As Long Long that's that should have the value 1 or 0
ATOM ByVal variable As Integer An expression that evaluates to an Integer
SHORT ByVal variable As Integer An 16 bit value, like the integer type used in Visual Basic
INT ByVal variable As Long A 32 bits integer value
LONG ByVal variable As Long Synonym for INT
WORD ByVal variable As Integer An integer value, or two (bit wise concatenated) BYTES *
DWORD ByVal variable As Long A long value, or two (bit wise concatenated) WORDS *
UINT ByVal variable As Long A 32 bits integer that can't have values below 0 *
LPARAM, WPARAM, LRESULT ByVal variable As Long Synonym for INT, used in some cases to describe the expected value
COLORREF ByVal variable As Long Synonym for INT; A simple RGB color code; but not like OLE_COLOR does *
HWND, HDC, HMENU, etc. ByVal variable As Long Synonym for INT; used in some cases to describe the expected value (a handle).
LPDWORD, LPINT, LPUINT variable As Long Long Pointer to the data type after LP
LPWORD variable As Integer Long Pointer to a WORD
LPRECT variable As RECT Long Pointer to a Type RECT structure
LP* variable As (type) Long Pointer to a variable, structure or function *
LPSTR, LPCSTR ByVal variable As String A String variable, Visual Basic converts the values
LPVOID variable As Any Any variable (use ByVal when passing a string)
NULL As Any or

ByVal variable As Long
Only supply ByVal Nothing, ByVal 0& or vbNullString as value
VOID Sub procedure Not applicable; void means empty, nothing, nada, nope

Special notes

COLORREF

A COLORREF value is a red-green-blue combination. Visual Basic uses OLE_COLOR, which can also store system colors identifiers. The COLORREF type does not support this. To convert that type, use the oleTranslateColor(). API.

DWORD, WORD

Very often you can treat these types as Long values. A DWORD is also used to store two values; a LoWord and HiWord. The HiWord is stored in the first two bytes, and the LoWord is stored in the last two bytes of the long value.

Strings

Strings are automatically converted to their C-style equivalent. Pass them as ByVal variable As String. Functions like StrPtr() and StrConv() are very useful if you need to store the String in a structure.

Pointers

Pointers are special types which store the memory location of a variable. Visual Basic does not allow to use pointers directly, but using ByRef in the argument list has the desired effect. It will pass the memory location (pointer) of the variable to the function.

Function Pointers

A pointer to a function can be passed using the AddressOf operator. Most callback functions provide an additional parameter to include a custom long value (the lParam parameter). To pass an object-pointer to that value, use ObjPtr(). In the callback function you can use the CopyMemory() API to copy the long value to an uninitialized object.

Unsigned types

Languages like C and C++ support so called "unsigned types". These types have a different range which Visual Basic does not support. A normal Long supports both negative and positive values. In an unsigned long, the negative range is not used.

This makes it posible to store larger positive values in the data type. For example, the value &HFFFFFFFF (-1 as Long) is 4294967295, which is twice as much a Visual Basic Long type can handle.


你可能感兴趣的:(随笔手记)