Solved

Convert UInt32 to IntPtr

Posted on 2009-07-08
13
1,450 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

831 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