C++ Function Pointer to VB.Net

  • Thread starter Thread starter banleong
  • Start date Start date
B

banleong

To all gurus,

I am currently converting some of C++ codes to VB.net

The C++ Codes is as follows :

================= C++ CODE ==================
typedef struct _tagBBCameraParameter
{
unsigned int preview_width;
unsigned int preview_height;
unsigned int preview_x;
unsigned int preview_y;
unsigned int preview_format;
unsigned int preview_zoom;

const TCHAR* capture_file_name;

unsigned int contrast;
unsigned int saturation;
unsigned int brightness;

} BBCameraParameter;

typedef LPVOID HBBCAMERA;

BBAPI DWORD WINAPI BBCameraPreviewStart(HBBCAMERA,
BBCameraParameter*);
BBAPI DWORD WINAPI BBCameraPreviewStop(HBBCAMERA);

================= END C++ CODE ==================

I managed to convert most of the codes to VB.Net but when I try to fun
BBCameraPreviewStart function, it gives error.

'=================Partial VB.Net Code


Public Shared Function BBCameraPreviewStart(ByVal HBBCamera As IntPtr,
ByRef BBParam1 As BBCameraParameter) As Integer
End Function

Public Shared Function BBCameraPreviewStop(ByVal HBBCamera As IntPtr)
As Integer
End Function

'=========== END OF PARTIAL VB.NET CODES

This is how i pass the parameters;

Dim BBParam as BBCameraParameter '// already declared but not shown
here
Dim HBBCamera as IntPtr '//Handle

'I have no problems getting handle but once i set the parameters for
BBParam and pass as follows, i get error (not much help from the
message).

BBCameraPreviewStart(HBBCAMERA, BBParam)

How do I call function BBCameraPreviewStart correctly? Did i do it
correctly?

Please advices.
Thanks
 
banleong wrote:
I am currently converting some of C++ codes to VB.net
BBAPI DWORD WINAPI BBCameraPreviewStart(HBBCAMERA,
BBCameraParameter*);
BBAPI DWORD WINAPI BBCameraPreviewStop(HBBCAMERA);

================= END C++ CODE ==================

I managed to convert most of the codes to VB.Net but when I try to fun
BBCameraPreviewStart function, it gives error.

'=================Partial VB.Net Code

Public Shared Function BBCameraPreviewStart(ByVal HBBCamera As IntPtr,
ByRef BBParam1 As BBCameraParameter) As Integer
End Function

Public Shared Function BBCameraPreviewStop(ByVal HBBCamera As IntPtr)
As Integer
End Function
<snip>

Don't those need to be Declares to external libraries instead of local
methods?

Something like:

Declare Function BBCameraPreviewStart Lib "???" ( _
ByVal HBBCamera As IntPtr,
ByRef BBParam1 As BBCameraParameter) As Integer


Declare Function BBCameraPreviewStop Lib "???" ( _
ByVal HBBCamera As IntPtr) As Integer


HTH.

Regards,

Branco.
 
I think function pointers are replaced (at least functionally) by
delegates. I'm not entirely sure what you're trying to do her, so I
can't suggest any code easily, but maybe look into using delegates
instead of function pointers.

What kind of error are you getting?
 
Branco,

Yes, it is external libraries.

Lord Zoltar,
Basically, when i tried to pass the parameter as follows in this
function, i got error:

BBCameraPreviewStart(HBBCAMERA, BBParam)

where HBBCamera is the handler and BBParam is the struction
declaration of BBCameraParameter.

I assume there is not problem getting the handler because I can call
other functions like getting camera info by passing the handler
(function not shown here).

So, I assume there could be the way I declare the structure or passing
parameters of BBParam that causes the error.

Perhaps i should break down by questions to:
1) Is my declaration correct for BBCameraPreviewStart in VB.net code
based on the C++ codes?

BBAPI DWORD WINAPI BBCameraPreviewStart(HBBCAMERA,
BBCameraParameter*);

to

Public Shared Function BBCameraPreviewStart(ByVal HBBCamera As IntPtr,
ByRef BBParam1 As BBCameraParameter) As Integer
End Function

2) Did i pass the parameter correct when calling BBCameraPreviewStart
function? Below are my procedure

Dim BBParam As BBCameraParameter 'Declare variable to hold
BBCameraParameter structure
With BBParam
.preview_width = 0
.preview_height = 100
.preview_format = 0
.ImageQuality = 5
.brightness = 5
.rotation = rotation_degree.rotate_0
.preview_x = 0
.preview_y = 0
.contrast = 200
.saturation = 200
.ef_mode = effect_mode.effect_none
.wb_mode = white_balance_mode.wb_auto
Dim pbuffer() As Byte
ReDim pbuffer(.preview_width * .preview_height * 2)
ReDim .reserved(20)
.p_app_buffer = pbuffer
.p_app_capture_buffer = pbuffer

End With

'Call function
BBCameraPreviewStart (Handler, BBParam)

Am i doing the right thing here?

Thanks and Regards
 
banleong wrote:
Yes, it is external libraries.
1) Is my declaration correct for BBCameraPreviewStart in VB.net code
based on the C++ codes?

BBAPI DWORD WINAPI BBCameraPreviewStart(HBBCAMERA,
BBCameraParameter*);

to

Public Shared Function BBCameraPreviewStart(ByVal HBBCamera As IntPtr,
ByRef BBParam1 As BBCameraParameter) As Integer
End Function
<snip>

Nope. When you have to reference a method in an external library, you
need to indicate to VB which library is this (the .dll file), the
character type accepted by the method (ansi or unicode) and the method
signature, among other things.

