Solved

Convert UInt32 to IntPtr

Posted on 2009-07-08
13
1,510 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 3
13 Comments
 
LVL 60

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 60

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
Why You Need a DevOps Toolchain

IT needs to deliver services with more agility and velocity. IT must roll out application features and innovations faster to keep up with customer demands, which is where a DevOps toolchain steps in. View the infographic to see why you need a DevOps toolchain.

 
LVL 60

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 60

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 60

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 60

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 60

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

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

732 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