Questions about Declare with Windows API functions

D

**Developer**

I saw the following in an example

Public Overloads Declare Auto Function CreateDC Lib "gdi32" (ByV...As
Integer...

Public Overloads Declare Auto Function CreateDC Lib "gdi32" (ByV...As
String...

I know what Overloads means in relation to a function in the base class.

But does it make sense to use Overloads with a Windows API function?

=======================================

Second question.

If a Windows API function returns a BOOL I've Declared it as returning an
Integer and check for zero (as a FALSE return).

If I Declare the return a Boolean will FALSE be marshaled to False and TRUE
to True?

Will TRUE be marshaled to -1?









Thanks
 
M

Mattias Sjögren

But does it make sense to use Overloads with a Windows API function?

Sometimes it does. There are plenty of functions where parameter types
can vary. But In the case of CreateDC, I don't see why any of the
parameters ever would be declared as Integer.

If a Windows API function returns a BOOL I've Declared it as returning an
Integer and check for zero (as a FALSE return).

If I Declare the return a Boolean will FALSE be marshaled to False and TRUE
to True?

Yes, so I'd recommend changing the return type to Boolean.

Will TRUE be marshaled to -1?

Not sure what you mean by that. The value of TRUE is 1 on the native
side.


Mattias
 
H

Herfried K. Wagner [MVP]

**Developer** said:
Public Overloads Declare Auto Function CreateDC Lib "gdi32" (ByV...As
Integer...

Public Overloads Declare Auto Function CreateDC Lib "gdi32" (ByV...As
String...

I know what Overloads means in relation to a function in the base class.

Overloads simply indicates that there is more than one "version" of a
function. In some cases it is mandatory, in some not (as in the sample
shown above).

BTW: I believe the sample above is nonsense because 'Auto' and the return
type 'String' are not valid for this function.
If a Windows API function returns a BOOL I've Declared it as returning an
Integer and check for zero (as a FALSE return).

If I Declare the return a Boolean will FALSE be marshaled to False and
TRUE to True?

Yes, simply use 'Boolean' as return type. The reason why people used 'Long'
as return type for these functions in VB6 was that 'Integer' in VB6 was a
16-bit data type.
 
H

Herfried K. Wagner [MVP]

Errata + addendum:

'Auto' makes sense for the function. Sample function declaration:

\\\
Private Delcare Auto Function CreateDC Lib "user32.dll" ( _
ByVal lpszDriver As String, _
ByVal lpszDevice As String, _
ByVal lpszOutput As String, _
ByRef lpInitData As DEVMODE _
) As IntPtr
///

Sample Call:

\\\
Dim hDC As IntPtr = CreateDC(..., ..., vbNullString, ...)
///
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top