Usually this is done using a Declare. Suppose, for instance, that the
method that you want to call is from, say, a dll called BBAPI.dll.
This would be the declaration of the function in VB:

Declare Ansi Function BBCameraPreviewStart _
Lib "BBAPI.dll" ( _
ByVal HBBCamera As IntPtr,
ByRef BBParam1 As BBCameraParameter) As Integer

Of course, this is just the declaration. To actually *call* the
method, it's no different from any other methods.

HTH.

Regards,

Branco.
 
banleong wrote:




<snip>

Nope. When you have to reference a method in an external library, you
need to indicate to VB which library is this (the .dll file), the
character type accepted by the method (ansi or unicode) and the method
signature, among other things.

Usually this is done using a Declare. Suppose, for instance, that the
method that you want to call is from, say, a dll called BBAPI.dll.
This would be the declaration of the function in VB:

Declare Ansi Function BBCameraPreviewStart _
Lib "BBAPI.dll" ( _
ByVal HBBCamera As IntPtr,
ByRef BBParam1 As BBCameraParameter) As Integer

Of course, this is just the declaration. To actually *call* the
method, it's no different from any other methods.

HTH.

Regards,

Branco.

Branco,

Thanks for your reply.

Actually i already declare the external function as

<DllImport("bbappapi.dll")> _
Public Shared Function BBCameraPreviewStart(ByVal HBBCamera As
IntPtr, ByRef BBParam1 As BBCameraParameter) As Integer
End Function

Sorry for the confusion because i didn't include the <DllImport> part
on early post.
However, passing the parameters seem to failed.Guess have not choice
but go back to manufacturer, eh?

Regards
 
hey C++ kid

go and play in a freeway

C++ is dead and so is VB


SERIOUSLY

it's about time that someone told you the truth-- MS had a full hand--
the worlds most popular programming language; and they FOLDED. THEY
GAVE UP AND KILLED THE LANGUAGE; BECAUSE THE KIDS IN REDMOND WERE
SCARED OF A TINY COMPANY FROM CALIFORNIA WITH A THREE BILLION DOLLAR
MARKET CAP.


C++ is dead.
Java is dead.
VB is dead.

PHP won the war.


I'm dead ****ing serious


-Todos
 
banleong wrote:
Actually i already declare the external function as

