C++ Function Pointer to VB.Net

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • banleong@gmail.com

    #1

    C++ Function Pointer to VB.Net

    To all gurus,

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

    The C++ Codes is as follows :

    =============== == C++ CODE =============== ===
    typedef struct _tagBBCameraPar ameter
    {
    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_na me;

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

    } BBCameraParamet er;

    typedef LPVOID HBBCAMERA;

    BBAPI DWORD WINAPI BBCameraPreview Start(HBBCAMERA ,
    BBCameraParamet er*);
    BBAPI DWORD WINAPI BBCameraPreview Stop(HBBCAMERA) ;

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

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

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


    Public Shared Function BBCameraPreview Start(ByVal HBBCamera As IntPtr,
    ByRef BBParam1 As BBCameraParamet er) As Integer
    End Function

    Public Shared Function BBCameraPreview Stop(ByVal HBBCamera As IntPtr)
    As Integer
    End Function

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

    This is how i pass the parameters;

    Dim BBParam as BBCameraParamet er '// 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).

    BBCameraPreview Start(HBBCAMERA , BBParam)

    How do I call function BBCameraPreview Start correctly? Did i do it
    correctly?

    Please advices.
    Thanks

  • Branco Medeiros

    #2
    Re: C++ Function Pointer to VB.Net

    banleong wrote:
    <snip>
    I am currently converting some of C++ codes to VB.net
    <snip>
    BBAPI DWORD WINAPI BBCameraPreview Start(HBBCAMERA ,
    BBCameraParamet er*);
    BBAPI DWORD WINAPI BBCameraPreview Stop(HBBCAMERA) ;
    >
    =============== == END C++ CODE =============== ===
    >
    I managed to convert most of the codes to VB.Net but when I try to fun
    BBCameraPreview Start function, it gives error.
    >
    '============== ===Partial VB.Net Code
    >
    Public Shared Function BBCameraPreview Start(ByVal HBBCamera As IntPtr,
    ByRef BBParam1 As BBCameraParamet er) As Integer
    End Function
    >
    Public Shared Function BBCameraPreview Stop(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 BBCameraPreview Start Lib "???" ( _
    ByVal HBBCamera As IntPtr,
    ByRef BBParam1 As BBCameraParamet er) As Integer


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


    HTH.

    Regards,

    Branco.

    Comment

    • lord.zoltar@gmail.com

      #3
      Re: C++ Function Pointer to VB.Net

      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?

      Comment

      • banleong@gmail.com

        #4
        Re: C++ Function Pointer to VB.Net

        Branco,

        Yes, it is external libraries.

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

        BBCameraPreview Start(HBBCAMERA , BBParam)

        where HBBCamera is the handler and BBParam is the struction
        declaration of BBCameraParamet er.

        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 BBCameraPreview Start in VB.net code
        based on the C++ codes?

        BBAPI DWORD WINAPI BBCameraPreview Start(HBBCAMERA ,
        BBCameraParamet er*);

        to

        Public Shared Function BBCameraPreview Start(ByVal HBBCamera As IntPtr,
        ByRef BBParam1 As BBCameraParamet er) As Integer
        End Function

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

        Dim BBParam As BBCameraParamet er 'Declare variable to hold
        BBCameraParamet er 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.eff ect_none
        .wb_mode = white_balance_m ode.wb_auto
        Dim pbuffer() As Byte
        ReDim pbuffer(.previe w_width * .preview_height * 2)
        ReDim .reserved(20)
        .p_app_buffer = pbuffer
        .p_app_capture_ buffer = pbuffer

        End With

        'Call function
        BBCameraPreview Start (Handler, BBParam)

        Am i doing the right thing here?

        Thanks and Regards

        Comment

        • Branco Medeiros

          #5
          Re: C++ Function Pointer to VB.Net

          banleong wrote:
          <snip>
          Yes, it is external libraries.
          <snip>
          1) Is my declaration correct for BBCameraPreview Start in VB.net code
          based on the C++ codes?
          >
          BBAPI DWORD WINAPI BBCameraPreview Start(HBBCAMERA ,
          BBCameraParamet er*);
          >
          to
          >
          Public Shared Function BBCameraPreview Start(ByVal HBBCamera As IntPtr,
          ByRef BBParam1 As BBCameraParamet er) 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 BBCameraPreview Start _
          Lib "BBAPI.dll" ( _
          ByVal HBBCamera As IntPtr,
          ByRef BBParam1 As BBCameraParamet er) 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.

          Comment

          • banleong@gmail.com

            #6
            Re: C++ Function Pointer to VB.Net

            On Mar 15, 1:17 pm, "Branco Medeiros" <branco.medei.. .@gmail.com>
            wrote:
            banleong wrote:
            >
            <snip>Yes, it is external libraries.
            <snip>
            1) Is my declaration correct for BBCameraPreview Start in VB.net code
            based on the C++ codes?
            >
            BBAPI DWORD WINAPI BBCameraPreview Start(HBBCAMERA ,
            BBCameraParamet er*);
            >
            to
            >
            Public Shared Function BBCameraPreview Start(ByVal HBBCamera As IntPtr,
            ByRef BBParam1 As BBCameraParamet er) 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 BBCameraPreview Start _
            Lib "BBAPI.dll" ( _
            ByVal HBBCamera As IntPtr,
            ByRef BBParam1 As BBCameraParamet er) 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("bba ppapi.dll")_
            Public Shared Function BBCameraPreview Start(ByVal HBBCamera As
            IntPtr, ByRef BBParam1 As BBCameraParamet er) As Integer
            End Function

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

            Regards

            Comment

            • susiedba@hotmail.com

              #7
              Re: C++ Function Pointer to VB.Net

              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 fucking serious


              -Todos





              On Mar 14, 2:13 am, banle...@gmail. com wrote:
              To all gurus,
              >
              I am currently converting some of C++ codes to VB.net
              >
              The C++ Codes is as follows :
              >
              =============== == C++ CODE =============== ===
              typedef struct _tagBBCameraPar ameter
              {
              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_na me;
              >
              unsigned int contrast;
              unsigned int saturation;
              unsigned int brightness;
              >
              } BBCameraParamet er;
              >
              typedef LPVOID HBBCAMERA;
              >
              BBAPI DWORD WINAPI BBCameraPreview Start(HBBCAMERA ,
              BBCameraParamet er*);
              BBAPI DWORD WINAPI BBCameraPreview Stop(HBBCAMERA) ;
              >
              =============== == END C++ CODE =============== ===
              >
              I managed to convert most of the codes to VB.Net but when I try to fun
              BBCameraPreview Start function, it gives error.
              >
              '============== ===Partial VB.Net Code
              >
              Public Shared Function BBCameraPreview Start(ByVal HBBCamera As IntPtr,
              ByRef BBParam1 As BBCameraParamet er) As Integer
              End Function
              >
              Public Shared Function BBCameraPreview Stop(ByVal HBBCamera As IntPtr)
              As Integer
              End Function
              >
              '=========== END OF PARTIAL VB.NET CODES
              >
              This is how i pass the parameters;
              >
              Dim BBParam as BBCameraParamet er '// 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).
              >
              BBCameraPreview Start(HBBCAMERA , BBParam)
              >
              How do I call function BBCameraPreview Start correctly? Did i do it
              correctly?
              >
              Please advices.
              Thanks

              Comment

              • Branco Medeiros

                #8
                Re: C++ Function Pointer to VB.Net

                banleong wrote:
                <snip>
                Actually i already declare the external function as
                >
                <DllImport("bba ppapi.dll")_
                Public Shared Function BBCameraPreview Start(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 BBCameraParamet er 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.

                Comment

                • Mudhead

                  #9
                  Re: C++ Function Pointer to VB.Net

                  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 BBCameraPreview Start(ByRef HBBCamera As IntPtr,
                  ByRef BBParam1 As BBCameraParamet er) As Integer
                  End Function

                  or

                  Dim HBBCamera as Unit32

                  Public Shared Function BBCameraPreview Start(ByRef HBBCamera As UInt32,
                  ByRef BBParam1 As BBCameraParamet er) As Integer
                  End Function



                  <banleong@gmail .comwrote in message
                  news:1173942127 .379021.128630@ e1g2000hsg.goog legroups.com...
                  On Mar 15, 1:17 pm, "Branco Medeiros" <branco.medei.. .@gmail.com>
                  wrote:
                  >banleong wrote:
                  >>
                  ><snip>Yes, it is external libraries.
                  ><snip>
                  1) Is my declaration correct for BBCameraPreview Start in VB.net code
                  based on the C++ codes?
                  >>
                  BBAPI DWORD WINAPI BBCameraPreview Start(HBBCAMERA ,
                  BBCameraParamet er*);
                  >>
                  to
                  >>
                  Public Shared Function BBCameraPreview Start(ByVal HBBCamera As IntPtr,
                  ByRef BBParam1 As BBCameraParamet er) 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 BBCameraPreview Start _
                  > Lib "BBAPI.dll" ( _
                  > ByVal HBBCamera As IntPtr,
                  > ByRef BBParam1 As BBCameraParamet er) 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("bba ppapi.dll")_
                  Public Shared Function BBCameraPreview Start(ByVal HBBCamera As
                  IntPtr, ByRef BBParam1 As BBCameraParamet er) As Integer
                  End Function
                  >
                  Sorry for the confusion because i didn't include the <DllImportpar t
                  on early post.
                  However, passing the parameters seem to failed.Guess have not choice
                  but go back to manufacturer, eh?
                  >
                  Regards
                  >

                  Comment

                  • banleong@gmail.com

                    #10
                    Re: C++ Function Pointer to VB.Net

                    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
                    "NotSupportedEx ception" 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_UPD ATE_FPS WM_USER+1000

                    #define BB_CAMERA_CODE_ SUCCESS 0
                    #define BB_CAMERA_CODE_ ERR_UNKNOWN 1
                    #define BB_CAMERA_CODE_ ERR_INVALID_HAN DLE 2
                    #define BB_CAMERA_CODE_ ERR_INVALID_PAR AMETER 3

                    typedef enum
                    {
                    wb_auto = 0,
                    wb_cloudy,
                    wb_daylight,
                    wb_fluorescent_ 1,
                    wb_fluorescent_ 2,
                    wb_light_bulb

                    } white_balance_m ode;

                    typedef enum
                    {
                    effect_none = 0,
                    effect_negative ,
                    effect_embossin g,
                    effect_black_n_ white,
                    effect_sketch,
                    effect_solariza tion,
                    effect_sephia,
                    effect_aqua,
                    effect_posteriz e,
                    effect_warm,
                    effect_cool,
                    effect_antique,
                    effect_moonligh t,
                    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_manu al
                    } 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_yc bcr = 0,
                    pixel_format_rg b_565, // output data °¡ bmp ÀÌ´Ù.
                    pixel_format_rg b_565_raw // output data °¡ 16bit rgb µ¥ÀÌÅÍ ÀÌ´Ù.

                    } pixel_format_se lect;


                    typedef struct _tagBBCameraPar ameter
                    {
                    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_na me;
                    unsigned int capture_width;
                    unsigned int capture_height;
                    pixel_format_se lect capture_format;
                    unsigned int capture_strobe_ on;

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

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

                    save_mode stillimage_save mode;
                    save_image_type save_fileType;
                    unsigned int ImageQuality;

                    BOOL isSaveFile;
                    rotation_degree rotation;

                    } BBCameraParamet er;

                    typedef struct _tagBBCameraInf o
                    {
                    unsigned int preview_max_wid th;
                    unsigned int preview_max_hei ght;
                    unsigned int preview_min_wid th;
                    unsigned int preview_min_hei ght;
                    unsigned int image_max_width ;
                    unsigned int image_max_heigh t;

                    unsigned int reserved[20];

                    } BBCameraInfo;


                    typedef LPVOID HBBCAMERA;

                    BBAPI HBBCAMERA WINAPI BBCameraOpen(HW ND);
                    BBAPI DWORD WINAPI BBCameraClose(H BBCAMERA);

                    BBAPI DWORD WINAPI BBCameraPreview Start(HBBCAMERA ,
                    BBCameraParamet er*);
                    BBAPI DWORD WINAPI BBCameraPreview Stop(HBBCAMERA) ;
                    BBAPI DWORD WINAPI BBCameraPreview Pause(HBBCAMERA );
                    BBAPI DWORD WINAPI BBCameraPreview Resume(HBBCAMER A);
                    BBAPI DWORD WINAPI BBCameraPreview Zoom(HBBCAMERA, BBCameraParamet er*);

                    BBAPI DWORD WINAPI BBCameraGetRawD ataStart(HBBCAM ERA,
                    BBCameraParamet er*);
                    BBAPI DWORD WINAPI BBCameraGetRawD ataStop(HBBCAME RA);
                    BBAPI DWORD WINAPI BBCameraGetRawD ataPause(HBBCAM ERA);
                    BBAPI DWORD WINAPI BBCameraGetRawD ataResume(HBBCA MERA);
                    BBAPI DWORD WINAPI BBCameraGetRawD ataZoom(HBBCAME RA,
                    BBCameraParamet er*);

                    BBAPI DWORD WINAPI BBCameraCapture (HBBCAMERA, BBCameraParamet er*);
                    BBAPI DWORD WINAPI BBCameraStoreCa ptureImage(HBBC AMERA,
                    BBCameraParamet er*);
                    BBAPI DWORD WINAPI BBCameraRestart PreviewFromCapt ure(HBBCAMERA,
                    BBCameraParamet er*);

                    BBAPI DWORD WINAPI BBCameraSetWhit eBalance(HBBCAM ERA,
                    BBCameraParamet er*);
                    BBAPI DWORD WINAPI BBCameraSetCont rast(HBBCAMERA, BBCameraParamet er*);
                    BBAPI DWORD WINAPI BBCameraSetSatu ration(HBBCAMER A,
                    BBCameraParamet er*);
                    BBAPI DWORD WINAPI BBCameraSetBrig htness(HBBCAMER A,
                    BBCameraParamet er*);
                    BBAPI DWORD WINAPI BBCameraSetEffe ct(HBBCAMERA, BBCameraParamet er*);
                    BBAPI DWORD WINAPI BBCameraSetFlip (HBBCAMERA, BBCameraParamet er*);
                    BBAPI DWORD WINAPI BBCameraNightMo de(HBBCAMERA, BBCameraParamet er*);

                    BBAPI DWORD WINAPI BBCameraGetInfo (HBBCAMERA, BBCameraInfo*);
                    BBAPI DWORD WINAPI BBCameraDumpReg ister(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_HAN DLE = 2
                    Public Const BB_CAMERA_CODE_ ERR_INVALID_PAR AMETER = 3

                    Enum white_balance_m ode
                    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_embossin g
                    effect_black_n_ white
                    effect_sketch
                    effect_solariza tion
                    effect_sephia
                    effect_aqua
                    effect_posteriz e
                    effect_warm
                    effect_cool
                    effect_antique
                    effect_moonligh t
                    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_manu al
                    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_se lect
                    pixel_format_yc bcr = 0
                    pixel_format_rg b_565 ' // output data °¡ bmp ÀÌ´Ù.
                    pixel_format_rg b_565_raw ' // output data °¡ 16bit rgb µ¥ÀÌÅÍ
                    ÀÌ´Ù.
                    End Enum

                    Structure BBCameraInfo
                    Public preview_max_wid th As System.UInt16
                    Public preview_max_hei ght As System.UInt16
                    Public preview_min_wid th As System.UInt16
                    Public image_max_width As System.UInt16
                    Public image_max_heigh t As System.UInt16
                    End Structure


                    Structure BBCameraParamet er
                    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_na me 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_se lect

                    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_m ode
                    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_b uffer() As Byte

                    Public stillimage_save mode 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 BBCameraParamet er

                    #End Region

                    #Region " BB CAMERA API"
                    <DllImport("bba ppapi.dll")_
                    Public Shared Function BBCameraOpen(By Val HWND As IntPtr)
                    As IntPtr
                    End Function

                    <DllImport("bba ppapi.dll")_
                    Public Shared Function BBCameraClose(B yVal HBBCamera As IntPtr)
                    As Integer
                    End Function

                    <DllImport("bba ppapi.dll")_
                    Public Shared Function BBCameraPreview Start(ByVal HBBCamera As
                    IntPtr, ByRef BBParam1 As BBCameraParamet er) As Integer
                    End Function

                    <DllImport("bba ppapi.dll")_
                    Public Shared Function BBCameraPreview Stop(ByVal HBBCamera As IntPtr)
                    As Integer
                    End Function

                    <DllImport("bba ppapi.dll")_
                    Public Shared Function BBCameraPreview Pause(ByVal HBBCamera As IntPtr)
                    As Integer
                    End Function

                    <DllImport("bba ppapi.dll")_
                    Public Shared Function BBCameraPreview Zoom(ByVal HBBCamera As IntPtr)
                    As Integer
                    End Function

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

                    <DllImport("bba ppapi.dll")_
                    Public Shared Function BBCameraGetRawD ataStart(ByVal HBBCamera As
                    IntPtr, ByRef BBParam As BBCameraParamet er) As Integer
                    End Function

                    <DllImport("bba ppapi.dll")_
                    Public Shared Function BBCameraGetRawD ataStop(ByVal HBBCamera As
                    IntPtr) As Integer
                    End Function

                    <DllImport("bba ppapi.dll")_
                    Public Shared Function BBCameraGetRawD ataPause(ByVal HBBCamera As
                    IntPtr, ByRef BBParam1 As BBCameraParamet er) As Integer
                    End Function

                    <DllImport("bba ppapi.dll")_
                    Public Shared Function BBCameraGetRawD ataResume(ByVal HBBCamera As
                    IntPtr) As Integer
                    End Function

                    <DllImport("bba ppapi.dll")_
                    Public Shared Function BBCameraCapture (ByVal HBBCamera As IntPtr,
                    ByRef BBParam1 As BBCameraParamet er) As Integer
                    End Function
                    #End Region


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

                    Dim BBParam As BBCameraParamet er
                    Dim StartCamera as boolean

                    Try
                    HBBCAMERA = BBCameraOpen(pi cCamera.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.eff ect_none
                    .wb_mode = white_balance_m ode.wb_auto
                    Dim pbuffer() As Byte
                    ReDim pbuffer(.previe w_width * .preview_height * 2)
                    ReDim .reserved(20)
                    .p_app_buffer = pbuffer
                    .p_app_capture_ buffer = pbuffer
                    End With


                    TextBox1.Text = "Get handle :" & HBBCAMERA.ToStr ing

                    'GET CAMERA INFO (No problem here, can get the info from
                    camera)
                    BBCameraGetInfo (HBBCAMERA, BBInfo)
                    TextBox1.Text += vbCrLf & "BBInfo.image_m ax_height:" &
                    BBInfo.image_ma x_height
                    TextBox1.Text += vbCrLf & "BBInfo.image_m ax_width:" &
                    BBInfo.image_ma x_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 = BBCameraPreview Start(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!

                    Comment

                    • Branco Medeiros

                      #11
                      Re: C++ Function Pointer to VB.Net

                      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.Struct Layout( _
                      InterOp.LayoutK ind.Sequential, _
                      Charset:=InterO p.CharSet.Ansi) _
                      Structure BBCameraParamet er
                      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.Marsha lAs(InterOp.Unm anagedType.LPSt r)_
                      Public capture_file_na me As String

                      Public capture_width As UInteger
                      Public capture_height As UInteger
                      Public capture_format As pixel_format_se lect
                      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_m ode
                      Public fp_mode As flip_mode
                      Public fr_mode As frame_rate_mode

                      'Must initialize explicitly
                      <VBFixedArray(R ESERVED_FIELD_M AX)_
                      Public reserved() As UInteger

                      <InterOp.Marsha lAs(InterOp.Unm anagedType.LPAr ray)_
                      Public p_app_buffer() As Byte
                      <InterOp.Marsha lAs(InterOp.Unm anagedType.LPAr ray)_
                      Public p_app_capture_b uffer() As Byte


                      Public stillimage_save mode 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.Struct Layout(InterOp. LayoutKind.Sequ ential)_
                      Structure BBCameraInfo
                      Public preview_max_wid th As UInteger
                      Public preview_max_hei ght As UInteger
                      Public preview_min_wid th As UInteger
                      Public preview_min_hei ght As UInteger
                      Public image_max_width As UInteger
                      Public image_max_heigh t As UInteger

                      'Must initialize explicitly
                      <VBFixedArray(R ESERVED_FIELD_M AX)_
                      Public reserved() As UInteger
                      End Structure

                      <InterOp.DllImp ort("bbappapi.d ll", Charset:=InterO p.CharSet.Ansi) _
                      Public Shared Function BBCameraPreview Start( _
                      ByVal HBBCamera As IntPtr, _
                      ByRef BBParam1 As BBCameraParamet er) As Integer
                      End Function
                      </code>

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

                      Dim Param As New BBCameraParamet er
                      ReDim Param.reserved( RESERVED_FIELD_ MAX)

                      Dim Info As New BBCameraInfo
                      ReDim Info.reserved(R ESERVED_FIELD_M AX)


                      HTH.

                      Regards,

                      Branco.

                      Comment

                      Working...