473,386 Members | 1,798 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

PInvoke restrictions Error?

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
Nov 21 '05 #1
5 3367
In article <11*************@corp.supernews.com>, Bruce D wrote:
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
--
Tom Shelton [MVP]
Nov 21 '05 #2
"Bruce D" <br*************@hotmail.com> schrieb:
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'.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #3
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'.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #4
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
Nov 21 '05 #5
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

"Bruce D" <br*************@hotmail.com> wrote in message
news:11*************@corp.supernews.com...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

Nov 21 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Ted | last post by:
Is it possible to use mailslots in .NET using PInvoke? I have a VC++ 6.0 based app that creates and listens to a mailslot. I have a second VC++ 6.0 based app that opens the mailslot and writes...
8
by: promko | last post by:
Hi! I need to call the following unmanged method: HFCI __cdecl FCICreate( void FAR* pv) I have written the next managed declaration: public static extern IntPtr FCICreate( IntPtr pv );
3
by: msnews.microsoft.com | last post by:
Hi i am using User32.dll in Visual stdio 2005. public static extern long SetActiveWindow(long hwnd); public static extern long keybd_event(byte bVk, byte bScan, long dwFlags,
4
by: Peter | last post by:
Are there any restrictions in using MarshalAs when p/invoke-ing from a class library as opposed to a windows application in .NET CF 2.0? The following code will not compile in a CF class library...
1
by: Roy | last post by:
Have a dll that calls a com object. Can call dll from C program fine, can call dll from C# program in compact framework (i.e. CE/PPC devices), no problem. On XP devices when I pinvoke it gets...
8
by: Rajesh Soni | last post by:
Hi! I'm getting a PInvoke error while trying to execute the following code... declaration: Structure POINTAPI Dim x As IntPtr
9
by: Ringo | last post by:
the LeafProject http://www.leafproject.org has a DLL for Face recognition. it is written in C++ but they interface to it from Lisp. I want to interface to it from C#. Their Lisp definitions looks...
14
by: Mohamed Mansour | last post by:
Hey there, this will be somewhat a long post, but any response is appreciated! I have done many PInvoke in the past from C++ to C#, but I did PInvoke within C# not C++/CLI. Can someone explain...
1
by: Paul van Brouwershaven | last post by:
Hi All, I'm struggling with the WDSL restrictions in PHP/SOAP for a while know. I would like to create some simple restrictions in my WDSL file. The script are running both on the same server...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.