<DllImport("bbappapi.dll")> _
Public Shared Function BBCameraPreviewStart(ByVal HBBCamera As
<snip>

I suggest you try to provide a more complete description of your
scenario. Having known that you had already declared the function with
DllImport would spare a couple a posts, don't you think? Also, in your
first post you say that you get an error message while calling the
function, but don't provide it for lack of relevancy in the message.
Well, maybe the message *is irrelevant*, but then, again, maybe not
(the "DllImport" wasn't irrelevant at all, you see?).

Also missing is an example of the original C++ code filling the
paramenters (sometimes thare's a gotcha that passes unnoticed).

Finally -- to give some purpose to this post -- I noticed that the
fields you show in the C++ declaration of the BBCameraParameter struct
don't seem to match your usage of the parameter. It would help also if
you provided your actual VB declaration of the struct.

HTH.

Branco.
 
HBBCAMERA is a typedef pointer, so you'll have to pass the first parameter
ByRef. If that doesn't work, try changing the IntPtr to an UInt32

Public Shared Function BBCameraPreviewStart(ByRef HBBCamera As IntPtr,
ByRef BBParam1 As BBCameraParameter) As Integer
End Function

or

Dim HBBCamera as Unit32

Public Shared Function BBCameraPreviewStart(ByRef HBBCamera As UInt32,
ByRef BBParam1 As BBCameraParameter) As Integer
End Function
 
Branco,

Thanks for your comment and sorry about the mixed up and confusion.
Below is the complete source code in C++ and converted VB.Net source
code.

The C++ code (bbappapi.h) is provided by hardware manufacturer.
However, they don't have any samples available in .Net and their
technical support is unreachable most of the time.

I also include the VB.Net source codes that i converted from C++ codes
and a function to call the methods. The error message is
"NotSupportedException" which i think it's useless, or is it?

I hope i didn't miss out other information here. By the way, it's for
Windows CE 5.0 device.

-------------------- Begin C++ Code (bbappapi.h)
------------------------------

/*
* camera
*/
#define WM_BBCAMERA_UPDATE_FPS WM_USER+1000

#define BB_CAMERA_CODE_SUCCESS 0
#define BB_CAMERA_CODE_ERR_UNKNOWN 1
#define BB_CAMERA_CODE_ERR_INVALID_HANDLE 2
#define BB_CAMERA_CODE_ERR_INVALID_PARAMETER 3

typedef enum
{
wb_auto = 0,
wb_cloudy,
wb_daylight,
wb_fluorescent_1,
wb_fluorescent_2,
wb_light_bulb

} white_balance_mode;

typedef enum
{
effect_none = 0,
effect_negative,
effect_embossing,
effect_black_n_white,
effect_sketch,
effect_solarization,
effect_sephia,
effect_aqua,
effect_posterize,
effect_warm,
effect_cool,
effect_antique,
effect_moonlight,
effect_fog,

} effect_mode;

typedef enum
{
flip_x = 1,
flip_y,
flip_x_y,
flip_origin


} flip_mode;

typedef enum
{
rate_auto = 1,
rate_day_manual,
rate_night_manual
} frame_rate_mode;

typedef enum
{
auto_save = 0,
manual_save
} save_mode;

typedef enum
{
file_type_jpg = 0,
file_type_bmp
} save_image_type;

typedef enum
{
rotate_default=0,
rotate_0,
rotate_90,
rotate_180,
rotate_270

} rotation_degree;

typedef enum
{
pixel_format_ycbcr = 0,
pixel_format_rgb_565, // output data °¡ bmp ÀÌ´Ù.
pixel_format_rgb_565_raw // output data °¡ 16bit rgb µ¥ÀÌÅÍ ÀÌ´Ù.

} pixel_format_select;


typedef struct _tagBBCameraParameter
{
unsigned int preview_width;
unsigned int preview_height;
unsigned int preview_x;
unsigned int preview_y;
unsigned int preview_format;
unsigned int preview_zoom;

const TCHAR* capture_file_name;
unsigned int capture_width;
unsigned int capture_height;
pixel_format_select capture_format;
unsigned int capture_strobe_on;

unsigned int contrast;
unsigned int saturation;
unsigned int brightness;
effect_mode ef_mode;
white_balance_mode wb_mode;
flip_mode fp_mode;
frame_rate_mode fr_mode;

unsigned int reserved[20];
BYTE* p_app_buffer;
BYTE* p_app_capture_buffer; //still image capture¿¡¼­ µ¥ÀÌÅ͸¦ ¹Þ
±âÀ§ÇÑ Æ÷ÀÎÅÍ

save_mode stillimage_savemode;
save_image_type save_fileType;
unsigned int ImageQuality;

BOOL isSaveFile;
rotation_degree rotation;

} BBCameraParameter;

typedef struct _tagBBCameraInfo
{
unsigned int preview_max_width;
unsigned int preview_max_height;
unsigned int preview_min_width;
unsigned int preview_min_height;
unsigned int image_max_width;
unsigned int image_max_height;

unsigned int reserved[20];

} BBCameraInfo;


typedef LPVOID HBBCAMERA;

BBAPI HBBCAMERA WINAPI BBCameraOpen(HWND);
BBAPI DWORD WINAPI BBCameraClose(HBBCAMERA);

BBAPI DWORD WINAPI BBCameraPreviewStart(HBBCAMERA,
BBCameraParameter*);
BBAPI DWORD WINAPI BBCameraPreviewStop(HBBCAMERA);
BBAPI DWORD WINAPI BBCameraPreviewPause(HBBCAMERA);
BBAPI DWORD WINAPI BBCameraPreviewResume(HBBCAMERA);
BBAPI DWORD WINAPI BBCameraPreviewZoom(HBBCAMERA, BBCameraParameter*);

BBAPI DWORD WINAPI BBCameraGetRawDataStart(HBBCAMERA,
BBCameraParameter*);
BBAPI DWORD WINAPI BBCameraGetRawDataStop(HBBCAMERA);
BBAPI DWORD WINAPI BBCameraGetRawDataPause(HBBCAMERA);
BBAPI DWORD WINAPI BBCameraGetRawDataResume(HBBCAMERA);
BBAPI DWORD WINAPI BBCameraGetRawDataZoom(HBBCAMERA,
BBCameraParameter*);

BBAPI DWORD WINAPI BBCameraCapture(HBBCAMERA, BBCameraParameter*);
BBAPI DWORD WINAPI BBCameraStoreCaptureImage(HBBCAMERA,
BBCameraParameter*);
BBAPI DWORD WINAPI BBCameraRestartPreviewFromCapture(HBBCAMERA,
BBCameraParameter*);

BBAPI DWORD WINAPI BBCameraSetWhiteBalance(HBBCAMERA,
BBCameraParameter*);
BBAPI DWORD WINAPI BBCameraSetContrast(HBBCAMERA, BBCameraParameter*);
BBAPI DWORD WINAPI BBCameraSetSaturation(HBBCAMERA,
BBCameraParameter*);
BBAPI DWORD WINAPI BBCameraSetBrightness(HBBCAMERA,
BBCameraParameter*);
BBAPI DWORD WINAPI BBCameraSetEffect(HBBCAMERA, BBCameraParameter*);
BBAPI DWORD WINAPI BBCameraSetFlip(HBBCAMERA, BBCameraParameter*);
BBAPI DWORD WINAPI BBCameraNightMode(HBBCAMERA, BBCameraParameter*);

BBAPI DWORD WINAPI BBCameraGetInfo(HBBCAMERA, BBCameraInfo*);
BBAPI DWORD WINAPI BBCameraDumpRegister(HBBCAMERA);

------------------------------ END of C++ Code -----------------------

---------------------------- Declaration in VB.Net------------
#Region " BB CAMERA API DECLARATIONS "
Public Const BB_CAMERA_CODE_SUCCESS = 0
Public Const BB_CAMERA_CODE_ERR_UNKNOWN = 1
Public Const BB_CAMERA_CODE_ERR_INVALID_HANDLE = 2
Public Const BB_CAMERA_CODE_ERR_INVALID_PARAMETER = 3

Enum white_balance_mode
wb_auto = 0
wb_cloudy
wb_daylight
wb_fluorescent_1
wb_fluorescent_2
wb_light_bulb
End Enum

Enum effect_mode
effect_none = 0
effect_negative
effect_embossing
effect_black_n_white
effect_sketch
effect_solarization
effect_sephia
effect_aqua
effect_posterize
effect_warm
effect_cool
effect_antique
effect_moonlight
effect_fog
End Enum

Enum flip_mode
flip_x = 1
flip_y
flip_x_y
flip_origin
End Enum

Enum frame_rate_mode
rate_auto = 1
rate_day_manual
rate_night_manual
End Enum

Enum save_mode
auto_save = 0
manual_save
End Enum

Enum save_image_type
file_type_jpg = 0
file_type_bmp
End Enum

Enum rotation_degree
rotate_default = 0
rotate_0
rotate_90
rotate_180
rotate_270
End Enum

Enum pixel_format_select
pixel_format_ycbcr = 0
pixel_format_rgb_565 ' // output data °¡ bmp ÀÌ´Ù.
pixel_format_rgb_565_raw ' // output data °¡ 16bit rgb µ¥ÀÌÅÍ
ÀÌ´Ù.
End Enum

Structure BBCameraInfo
Public preview_max_width As System.UInt16
Public preview_max_height As System.UInt16
Public preview_min_width As System.UInt16
Public image_max_width As System.UInt16
Public image_max_height As System.UInt16
End Structure


Structure BBCameraParameter
Public preview_width As System.UInt16
Public preview_height As System.UInt16
Public preview_x As System.UInt16
Public preview_y As System.UInt16
Public preview_format As System.UInt16
Public preview_zoom As System.UInt16

Public capture_file_name As String

Public capture_width As System.UInt16
Public capture_height As System.UInt16
Public capture_strobe_on As System.UInt16

Public capture_format As pixel_format_select

Public contrast As System.UInt16
Public saturation As System.UInt16
Public brightness As System.UInt16


Public ef_mode As effect_mode
Public wb_mode As white_balance_mode
Public fp_mode As flip_mode
Public fr_mode As frame_rate_mode

' unsigned int reserved[20];
Public reserved() As System.UInt16

Public p_app_buffer() As Byte
Public p_app_capture_buffer() As Byte

Public stillimage_savemode As save_mode
Public save_fileType As save_image_type
Public ImageQuality As Integer

Public isSaveFile As Boolean

Public rotation As rotation_degree
End Structure
#End Region

#Region " VARIABLES DECLARATION "
Dim HBBCAMERA As IntPtr
Dim BBInfo As New BBCameraInfo
Dim BBParam As New BBCameraParameter

#End Region

#Region " BB CAMERA API"
<DllImport("bbappapi.dll")> _
Public Shared Function BBCameraOpen(ByVal HWND As IntPtr)
As IntPtr
End Function

<DllImport("bbappapi.dll")> _
Public Shared Function BBCameraClose(ByVal HBBCamera As IntPtr)
As Integer
End Function

<DllImport("bbappapi.dll")> _
Public Shared Function BBCameraPreviewStart(ByVal HBBCamera As
IntPtr, ByRef BBParam1 As BBCameraParameter) As Integer
End Function

<DllImport("bbappapi.dll")> _
Public Shared Function BBCameraPreviewStop(ByVal HBBCamera As IntPtr)
As Integer
End Function

<DllImport("bbappapi.dll")> _
Public Shared Function BBCameraPreviewPause(ByVal HBBCamera As IntPtr)
As Integer
End Function

<DllImport("bbappapi.dll")> _
Public Shared Function BBCameraPreviewZoom(ByVal HBBCamera As IntPtr)
As Integer
End Function

<DllImport("bbappapi.dll")> _
Public Shared Function BBCameraGetInfo(ByVal HBBCamera As IntPtr,
ByRef BBInfo As BBCameraInfo) As Integer
End Function

<DllImport("bbappapi.dll")> _
Public Shared Function BBCameraGetRawDataStart(ByVal HBBCamera As
IntPtr, ByRef BBParam As BBCameraParameter) As Integer
End Function

<DllImport("bbappapi.dll")> _
Public Shared Function BBCameraGetRawDataStop(ByVal HBBCamera As
IntPtr) As Integer
End Function

<DllImport("bbappapi.dll")> _
Public Shared Function BBCameraGetRawDataPause(ByVal HBBCamera As
IntPtr, ByRef BBParam1 As BBCameraParameter) As Integer
End Function

<DllImport("bbappapi.dll")> _
Public Shared Function BBCameraGetRawDataResume(ByVal HBBCamera As
IntPtr) As Integer
End Function

<DllImport("bbappapi.dll")> _
Public Shared Function BBCameraCapture(ByVal HBBCamera As IntPtr,
ByRef BBParam1 As BBCameraParameter) As Integer
End Function
#End Region


' So this is the method that i call to
'
Private Sub StartOperation()

Dim BBParam As BBCameraParameter
Dim StartCamera as boolean

Try
HBBCAMERA = BBCameraOpen(picCamera.Handle) 'Get the
handler
StartCamera = True
Catch ex As Exception
StartCamera = False
End Try

If StartCamera Then
' If camera started then assign some parameters
With BBParam
.preview_width = 0
.preview_height = 100
.preview_format = 0
.ImageQuality = 5
.brightness = 50
.rotation = rotation_degree.rotate_0
.preview_x = 0
.preview_y = 0
.contrast = 200
.saturation = 200
.ef_mode = effect_mode.effect_none
.wb_mode = white_balance_mode.wb_auto
Dim pbuffer() As Byte
ReDim pbuffer(.preview_width * .preview_height * 2)
ReDim .reserved(20)
.p_app_buffer = pbuffer
.p_app_capture_buffer = pbuffer
End With


TextBox1.Text = "Get handle :" & HBBCAMERA.ToString

'GET CAMERA INFO (No problem here, can get the info from
camera)
BBCameraGetInfo(HBBCAMERA, BBInfo)
TextBox1.Text += vbCrLf & "BBInfo.image_max_height:" &
BBInfo.image_max_height
TextBox1.Text += vbCrLf & "BBInfo.image_max_width:" &
BBInfo.image_max_width
TextBox1.Text += vbCrLf & "BBInfo.preview_max_height:" &
BBInfo.preview_max_height
TextBox1.Text += vbCrLf & "BBInfo.preview_max_width:" &
BBInfo.preview_max_width
TextBox1.Text += vbCrLf & "BBInfo.preview_min_width:" &
BBInfo.preview_min_width

Try
'Preview camera image (ERROR OCCURS HERE)
Dim rst As Integer = BBCameraPreviewStart(HBBCAMERA,
BBParam)
TextBox1.Text += vbCrLf & "Preview OK"

Catch ex As Exception
TextBox1.Text += vbCrLf & "Preview Failed"

End Try

Else
TextBox1.Text += vbCrLf & "Cannot start camera"
End If
End Sub
----------------------------- 'END OF VB.Net
Codes---------------------

Hope to hear from you guys.

Thanks again!
 
banleong wrote:
<snipped due to sheer size>

Yikes, it seems Google groups didn't like my post. Here it goes again
(sorry if it gets duplicated...)

Yes, there were issues with you conversion of the structures: missing,
wrongly typed and out-of-order fields, as well as missing marshalling
information. This may be causing your code to fail.

Try the rewrites below, I hope they're somewhat more "correct" than
the ones you already have.

<code>
'At file level
Imports InterOp = System.Runtime.InteropServices

'Inside your class
Public Const RESERVED_FIELD_MAX As integer = 19

<InterOp.StructLayout( _
InterOp.LayoutKind.Sequential, _
Charset:=InterOp.CharSet.Ansi)> _
Structure BBCameraParameter
Public preview_width As UInteger
Public preview_height As UInteger
Public preview_x As UInteger
Public preview_y As UInteger
Public preview_format As UInteger
Public preview_zoom As UInteger


<InterOp.MarshalAs(InterOp.UnmanagedType.LPStr)> _
Public capture_file_name As String

Public capture_width As UInteger
Public capture_height As UInteger
Public capture_format As pixel_format_select
Public capture_strobe_on As UInteger


Public contrast As UInteger
Public saturation As UInteger
Public brightness As UInteger
Public ef_mode As effect_mode
Public wb_mode As white_balance_mode
Public fp_mode As flip_mode
Public fr_mode As frame_rate_mode

'Must initialize explicitly
<VBFixedArray(RESERVED_FIELD_MAX)> _
Public reserved() As UInteger

<InterOp.MarshalAs(InterOp.UnmanagedType.LPArray)> _
Public p_app_buffer() As Byte
<InterOp.MarshalAs(InterOp.UnmanagedType.LPArray)> _
Public p_app_capture_buffer() As Byte


Public stillimage_savemode As save_mode
Public save_fileType As save_image_type
Public ImageQuality As UInteger

Public isSaveFile As Boolean
Public rotation_degree As rotation_degree

End Structure

<InterOp.StructLayout(InterOp.LayoutKind.Sequential)> _
Structure BBCameraInfo
Public preview_max_width As UInteger
Public preview_max_height As UInteger
Public preview_min_width As UInteger
Public preview_min_height As UInteger
Public image_max_width As UInteger
Public image_max_height As UInteger

'Must initialize explicitly
<VBFixedArray(RESERVED_FIELD_MAX)> _
Public reserved() As UInteger
End Structure

<InterOp.DllImport("bbappapi.dll", Charset:=InterOp.CharSet.Ansi)> _
Public Shared Function BBCameraPreviewStart( _
ByVal HBBCamera As IntPtr, _
ByRef BBParam1 As BBCameraParameter) As Integer
End Function
</code>

Just remember that when declaring instances of both BBCameraParamete
and BBCamera info you need to explicitly dimmension the reserverd
field:

Dim Param As New BBCameraParameter
ReDim Param.reserved(RESERVED_FIELD_MAX)

Dim Info As New BBCameraInfo
ReDim Info.reserved(RESERVED_FIELD_MAX)


HTH.

Regards,

Branco.
 
Hi,

I am facing the same problem. I've no experience in pointer and c++. Can you please convert this sample to vb.net?

I've converted a little bit and i dont understand how to go on when it comes to CAMERA_GetJpegBits where pointer is involved. Pls help:
<DllImport("\Storage Card\USBCAM.dll")> _
Private Shared Function CAMERA_InitDevice() As [Boolean]
End Function
<DllImport("\Storage Card\USBCAM.dll")> _
Private Shared Function CAMERA_CloseDevice() As [Boolean]
End Function
<DllImport("\Storage Card\USBCAM.dll")> _
Private Shared Function CAMERA_StartCapture() As [Boolean]
End Function
<DllImport("\Storage Card\USBCAM.dll")> _
Private Shared Function CAMERA_StopCapture() As [Boolean]
End Function
<DllImport("\Storage Card\USBCAM.dll")> _
Private Shared Function CAMERA_GetJpegBits(ByRef lpJpegBuf As Integer, ByRef nBufSize As Integer, ByRef lpBytesReturned As Integer) As [Boolean]
End Function


Below is the c++ sample.

BOOL CAMERA_InitDevice (void );

BOOL CAMERA_CloseDevice (void);

BOOL CAMERA_StartCapture (void);

BOOL CAMERA_StopCapture (void);

BOOL CAMERA_GetJpegBits (LVOID lpJpegBuf,DWORD nBufSize,LPWORD
lpBytesReturned);

Parameters
lpJpegBuf
[out] Pointer used to point to the buffer saving a JPEG picture.
nBufSize
[in] lpJpegBuf used to point to the size of buffer.
lpBytesReturned
[out] return the byte number of the got picture.
Return Value
Return TRUE if the picture is got successfully, or else return FALSE





USB Camera Example

#define JPEG_BUFFER_SIZE 1024*10
Development Guide of WinCE Platform for iClock V2.0
Copyright ? ZKSoftware Inc. 2008-2009 All rights reserved 69
UINT8 lpJpegBuf[JPEG_BUFFER_SIZE] = {0,};
DWORD dwBufSize = JPEG_BUFFER_SIZE;
DWORD dwBytesReturned = 0;


if( !CAMERA_InitDevice())
{
//TODO:Add handle code here for initializing usb camera device fail
return;
}
if ( !CAMERA_StartCapture())
{
//TODO:Add handle code here for function called fail
return;
}
if( CAMERA_GetJpegBits((PUINT8)lpJpegBuf, dwBufSize,&dwBytesReturned))
{
//Have got one jpeg file
//Add your code here
}
CAMERA_StopCapture();
CAMERA_CloseDevice();
To all gurus,

I am currently converting some of C++ codes to VB.net

The C++ Codes is as follows :

================= C++ CODE ==================
typedef struct _tagBBCameraParameter
{
unsigned int preview_width;
unsigned int preview_height;
unsigned int preview_x;
unsigned int preview_y;
unsigned int preview_format;
unsigned int preview_zoom;

const TCHAR* capture_file_name;

unsigned int contrast;
unsigned int saturation;
unsigned int brightness;

} BBCameraParameter;

typedef LPVOID HBBCAMERA;

BBAPI DWORD WINAPI BBCameraPreviewStart(HBBCAMERA,
BBCameraParameter*);
BBAPI DWORD WINAPI BBCameraPreviewStop(HBBCAMERA);

================= END C++ CODE ==================

I managed to convert most of the codes to VB.Net but when I try to fun
BBCameraPreviewStart function, it gives error.

'=================Partial VB.Net Code


Public Shared Function BBCameraPreviewStart(ByVal HBBCamera As IntPtr,
ByRef BBParam1 As BBCameraParameter) As Integer
End Function

Public Shared Function BBCameraPreviewStop(ByVal HBBCamera As IntPtr)
As Integer
End Function

'=========== END OF PARTIAL VB.NET CODES

This is how i pass the parameters;

Dim BBParam as BBCameraParameter '// already declared but not shown
here
Dim HBBCamera as IntPtr '//Handle

'I have no problems getting handle but once i set the parameters for
BBParam and pass as follows, i get error (not much help from the
message).

BBCameraPreviewStart(HBBCAMERA, BBParam)

How do I call function BBCameraPreviewStart correctly? Did i do it
correctly?

Please advices.
Thanks
<snip>
<snip>
<snip>

Don't those need to be Declares to external libraries instead of local
methods?

Something like:

Declare Function BBCameraPreviewStart Lib "???" ( _
ByVal HBBCamera As IntPtr,
ByRef BBParam1 As BBCameraParameter) As Integer


Declare Function BBCameraPreviewStop Lib "???" ( _
ByVal HBBCamera As IntPtr) As Integer


HTH.

Regards,

Branco.
On Thursday, March 15, 2007 11:05 PM banleon wrote:
Branco,

Thanks for your comment and sorry about the mixed up and confusion.
Below is the complete source code in C++ and converted VB.Net source
code.

The C++ code (bbappapi.h) is provided by hardware manufacturer.
However, they don't have any samples available in .Net and their
technical support is unreachable most of the time.

I also include the VB.Net source codes that i converted from C++ codes
and a function to call the methods. The error message is
"NotSupportedException" which i think it's useless, or is it?

I hope i didn't miss out other information here. By the way, it's for
Windows CE 5.0 device.

-------------------- Begin C++ Code (bbappapi.h)
------------------------------

/*
* camera
*/


typedef enum
{
wb_auto =3D 0,
wb_cloudy,
wb_daylight,
wb_fluorescent_1,
wb_fluorescent_2,
wb_light_bulb

} white_balance_mode;

typedef enum
{
effect_none =3D 0,
effect_negative,
effect_embossing,
effect_black_n_white,
effect_sketch,
effect_solarization,
effect_sephia,
effect_aqua,
effect_posterize,
effect_warm,
effect_cool,
effect_antique,
effect_moonlight,
effect_fog,

} effect_mode;

typedef enum
{
flip_x =3D 1,
flip_y,
flip_x_y,
flip_origin


} flip_mode;

typedef enum
{
rate_auto =3D 1,
rate_day_manual,
rate_night_manual
} frame_rate_mode;

typedef enum
{
auto_save =3D 0,
manual_save
} save_mode;

typedef enum
{
file_type_jpg =3D 0,
file_type_bmp
} save_image_type;

typedef enum
{
rotate_default=3D0,
rotate_0,
rotate_90,
rotate_180,
rotate_270

} rotation_degree;

typedef enum
{
pixel_format_ycbcr =3D 0,
pixel_format_rgb_565, // output data =B0=A1 bmp =C0=CC=B4=D9.
pixel_format_rgb_565_raw // output data =B0=A1 16bit rgb =B5=A5=C0=CC=C5=
=CD =C0=CC=B4=D9.

} pixel_format_select;


typedef struct _tagBBCameraParameter
{
unsigned int preview_width;
unsigned int preview_height;
unsigned int preview_x;
unsigned int preview_y;
unsigned int preview_format;
unsigned int preview_zoom;

const TCHAR* capture_file_name;
unsigned int capture_width;
unsigned int capture_height;
pixel_format_select capture_format;
unsigned int capture_strobe_on;

unsigned int contrast;
unsigned int saturation;
unsigned int brightness;
effect_mode ef_mode;
white_balance_mode wb_mode;
flip_mode fp_mode;
frame_rate_mode fr_mode;

unsigned int reserved[20];
BYTE* p_app_buffer;
BYTE* p_app_capture_buffer; //still image capture=BF=A1=BC=AD =B5=A5=C0=
=CC=C5=CD=B8=A6 =B9=DE
=B1=E2=C0=A7=C7=D1 =C6=F7=C0=CE=C5=CD

save_mode stillimage_savemode;
save_image_type save_fileType;
unsigned int ImageQuality;

BOOL isSaveFile;
rotation_degree rotation;

} BBCameraParameter;

typedef struct _tagBBCameraInfo
{
unsigned int preview_max_width;
unsigned int preview_max_height;
unsigned int preview_min_width;
unsigned int preview_min_height;
unsigned int image_max_width;
unsigned int image_max_height;

unsigned int reserved[20];

} BBCameraInfo;


typedef LPVOID HBBCAMERA;

BBAPI HBBCAMERA WINAPI BBCameraOpen(HWND);
BBAPI DWORD WINAPI BBCameraClose(HBBCAMERA);

BBAPI DWORD WINAPI BBCameraPreviewStart(HBBCAMERA,
BBCameraParameter*);
BBAPI DWORD WINAPI BBCameraPreviewStop(HBBCAMERA);
BBAPI DWORD WINAPI BBCameraPreviewPause(HBBCAMERA);
BBAPI DWORD WINAPI BBCameraPreviewResume(HBBCAMERA);
BBAPI DWORD WINAPI BBCameraPreviewZoom(HBBCAMERA, BBCameraParameter*);

BBAPI DWORD WINAPI BBCameraGetRawDataStart(HBBCAMERA,
BBCameraParameter*);
BBAPI DWORD WINAPI BBCameraGetRawDataStop(HBBCAMERA);
BBAPI DWORD WINAPI BBCameraGetRawDataPause(HBBCAMERA);
BBAPI DWORD WINAPI BBCameraGetRawDataResume(HBBCAMERA);
BBAPI DWORD WINAPI BBCameraGetRawDataZoom(HBBCAMERA,
BBCameraParameter*);

BBAPI DWORD WINAPI BBCameraCapture(HBBCAMERA, BBCameraParameter*);
BBAPI DWORD WINAPI BBCameraStoreCaptureImage(HBBCAMERA,
BBCameraParameter*);
BBAPI DWORD WINAPI BBCameraRestartPreviewFromCapture(HBBCAMERA,
BBCameraParameter*);

BBAPI DWORD WINAPI BBCameraSetWhiteBalance(HBBCAMERA,
BBCameraParameter*);
BBAPI DWORD WINAPI BBCameraSetContrast(HBBCAMERA, BBCameraParameter*);
BBAPI DWORD WINAPI BBCameraSetSaturation(HBBCAMERA,
BBCameraParameter*);
BBAPI DWORD WINAPI BBCameraSetBrightness(HBBCAMERA,
BBCameraParameter*);
BBAPI DWORD WINAPI BBCameraSetEffect(HBBCAMERA, BBCameraParameter*);
BBAPI DWORD WINAPI BBCameraSetFlip(HBBCAMERA, BBCameraParameter*);
BBAPI DWORD WINAPI BBCameraNightMode(HBBCAMERA, BBCameraParameter*);

BBAPI DWORD WINAPI BBCameraGetInfo(HBBCAMERA, BBCameraInfo*);
BBAPI DWORD WINAPI BBCameraDumpRegister(HBBCAMERA);

------------------------------ END of C++ Code -----------------------

---------------------------- Declaration in VB.Net------------
Public Const BB_CAMERA_CODE_SUCCESS =3D 0
Public Const BB_CAMERA_CODE_ERR_UNKNOWN =3D 1
Public Const BB_CAMERA_CODE_ERR_INVALID_HANDLE =3D 2
Public Const BB_CAMERA_CODE_ERR_INVALID_PARAMETER =3D 3

Enum white_balance_mode
wb_auto =3D 0
wb_cloudy
wb_daylight
wb_fluorescent_1
wb_fluorescent_2
wb_light_bulb
End Enum

Enum effect_mode
effect_none =3D 0
effect_negative
effect_embossing
effect_black_n_white
effect_sketch
effect_solarization
effect_sephia
effect_aqua
effect_posterize
effect_warm
effect_cool
effect_antique
effect_moonlight
effect_fog
End Enum

Enum flip_mode
flip_x =3D 1
flip_y
flip_x_y
flip_origin
End Enum

Enum frame_rate_mode
rate_auto =3D 1
rate_day_manual
rate_night_manual
End Enum

Enum save_mode
auto_save =3D 0
manual_save
End Enum

Enum save_image_type
file_type_jpg =3D 0
file_type_bmp
End Enum

Enum rotation_degree
rotate_default =3D 0
rotate_0
rotate_90
rotate_180
rotate_270
End Enum

Enum pixel_format_select
pixel_format_ycbcr =3D 0
pixel_format_rgb_565 ' // output data =B0=A1 bmp =C0=CC=B4=D9.
pixel_format_rgb_565_raw ' // output data =B0=A1 16bit rgb =B5=A5=
=C0=CC=C5=CD
=C0=CC=B4=D9.
End Enum

Structure BBCameraInfo
Public preview_max_width As System.UInt16
Public preview_max_height As System.UInt16
Public preview_min_width As System.UInt16
Public image_max_width As System.UInt16
Public image_max_height As System.UInt16
End Structure


Structure BBCameraParameter
Public preview_width As System.UInt16
Public preview_height As System.UInt16
Public preview_x As System.UInt16
Public preview_y As System.UInt16
Public preview_format As System.UInt16
Public preview_zoom As System.UInt16

Public capture_file_name As String

Public capture_width As System.UInt16
Public capture_height As System.UInt16
Public capture_strobe_on As System.UInt16

Public capture_format As pixel_format_select

Public contrast As System.UInt16
Public saturation As System.UInt16
Public brightness As System.UInt16


Public ef_mode As effect_mode
Public wb_mode As white_balance_mode
Public fp_mode As flip_mode
Public fr_mode As frame_rate_mode

' unsigned int reserved[20];
Public reserved() As System.UInt16

Public p_app_buffer() As Byte
Public p_app_capture_buffer() As Byte

Public stillimage_savemode As save_mode
Public save_fileType As save_image_type
Public ImageQuality As Integer

Public isSaveFile As Boolean

Public rotation As rotation_degree
End Structure

Dim HBBCAMERA As IntPtr
Dim BBInfo As New BBCameraInfo
Dim BBParam As New BBCameraParameter


<DllImport("bbappapi.dll")> _
Public Shared Function BBCameraOpen(ByVal HWND As IntPtr)
As IntPtr
End Function

<DllImport("bbappapi.dll")> _
Public Shared Function BBCameraClose(ByVal HBBCamera As IntPtr)
As Integer
End Function

<DllImport("bbappapi.dll")> _
Public Shared Function BBCameraPreviewStart(ByVal HBBCamera As
IntPtr, ByRef BBParam1 As BBCameraParameter) As Integer
End Function

<DllImport("bbappapi.dll")> _
Public Shared Function BBCameraPreviewStop(ByVal HBBCamera As IntPtr)
As Integer
End Function

<DllImport("bbappapi.dll")> _
Public Shared Function BBCameraPreviewPause(ByVal HBBCamera As IntPtr)
As Integer
End Function

<DllImport("bbappapi.dll")> _
Public Shared Function BBCameraPreviewZoom(ByVal HBBCamera As IntPtr)
As Integer
End Function

<DllImport("bbappapi.dll")> _
Public Shared Function BBCameraGetInfo(ByVal HBBCamera As IntPtr,
ByRef BBInfo As BBCameraInfo) As Integer
End Function

<DllImport("bbappapi.dll")> _
Public Shared Function BBCameraGetRawDataStart(ByVal HBBCamera As
IntPtr, ByRef BBParam As BBCameraParameter) As Integer
End Function

<DllImport("bbappapi.dll")> _
Public Shared Function BBCameraGetRawDataStop(ByVal HBBCamera As
IntPtr) As Integer
End Function

<DllImport("bbappapi.dll")> _
Public Shared Function BBCameraGetRawDataPause(ByVal HBBCamera As
IntPtr, ByRef BBParam1 As BBCameraParameter) As Integer
End Function

<DllImport("bbappapi.dll")> _
Public Shared Function BBCameraGetRawDataResume(ByVal HBBCamera As
IntPtr) As Integer
End Function

<DllImport("bbappapi.dll")> _
Public Shared Function BBCameraCapture(ByVal HBBCamera As IntPtr,
ByRef BBParam1 As BBCameraParameter) As Integer
End Function


' So this is the method that i call to
'
Private Sub StartOperation()

Dim BBParam As BBCameraParameter
Dim StartCamera as boolean

Try
HBBCAMERA =3D BBCameraOpen(picCamera.Handle) 'Get the
handler
StartCamera =3D True
Catch ex As Exception
StartCamera =3D False
End Try

If StartCamera Then
' If camera started then assign some parameters
With BBParam
.preview_width =3D 0
.preview_height =3D 100
.preview_format =3D 0
.ImageQuality =3D 5
.brightness =3D 50
.rotation =3D rotation_degree.rotate_0
.preview_x =3D 0
.preview_y =3D 0
.contrast =3D 200
.saturation =3D 200
.ef_mode =3D effect_mode.effect_none
.wb_mode =3D white_balance_mode.wb_auto
Dim pbuffer() As Byte
ReDim pbuffer(.preview_width * .preview_height * 2)
ReDim .reserved(20)
.p_app_buffer =3D pbuffer
.p_app_capture_buffer =3D pbuffer
End With


TextBox1.Text =3D "Get handle :" & HBBCAMERA.ToString

'GET CAMERA INFO (No problem here, can get the info from
camera)
BBCameraGetInfo(HBBCAMERA, BBInfo)
TextBox1.Text +=3D vbCrLf & "BBInfo.image_max_height:" &
BBInfo.image_max_height
TextBox1.Text +=3D vbCrLf & "BBInfo.image_max_width:" &
BBInfo.image_max_width
TextBox1.Text +=3D vbCrLf & "BBInfo.preview_max_height:" &
BBInfo.preview_max_height
TextBox1.Text +=3D vbCrLf & "BBInfo.preview_max_width:" &
BBInfo.preview_max_width
TextBox1.Text +=3D vbCrLf & "BBInfo.preview_min_width:" &
BBInfo.preview_min_width

Try
'Preview camera image (ERROR OCCURS HERE)
Dim rst As Integer =3D BBCameraPreviewStart(HBBCAMERA,
BBParam)
TextBox1.Text +=3D vbCrLf & "Preview OK"

Catch ex As Exception
TextBox1.Text +=3D vbCrLf & "Preview Failed"

End Try

Else
TextBox1.Text +=3D vbCrLf & "Cannot start camera"
End If
End Sub
----------------------------- 'END OF VB.Net
Codes---------------------

Hope to hear from you guys.

Thanks again!
 
Back
Top