Solved

Convert UInt32 to IntPtr

Posted on 2009-07-08
13
1,534 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
Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

 
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

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

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…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…

728 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