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 typeDeclare asDescription
BYTE, CHARByVal variable As ByteA single byte in the memory
BOOLByVal variable As LongLong that's that should have the value 1 or 0
ATOMByVal variable As IntegerAn expression that evaluates to an Integer
SHORTByVal variable As IntegerAn 16 bit value, like the integer type used in Visual Basic
INTByVal variable As LongA 32 bits integer value
LONGByVal variable As LongSynonym for INT
WORDByVal variable As IntegerAn integer value, or two (bit wise concatenated) BYTES *
DWORDByVal variable As LongA long value, or two (bit wise concatenated) WORDS *
UINTByVal variable As LongA 32 bits integer that can't have values below 0 *
LPARAM, WPARAM, LRESULTByVal variable As LongSynonym for INT, used in some cases to describe the expected value
COLORREFByVal variable As LongSynonym for INT; A simple RGB color code; but not like OLE_COLOR does *
HWND, HDC, HMENU, etc.ByVal variable As LongSynonym for INT; used in some cases to describe the expected value (a handle).
LPDWORD, LPINT, LPUINTvariable As LongLong Pointer to the data type after LP
LPWORDvariable As IntegerLong Pointer to a WORD
LPRECTvariable As RECTLong Pointer to a Type RECT structure
LP*variable As (type)Long Pointer to a variable, structure or function *
LPSTR, LPCSTRByVal variable As StringA String variable, Visual Basic converts the values
LPVOIDvariable As AnyAny variable (use ByVal when passing a string)
NULLAs Any or

ByVal variable As Long
Only supply ByVal Nothing, ByVal 0& or vbNullString as value
VOIDSub procedureNot 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.


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值