473,396 Members | 1,766 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,396 software developers and data experts.

SDK's Unmanaged DLL in VB.NET (Pointer issues)

Hello All,

I'm working with a SDK for a piece of hardware attached to a Windows
CE device. The sample code provided is all written in eVC++ and I'm
trying to port it over to a VB.NET application we're writing. Up to
this point I've been able to translate the code into VB.NET, but one
function (BK_EXTRACT) from the SDK has me perplexed.

In the interest of disclosure, I've barely got my head wrapped around
pointers. I'm sure what I'm doing isn't rocket science, but I could
still use a hand.

Here is the function's description in the developer's manual:
---
BK_API char *CaptureSensor(void)
Abstract: capture fingerprint image
Parameter: none
Remarks: this function is included in .dll
Return: return pointer to internal image buffer
BK_API int BK_EXTRACT(HANDLE Handle, BYTE* PixelsBuffer, BYTE
*Template, int PurposeMode)
Abstract: EXTRACT the characteristic template of fingerprint image in
current buffer, whose result put into the buffer pointed by Template.
Parameter:
Handle$B!'(B the algorithms handle returned by calling BK_API HANDLE
FPInit to initialize the fingerprint algorithms library
BYTE* PixelsBuffer$B!'(BA pointer to fingerprint image which will be
extracted later
BYTE *Template is used for buffering the extracted characteristic
template, the size should be configured at 2K in advance.
int PurposeMode$B!'(Bthe following parameters should be set up as below:
EXTRACT_FOR_ENROLLMENT (= 0)
EXTRACT_FOR_IDENTIFICATION (= 1)
EXTRACT_FOR_VERIFICATION (= 2)
Remarks: this function is included in the .dll
Return: return the real size of the fingerprint template if extracted
successfully, otherwise return <=0
---

Here's how it is used in eVC++:
---
int tmpLen=0;
BYTE tmp[2048];
int score=0,tid=0;
int tmpCapture=IsCapturing;
int ret;
if(tmpCapture)
{
pchImgBuf = CaptureSensor ();
if(pchImgBuf)
else
{
printf("Capture failed\n");
return 0;
}
IsCapturing=0;
}
else
return 0;
ret=FPTest(fhdl);
if(ret)
{
tmpLen=BK_EXTRACT(fhdl, (BYTE*)pchImgBuf,
tmp,EXTRACT_FOR_IDENTIFICATION);
}
---

Here is how I'm declaring those functions in VB.NET:
---
Declare Function CaptureSensor Lib "\Storage Card\Finger.DLL" ()
As IntPtr

Declare Function BK_EXTRACT Lib "\Storage Card\Finger.DLL" _
(ByVal FPInitHandle As IntPtr, _
ByRef PixelsBuffer As IntPtr, _
ByRef Template As Byte, _
ByVal PurposeMode As Integer) As Integer
---

And here's the code I have written thus far:
---
Dim ptrImageBuffer As IntPtr
Dim tmpLen As Integer
Dim bytPixelsBuffer(2048) As Byte
Dim bytTemplate(2048) As Byte

'On the interval, capture what the reader sees
Try
ptrImageBuffer = CaptureSensor()
Catch ex As Exception
End Try

'If we caught something, check it out
If (ptrImageBuffer) Then
'If it's an actual print on the reader, continue
If FPTest(fpcHandle) Then
'Extract the captured image as a template and an image
Try
tmpLen = BK_EXTRACT(fpcHandle, ptrImageBuffer,
bytTemplate(0), EXTRACT_FOR_IDENTIFICATION)
Catch ex As Exception
lblUSBStatus.Text = "Bioprint extraction failed: "
& ex.Message
End Try
End If
End If
End If
---

I've tried several things to get this to work, but I think I'm just
not sure how to properly use them. It would seem that I need to use
Marshall.Copy somewhere, but again, I'm just not getting my head
wrapped around it. I either throw CE native access violations or
stack overflows depending on what I try.

If anyone has any ideas or could lead me in the right direction I
would greatly appreciate it.

Thank you all in advance,
Jun 27 '08 #1
7 2737

<ag*****@cox.netwrote in message
news:5f**********************************@d45g2000 hsc.googlegroups.com...
Hello All,

I'm working with a SDK for a piece of hardware attached to a Windows
CE device. The sample code provided is all written in eVC++ and I'm
trying to port it over to a VB.NET application we're writing. Up to
this point I've been able to translate the code into VB.NET, but one
function (BK_EXTRACT) from the SDK has me perplexed.

