Error: System.ArgumentException

  • Thread starter Richard de Beer
  • Start date
R

Richard de Beer

Hi,

I've got some old code(VB6) which has been upgraded to VB.net.
In the old code, the undocumented function "VarPtr" is used. But in VB.net,
this function is no longer supported. I've searched the internet and found
some code withch should solve the problem.

This is the code:

Public Function VarPtr(ByVal o As Object) As Integer
Dim GC As System.Runtime.InteropServices.GCHandle =
System.Runtime.InteropServices.GCHandle.Alloc(o,
System.Runtime.InteropServices.GCHandleType.Pinned)
Dim ret As Integer = GC.AddrOfPinnedObject.ToInt32
GC.Free()
Return ret
End Function

But this code generates an error:

"An unhandled exception of type 'System.ArgumentException' occurred in
mscorlib.dll Additional information: Object contains non-primitive or
non-blittable data."

Since I'm a beginner, I was hoping that you could help me with this problem.

Thanks a lot!
 
R

Rob Richardson

Richard,

VarPtr() is a very advanced piece of Visual Basic. It's not documented for
a reason (although I can't tell you what it is). The best I can think of
for you to do is to try to figure out what it's used for in the original
code and find some way to write new code that does the same thing.

Good luck!

Rob
 
A

Armin Zingler

Richard de Beer said:
Public Function VarPtr(ByVal o As Object) As Integer
Dim GC As System.Runtime.InteropServices.GCHandle =
System.Runtime.InteropServices.GCHandle.Alloc(o,
System.Runtime.InteropServices.GCHandleType.Pinned)
Dim ret As Integer = GC.AddrOfPinnedObject.ToInt32
GC.Free()
Return ret
End Function

But this code generates an error:

"An unhandled exception of type 'System.ArgumentException' occurred
in mscorlib.dll Additional information: Object contains non-primitive
or non-blittable data."

Since I'm a beginner, I was hoping that you could help me with this
problem.

I think after calling GC.Free, the ret value gets invalid because the object
isn't pinned anymore. In addition, if the object is a value type, it
is destroyed when the functions returns


But, what are you trying to achieve?
 
H

Herfried K. Wagner [MVP]

* "Richard de Beer said:
I've got some old code(VB6) which has been upgraded to VB.net.
In the old code, the undocumented function "VarPtr" is used. But in VB.net,
this function is no longer supported. I've searched the internet and found
some code withch should solve the problem.

Try to find a way around using this function. Why do you need it? What
exactly do you want to do? Maybe there is a much cleaner and better
solution.
This is the code:

Public Function VarPtr(ByVal o As Object) As Integer
Dim GC As System.Runtime.InteropServices.GCHandle =
System.Runtime.InteropServices.GCHandle.Alloc(o,
System.Runtime.InteropServices.GCHandleType.Pinned)
Dim ret As Integer = GC.AddrOfPinnedObject.ToInt32
GC.Free()
Return ret
End Function

You must not free the 'GC' here, but that's not the main problem.
But this code generates an error:

"An unhandled exception of type 'System.ArgumentException' occurred in
mscorlib.dll Additional information: Object contains non-primitive or
non-blittable data."

Blittable and Non-Blittable Types
<http://msdn.microsoft.com/library/d...ide/html/cpconblittablenon-blittabletypes.asp>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top