IntPtr to a boolean in VB?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Wrapping an unmanaged DLL I've got this declaration:
'------------------------------------------Set Attribute Value
Declare Auto Function KSetAttributeValue Lib "DCSPro4SLR.dll"_
Alias "KPDCSetAttributeValue" (ByVal inRef As IntPtr, ByVal inAttrID As
Integer,_
ByVal inType As KPDCDataTypes, ByVal inAttrSize As Integer, ByVal
inAttrValue As IntPtr) As Integer

So, inType is an enumerator that tells the function what type (Integer,
String, Boolean) inAttrValue is, or actually is pointing to. So how to I
create a pointer to a boolean? I've created pointers that get passed by
reference and filled by the unmanaged library, but I haven't filled a pointer
myself.

Thanks.
 
take a look at the Marshal.AllocHGlobal method. Here's how you could do
this:
Dim ptr As IntPtr = Marshal.AllocHGlobal(Marshal.SizeOf(GetType(Boolean)))

Then you can retrieve the boolean pointed to by ptr (after your function has
executed and populated the values) as:
Dim b As Byte = Marshal.ReadByte(ptr)

And ofcourse dispose off the allocated memory:
Marshal.FreeHGlobal(ptr)


hope that helps..
Imran.
 
That helps, only thing is, how do I set the boolean in the pointer to false
before I pass it to my function?
 
My apologies. To read the boolean value, you'll have to use ReadInt16
instead of ReadByte (what was I thinking ?!?) since booleans are stored as
16-bit numbers. So, to write a boolean, you can use the counter part of the
Read method - the WriteInt16 method off the Marshal class.

Marshal.WriteInt16(ptr, CShort(b))

and then when reading:
Dim bResult As Boolean = CBool(Marshal.ReadInt16(ptr))

hope that helps..
Imran.
 
So, inType is an enumerator that tells the function what type (Integer,
String, Boolean) inAttrValue is, or actually is pointing to. So how to I
create a pointer to a boolean? I've created pointers that get passed by
reference and filled by the unmanaged library, but I haven't filled a pointer
myself.

The easiest way is to write strongly typed overloads for each possible
parameter type:

Declare Auto Function KSetAttributeValue Lib "DCSPro4SLR.dll" Alias
"KPDCSetAttributeValue" (ByVal inRef As IntPtr, ByVal inAttrID As
Integer, ByVal inType As KPDCDataTypes, ByVal inAttrSize As Integer,
ByVal inAttrValue As String) As Integer

Declare Auto Function KSetAttributeValue Lib "DCSPro4SLR.dll" Alias
"KPDCSetAttributeValue" (ByVal inRef As IntPtr, ByVal inAttrID As
Integer, ByVal inType As KPDCDataTypes, ByVal inAttrSize As Integer,
ByRef inAttrValue As Boolean) As Integer

Declare Auto Function KSetAttributeValue Lib "DCSPro4SLR.dll" Alias
"KPDCSetAttributeValue" (ByVal inRef As IntPtr, ByVal inAttrID As
Integer, ByVal inType As KPDCDataTypes, ByVal inAttrSize As Integer,
ByRef inAttrValue As Integer) As Integer



Mattias
 
Back
Top