In the interest of disclosure, I've barely got my head wrapped around
pointers. I'm sure what I'm doing isn't rocket science, but I could
still use a hand.

Here is the function's description in the developer's manual:
---
BK_API char *CaptureSensor(void)
Abstract: capture fingerprint image
Parameter: none
Remarks: this function is included in .dll
Return: return pointer to internal image buffer
BK_API int BK_EXTRACT(HANDLE Handle, BYTE* PixelsBuffer, BYTE
*Template, int PurposeMode)
Abstract: EXTRACT the characteristic template of fingerprint image in
current buffer, whose result put into the buffer pointed by Template.
Parameter:
Handle$B!'(B the algorithms handle returned by calling BK_API HANDLE
FPInit to initialize the fingerprint algorithms library
BYTE* PixelsBuffer$B!'(BA pointer to fingerprint image which will be
extracted later
BYTE *Template is used for buffering the extracted characteristic
template, the size should be configured at 2K in advance.
int PurposeMode$B!'(Bthe following parameters should be set up as below:
EXTRACT_FOR_ENROLLMENT (= 0)
EXTRACT_FOR_IDENTIFICATION (= 1)
EXTRACT_FOR_VERIFICATION (= 2)
Remarks: this function is included in the .dll
Return: return the real size of the fingerprint template if extracted
successfully, otherwise return <=0
---

Here's how it is used in eVC++:
---
int tmpLen=0;
BYTE tmp[2048];
int score=0,tid=0;
int tmpCapture=IsCapturing;
int ret;
if(tmpCapture)
{
pchImgBuf = CaptureSensor ();
if(pchImgBuf)
else
{
printf("Capture failed\n");
return 0;
}
IsCapturing=0;
}
else
return 0;
ret=FPTest(fhdl);
if(ret)
{
tmpLen=BK_EXTRACT(fhdl, (BYTE*)pchImgBuf,
tmp,EXTRACT_FOR_IDENTIFICATION);
}
---

Here is how I'm declaring those functions in VB.NET:
---
Declare Function CaptureSensor Lib "\Storage Card\Finger.DLL" ()
As IntPtr

Declare Function BK_EXTRACT Lib "\Storage Card\Finger.DLL" _
(ByVal FPInitHandle As IntPtr, _
ByRef PixelsBuffer As IntPtr, _
ByRef Template As Byte, _
ByVal PurposeMode As Integer) As Integer
---

And here's the code I have written thus far:
---
Dim ptrImageBuffer As IntPtr
Dim tmpLen As Integer
Dim bytPixelsBuffer(2048) As Byte
Dim bytTemplate(2048) As Byte

'On the interval, capture what the reader sees
Try
ptrImageBuffer = CaptureSensor()
Catch ex As Exception
End Try

'If we caught something, check it out
If (ptrImageBuffer) Then
'If it's an actual print on the reader, continue
If FPTest(fpcHandle) Then
'Extract the captured image as a template and an image
Try
tmpLen = BK_EXTRACT(fpcHandle, ptrImageBuffer,
bytTemplate(0), EXTRACT_FOR_IDENTIFICATION)
Catch ex As Exception
lblUSBStatus.Text = "Bioprint extraction failed: "
& ex.Message
End Try
End If
End If
End If
---

I've tried several things to get this to work, but I think I'm just
not sure how to properly use them. It would seem that I need to use
Marshall.Copy somewhere, but again, I'm just not getting my head
wrapped around it. I either throw CE native access violations or
stack overflows depending on what I try.

If anyone has any ideas or could lead me in the right direction I
would greatly appreciate it.

Thank you all in advance,
For translating some of the API calls use the following tool (free from MS).

P/Invoke Interop Assistant

Search in MS and you can paste declaration code in and have it translated
correctly to VB/C#

LS

Jun 27 '08 #2
Hi LS,

That was incredibly helpful! I wish I had that last week when I
started this mess.

