Solved

Convert UInt32 to IntPtr

Posted on 2009-07-08
13
1,493 Views
Last Modified: 2013-11-07
Hi experts,

I use vb.net 1.1 and am trying to convert a uint32 to intptr.
The following function does not work:
ctype(myUint32,IntPtr)
the error I get is: Value of type system.uintptr cannot be converted to a system.intptr

Any Idea how I can achied this?

Thanks
0
Comment
Question by:etiennedemers
  • 7
  • 3
13 Comments
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 24805127
Think you have to create a new instance of IntPtr with the unsigned integer as the parameter.
Dim uint As UInt32 = 25
Dim iptr As New IntPtr(uint)

Open in new window

0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 24805147
You may want to create as UIntPtr (unsigned integer pointer):
Dim uint As UInt32 = 25
Dim iptr As New UIntPtr(uint)

Open in new window

0
 

Author Comment

by:etiennedemers
ID: 24805229
new intptr does not support uint

I even tried

new intptr(convert.toint32(myuint))

no errors here but the value = nothing

i use the intptr in the marshal function
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 59

Expert Comment

by:Kevin Cross
ID: 24805293

'Try like this
Dim iptr As New UIntPtr(UInteger.Parse(myuint))
'Can try with Integer and IntPtr also

Open in new window

0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 24805315

'Try with op_Explicit
Dim iptr As IntPtr = IntPtr.op_Explicit(myuint)

Open in new window

0
 
LVL 15

Expert Comment

by:jinal
ID: 24805320
Dim sint As Int32 = your int value
 Dim iptr As New IntPtr(sint)

 Dim uint As Int32 = your int value.
 Dim i1ptr As New IntPtr(sint)

Can you please give me more detail ?

1. Which value you are passing ?
2. Is your system 32 bit or 64 bit ?
0
 

Author Comment

by:etiennedemers
ID: 24805370
it doesn't like the parse

value uint cannot be converted to string.
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 24805429
Sorry, forgot the ToString and it worked on my system, but I see now you are using .NET 1.x and I have .NET 2.x and 3.x installed.

See my post here -- http:#24805315
Although, I can't remember when the op_Explicit was introduced, but think it was .NET 2.0.
0
 

Author Comment

by:etiennedemers
ID: 24805449
Yes it works fine on dotnet 2 and higher...
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 24805538
The documentation states it is supported in .NET 1.1, but you will have to test for sure.  Also, you might be having the issue since going from UInt32 to IntPtr as it is expecting Int32.  Should do the cast automatically, but you may have to explicitly convert your value to Int32 then use as value in op_Explicit.
0
 
LVL 59

Accepted Solution

by:
Kevin Cross earned 500 total points
ID: 24982323
http:#24805315 -- op_Explicit is the solution.

Both IntPtr.op_Explicit and UIntPtr.op_Explicit are supported in .NET 1.1:
http://msdn.microsoft.com/en-us/library/a0f1byxz(VS.80).aspx
http://msdn.microsoft.com/en-us/library/4hwc6zkt(VS.80).aspx
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
vb.net convert long time to mm:ss 23 20
Problem to Office 1 39
visual studio vb.net windows onkeyup 2 25
ASP.NET Built-In Report Creator / Viewer 5 17
In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

713 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question