Data type conversions for API calls from Visual Basic

来源:互联网 发布:计算机视觉python 编辑:程序博客网 时间:2024/05/18 01:14

转贴自 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 theByRef or ByVal keyword explicitly in the declaration. The aliases used in the orginal C-style declarations already have this set.

C data typeDeclare asDescriptionBYTE, CHARByVal variable As ByteA single byte in the memoryBOOLByVal variable As LongLong that's that should have the value 1 or 0ATOMByVal variable As IntegerAn expression that evaluates to an IntegerSHORTByVal variable As IntegerAn 16 bit value, like the integer type used in Visual BasicINTByVal variable As LongA 32 bits integer valueLONGByVal variable As LongSynonym for INTWORDByVal 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 valueCOLORREFByVal variable As LongSynonym for INT; A simple RGB color code; but not likeOLE_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 LPLPWORDvariable As IntegerLong Pointer to a WORDLPRECTvariable As RECTLong Pointer to a Type RECT structureLP*variable As (type)Long Pointer to a variable, structure or function *LPSTR, LPCSTRByVal variable As StringA String variable, Visual Basic converts the valuesLPVOIDvariable As AnyAny variable (use ByVal when passing a string)NULLAs Any or

ByVal variable As LongOnly supply ByVal Nothing, ByVal 0& or vbNullString as valueVOIDSub procedureNot applicable; void means empty, nothing, nada, nope

Special notes

COLORREF

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

DWORD, WORD

Very often you can treat these types as Long values. ADWORD 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 usingByRef 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 (thelParam parameter). To pass an object-pointer to that value, useObjPtr(). 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 normalLong 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 BasicLong type can handle.


原创粉丝点击