PInvoke restrictions Error?

B

Bruce D

I have a VB .NET appl that I'm trying to run on another machine and I keep
getting errors. I'm sure it has to do with the GlobalLock()?? The error I
receive is: PInvoke restrictions: can not return variants

Any ideas?

Imports Scanning_App.EZTwain
Imports ClearImage
Imports System.Runtime.InteropServices

Public Declare Function GlobalSize Lib "kernel32" ()
Public Declare Function GlobalLock Lib "kernel32" ()

Dim hdib As IntPtr
hdib = EZTwain.Acquire(Me.Handle())
Dim lMem As Byte()
Dim lLen As Integer
lLen = GlobalSize(hdib)
ReDim lMem(lLen - 1)
Marshal.Copy(GlobalLock(hdib), lMem, 0, lLen)


TIA
-bruce duncan
 
T

Tom Shelton

I have a VB .NET appl that I'm trying to run on another machine and I keep
getting errors. I'm sure it has to do with the GlobalLock()?? The error I
receive is: PInvoke restrictions: can not return variants

Any ideas?

Yes... You don't have Option Strict On or you would have found the
error when you compiled :)

Option Strict On
Option Explicit On
Imports Scanning_App.EZTwain
Imports ClearImage
Imports System.Runtime.InteropServices

Public Declare Function GlobalSize Lib "kernel32" ()

Public Declare Function GlobalSize Lib "kernel32" () As Integer
Public Declare Function GlobalLock Lib "kernel32" ()

Public Declare Function GlobalLock Lib "kernel32" () As IntPtr
Dim hdib As IntPtr
hdib = EZTwain.Acquire(Me.Handle())
Dim lMem As Byte()
Dim lLen As Integer
lLen = GlobalSize(hdib)
ReDim lMem(lLen - 1)
Marshal.Copy(GlobalLock(hdib), lMem, 0, lLen)


TIA
-bruce duncan

HTH
 
H

Herfried K. Wagner [MVP]

Bruce D said:
I have a VB .NET appl that I'm trying to run on another machine and I keep
getting errors. I'm sure it has to do with the GlobalLock()?? The error
I
receive is: PInvoke restrictions: can not return variants
[...]
Public Declare Function GlobalSize Lib "kernel32" ()

'... () As IntPtr'.
 
H

Herfried K. Wagner [MVP]

Errata:
receive is: PInvoke restrictions: can not return variants
[...]
Public Declare Function GlobalSize Lib "kernel32" ()

'... () As IntPtr'.

I removed too much from text the original message: The return value of
'GlobalSize' should be an 'Int32', and the return value of 'GlobalLock' an
'IntPtr'.
 
B

Bruce D

It did help! Thank you...but now it won't compile. I get 2 different
errors at the lines marked with a "<error>"

Public Declare Function GlobalSize Lib "kernel32" () as Integer
Public Declare Function GlobalLock Lib "kernel32" () as IntPtr

Dim hdib As IntPtr
hdib = EZTwain.Acquire(Me.Handle()) 'returns a pointer to handle of a DIB
Dim lMem As Byte()
Dim lLen As Integer
<error line #1>lLen = GlobalSize(hdib)
ReDim lMem(lLen - 1)
<error line #2>Marshal.Copy(GlobalLock(hdib), lMem, 0, lLen)

error descriptions:
<#1> 'Declare Ansi Function GlobalSize Lib "kernel32"() As Integer' has no
parameters and its return type cannot be indexed.
<#2> Structure 'System.IntPtr' cannot be indexed because it has no default
property.

Any ideas?
-bruce
 
J

Jay B. Harlow [MVP - Outlook]

Bruce,
In addition to the other comments, GlobalSize & GlobalLock both expect
parameters & return values.
Public Declare Function GlobalSize Lib "kernel32" (ByVal hMem As IntPtr)
As UIntPtr
Public Declare Function GlobalLock Lib "kernel32" (ByVal hMem As IntPtr)
As IntPtr

GlobalSize should to return a UIntPtr as the size could be 64-bit under the
64-bit version of .NET 2.0, you could use Integer, however you will have
porting problems if you ever run your assembly under the 64-bit version of
..NET 2.0... You should be able to safely use IntPtr instead of UIntPtr...

I'm not seeing one right now, I would have expected the Marshal class to
have the above two methods...

Hope this helps
Jay
 

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