I'm running into a new issue now: I'm throwing an OverflowException
now. Here's my declaration:
<System.Runtime.InteropServices.DllImportAttribute ("\Storage Card
\Finger.DLL", EntryPoint:="BK_EXTRACT")_
Public Shared Function BK_EXTRACT(ByVal Handle As System.IntPtr,
ByRef PixelsBuffer As Byte, ByRef Template As Byte(), ByVal
PurposeMode As Integer) As Integer
End Function

And here's how I'm calling it:
---
Dim ptrImageBuffer As IntPtr
Dim tmpLen As Integer
Dim bytTemplate(2047) As Byte
Dim ptrTemplate

'On the interval, capture what the reader sees
Try
ptrImageBuffer = CaptureSensor
Catch ex As Exception
End Try

'If we caught something, check it out
If (ptrImageBuffer) Then
'If it's an actual print on the reader, continue
If FPTest(fpcHandle) Then
'Extract the captured image as a template and an image
Try
ptrTemplate = Marshal.AllocHGlobal(2048)
Marshal.Copy(ptrTemplate, bytTemplate, 0, 2047)

tmpLen = BK_EXTRACT(fpcHandle, ptrImageBuffer,
bytTemplate, EXTRACT_FOR_IDENTIFICATION)
Catch ex As Exception
lblUSBStatus.Text = "Bioprint extraction failed: "
& ex.Message
End Try

End If
End If
---

I feel like it has something to do with how I'm handling that byte
array... I just haven't been able to get it working.

Anyone with an idea?

Thanks again!
Jun 27 '08 #3
I think PixelsBuffer and Template should be ByVal, not ByRef.

<ag*****@cox.netwrote in message
news:f6**********************************@k13g2000 hse.googlegroups.com...
Hi LS,

That was incredibly helpful! I wish I had that last week when I
started this mess.

I'm running into a new issue now: I'm throwing an OverflowException
now. Here's my declaration:
<System.Runtime.InteropServices.DllImportAttribute ("\Storage Card
\Finger.DLL", EntryPoint:="BK_EXTRACT")_
Public Shared Function BK_EXTRACT(ByVal Handle As System.IntPtr,
ByRef PixelsBuffer As Byte, ByRef Template As Byte(), ByVal
PurposeMode As Integer) As Integer
End Function

And here's how I'm calling it:
---
Dim ptrImageBuffer As IntPtr
Dim tmpLen As Integer
Dim bytTemplate(2047) As Byte
Dim ptrTemplate

'On the interval, capture what the reader sees
Try
ptrImageBuffer = CaptureSensor
Catch ex As Exception
End Try

'If we caught something, check it out
If (ptrImageBuffer) Then
'If it's an actual print on the reader, continue
If FPTest(fpcHandle) Then
'Extract the captured image as a template and an image
Try
ptrTemplate = Marshal.AllocHGlobal(2048)
Marshal.Copy(ptrTemplate, bytTemplate, 0, 2047)

tmpLen = BK_EXTRACT(fpcHandle, ptrImageBuffer,
bytTemplate, EXTRACT_FOR_IDENTIFICATION)
Catch ex As Exception
lblUSBStatus.Text = "Bioprint extraction failed: "
& ex.Message
End Try

End If
End If
---

I feel like it has something to do with how I'm handling that byte
array... I just haven't been able to get it working.

Anyone with an idea?

Thanks again!
Jun 27 '08 #4
Thanks for the input Bill! Unfortunately that yielded the same result
- overflowexception.

The P/Invoke Interop Assistant named those as ByRef and in previous
research I found that should be the way to go. If I'm understanding
the scenario correctly, since we're making reference to the byte via a
pointer and not actually using the byte itself we need to set those
values as ByRef.

Regardless, thanks for chiming in!
Jun 27 '08 #5

<ag*****@cox.netwrote in message
news:91**********************************@d1g2000h sg.googlegroups.com...
Thanks for the input Bill! Unfortunately that yielded the same result
- overflowexception.

The P/Invoke Interop Assistant named those as ByRef and in previous
research I found that should be the way to go. If I'm understanding
the scenario correctly, since we're making reference to the byte via a
pointer and not actually using the byte itself we need to set those
values as ByRef.

Regardless, thanks for chiming in!
It's very very rare for arrays to be ByRef. Usually you want them ByVal and
apply the appropaite marshalling attributes including In, Out and either
LPArray or SafeArray. In this case LPArray. ByRef for an array variable is a
pointer to the array, not the first element.. a Pointer to the first
element would be ByRef element(0).

If I go back to your original code, I would try it as :

Declare Function BK_EXTRACT Lib "\Storage Card\Finger.DLL" _
(ByVal FPInitHandle As IntPtr, _
ByVal PixelsBuffer As IntPtr, _
<[In](), Out()ByVal Template() As Byte, _
ByVal PurposeMode As Integer) As Integer
---

---
Dim ptrImageBuffer As IntPtr
Dim tmpLen As Integer
Dim bytPixelsBuffer(0 to 2047) As Byte
Dim bytTemplate(0 to 2047) As Byte

'On the interval, capture what the reader sees
Try
ptrImageBuffer = CaptureSensor()
Catch ex As Exception
' don't swallow exceptions
Throw
End Try

'If we caught something, check it out
If ptrImageBuffer <IntPtr.Zero Then
'If it's an actual print on the reader, continue
If FPTest(fpcHandle) Then
'Extract the captured image as a template and an image
Try
tmpLen = BK_EXTRACT(fpcHandle, ptrImageBuffer,
bytTemplate, EXTRACT_FOR_IDENTIFICATION)
' need to deal with the need to make the array larger
here, and also if tmpLen is < 0

Catch ex As Exception
lblUSBStatus.Text = "Bioprint extraction failed: "
& ex.Message
End Try
End If
End If
End If
Jun 27 '08 #6
Thanks again Bill!

Unfortunately I'm still running into issues.

As soon as I declare PixelsBuffer as an IntPtr in the function
declaration, I get a native error from the device 0xC00000FD. That is
a stack overflow I believe. I've moved around the marshaling
attributes from what you suggested to other orientations and I receive
the same error. I get an overflow exception thrown from my
application if PixelsBuffer is a byte.

Again, thank you for the input, but this thing is still kickin' my
butt!
Jun 27 '08 #7

<ag*****@cox.netwrote in message
news:a9**********************************@k37g2000 hsf.googlegroups.com...
Thanks again Bill!

Unfortunately I'm still running into issues.

As soon as I declare PixelsBuffer as an IntPtr in the function
declaration, I get a native error from the device 0xC00000FD. That is
a stack overflow I believe.
Really hard to say. It maybe that you are getting the IntPtr wrong.

Oh, and if you are developing on a x64 machine and the dll is only 32 bit,
you might want to set your output to x86 only. Other than that, I'd be
looking at how you are getting that IntPtr.
I've moved around the marshaling
attributes from what you suggested to other orientations and I receive
the same error. I get an overflow exception thrown from my
application if PixelsBuffer is a byte.

Again, thank you for the input, but this thing is still kickin' my
butt!
Jun 27 '08 #8

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

Similar topics

1
by: SpookyET | last post by:
I want to create a wrapper for SQLite and I'm having a problem with getting NullReferenceException when calling the unmanaged function. I do not wish to write in C++ since i do not have enough...
7
by: Lev | last post by:
Hi, I have an unmanaged pointer to a class that I want to hold in a managed class. I pass the pointer (from unmanaged code) in the constructor of the managed class, and at that point it has...
7
by: Bob Rock | last post by:
Hello, converting from the managed to the unmanaged world (and viceversa strings) and byte arrays is something I do often and I'd like to identify the most correct and efficient way to do it....
1
by: Bill Soudan | last post by:
Hi all, I'm a MS/.NET newbie, diving right into CLR interop. I've worked through the various issues foreign to me as a UNIX guy: multiple heap issues because I was linking to different CRT...
1
by: Sam Carleton | last post by:
The 3rd party SDK that I would like to use in C# (I am an C/C++ programmer) is designed to be used from C/C++. This is how it works: The vendor is abstracting things a bit. The real DLL has...
3
by: Tommy Svensson \(InfoGrafix\) | last post by:
I've been instructed to work againt a huge unmanaged C++ API from a C# application. Now, the only way, as I've understood it, is to go the Managed Extensions for C++ way. This means I have to...
5
by: R. MacDonald | last post by:
Hello, all, I am currently working on a .Net (VB) application that invokes routines in unmanaged (Fortran) DLLs. The unmanaged routines then communicate with the .Net application by means of a...
7
by: Entwickler | last post by:
hello, i have the following problem. i want to write a code that enables the user to call functions from a unmanaged code .dll at running time . so i have to use the late binding . i tried the...
0
by: dia sdk user | last post by:
Visual Studio 2005 Educational / DIA SDK 8.0 / .NET 2.0 (MS tech support was nice enough to give me DIA SDK 8.0.) Hello everyone: I have been working on a DIA SDK app with the tools described...
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:
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
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.