Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / December 2004

Tip: Looking for answers? Try searching our database.

IntPtr to a boolean in VB?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
M K - 09 Dec 2004 16:59 GMT
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.
Imran Koradia - 09 Dec 2004 18:19 GMT
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.

> Wrapping an unmanaged DLL I've got this declaration:
>  '------------------------------------------Set Attribute Value
[quoted text clipped - 11 lines]
>
> Thanks.
M K - 09 Dec 2004 19:51 GMT
That helps, only thing is, how do I set the boolean in the pointer to false
before I pass it to my function?

> take a look at the Marshal.AllocHGlobal method. Here's how you could do
> this:
[quoted text clipped - 26 lines]
> >
> > Thanks.
Imran Koradia - 09 Dec 2004 20:09 GMT
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.

> That helps, only thing is, how do I set the boolean in the pointer to false
> before I pass it to my function?
[quoted text clipped - 29 lines]
> > >
> > > Thanks.
Mattias Sj?gren - 09 Dec 2004 22:21 GMT
>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

Signature

Mattias Sjögren [MVP]  mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

M K - 10 Dec 2004 13:55 GMT
Duh, yes, that could do it. Thank you so much. Let me try...

"Mattias Sjögren" wrote:

> >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
[quoted text clipped - 21 lines]
>
> Mattias

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.