Solved

Convert UInt32 to IntPtr

Posted on 2009-07-08
13
1,416 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
 
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
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.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

707 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now