LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Call C++ Dll function from Labview

The Windows Message Queue Library demonstrates how to find the hWnd for a LabVIEW window.  If you search on this forum for hWnd you'll see that individual LabVIEW controls are not windows, so you will probably want to create a separate VI and pass its hWnd to your DLL so that your camera image isn't placed on top of your LabVIEW controls (assuming I'm understanding the mechanism here properly).

 

Unfortunately I can't offer any help with cWnd, that's beyond my level of C++/Windows programming.  A quick search on the web suggests that there may be no way for you to pass a valid cWnd value from LabVIEW because it is a C++ class, but I didn't investigate enough to be certain.

0 Kudos
Message 11 of 19
(2,269 Views)

Thanks for you help. I will try with this example.

 

An other problem I have is : where are stored the datas coming from the USB Webcam ?

 

I mean : the CameraPlay subfunction don't deliver any output argument. The man who sold me the webcam told me that datas are stored in a buffer, but as he is not the programmer, he couldn't help me.

 

I suppose I have to define a kind af pointer with the CameraInit subfunction

 

Name:    CameraInit
Desc:   Initialize video equipment

Param:   pCallbackFunction  Callback fuction pointer,called by SDK,users can add the image analysis in callback fuction.    
        uiResolution  Resolution index
        hWndDisplay      Video display control handle(Set it to NULL,when don't need to display an image )
        lpThreadparam
Return: Call returns a STATUS_OK on success,otherwise returns an error code
Note:   
  --------------------------------------------------------------*/
DT_API DS_CAMERA_STATUS CameraInit(DS_SNAP_PROC pCallbackFunction, IN DS_RESOLUTION uiResolution,
                                   IN HWND hWndDisplay, CWnd* pParent );

 

The type DS_SNAP_PROC is defined as :

 

typedef int (CALLBACK* DS_SNAP_PROC)(BYTE *pImageBuffer, LPVOID lpContext);

 

So I suppose that the datas are maybe stored in the *pImageBuffer. Or maybe they are directly send to the hWndDisplay object ???

 

The callback is defined as :

 

int CALLBACK SnapThreadCallback(BYTE *pBuffer, LPVOID lpContext)
{
#if 0
    int Width = 0;
    int Height= 0;
    if (STATUS_OK != CameraGetImageSize(&Width, &Height))
    {
        return FALSE;    
    }
    for (int i= 100; i<=101; i++)
    {
        for (int j=0; j<Width; j++)
        {
            *(pBuffer+(i*Width+j)*3+0) = 0x00;
            *(pBuffer+(i*Width+j)*3+1) = 0x00;
            *(pBuffer+(i*Width+j)*3+2) = 0xff;
        }
    }
#endif
    return TRUE;
}

At the moment, I initalize the Camera like that. It works in order to call all the other subfunctions, except that I can not see and save the image datas.

CameraInit

Has anybody an idea to help me to get the data of each pixel ?

 

Thank you very mch for your suggestions.

 

Mathieu

0 Kudos
Message 12 of 19
(2,259 Views)

Hello,

 

in fact the library Windows Messaging Queue is too old. Labview 2010 can not open the Vis.

 

Mathieu

0 Kudos
Message 13 of 19
(2,251 Views)

 


@MathieuSG wrote:

in fact the library Windows Messaging Queue is too old. Labview 2010 can not open the Vis.


Look more carefully; the page to which I linked also has the library saved for LV2009, which you can open in 2010.

 

As for retrieving the image data: it looks like you need a pointer to a callback function.  As I wrote previously, there is no way to generate a pointer to a function within LabVIEW.  The only way to deal with this is to write a callback function in C.

 

0 Kudos
Message 14 of 19
(2,241 Views)

Hello,

 

you are right. I didn't see the 2009 version.

 

I met this afternoon a man who knows C++. He explained me different things. As you, his conclusion is that the SnapthreadCallBack returns always the TRUE value in the DEMO code I have.So, his conclusion was that the DS_SNAP_PROC type is useless.

 

I don't understand what you mean about the callback function in C.

 

Do you think I need to modify the SnapthreadCallBack function in C, then Labview calls this function which would return the pBuffer value ? (pBuffer would contains the image datas) ???

 

Mathieu

 

0 Kudos
Message 15 of 19
(2,234 Views)

 


MathieuSG wrote: 

 

I don't understand what you mean about the callback function in C.

 

Do you think I need to modify the SnapthreadCallBack function in C, then Labview calls this function which would return the pBuffer value ? (pBuffer would contains the image datas) ???


 

It's more complicated than that.  LabVIEW does not call SnapthreadCallback.  The camera library calls the SnapThreadCallback function (when there is new data available, I assume).  When you call CameraInit, you pass the address of SnapThreadCallback as a parameter.  That address is a pointer to a function.  Then, when camera receives new data, it calls the function at that memory address.  There is no way for LabVIEW to duplicate this.  You cannot get a pointer to a LabVIEW VI, and you can't call a LabVIEW function by memory address.  You need to have a callback written in C (or some compatible language with identical calling conventions).  You'll also need a way to get the address of that callback function in order to pass it to CameraInit, so you'll either need an additional function written in C that returns the address of the callback, or you need to write a wrapper in C that calls CameraInit.

 

So, you need to write a DLL in C that contains at least two functions: the callback function, and either a function that returns the callback address, or a function that wraps Camera Init.

 

Once you have all that worked out, you then need a way to pass the image data from the callback function to LabVIEW.  There are a number of ways to do this; for example, you could have  declare a variable in your DLL that contains the image data, then add another function that returns that data to LabVIEW.  Another option is to link your DLL against the LabVIEW run-time library and call a user event using PostLVUserEvent containing the data.  All of these options require some level of experience with C, and access to a C compiler.

0 Kudos
Message 16 of 19
(2,226 Views)

Hello,

 

thanks for this answer. I read a lot of things about C code and I will try to do what you said.

 

There is stille something that I don't understand. The callback SnapThreadCallback is not included in the dll provided in the SDK of the camera. I just know that she exists because there is a c++ example to show how to use the SDK. In this example, as I wrote in a previous message, there is a # if 0 so the callback returns always TRUE.

It means that the callback is in fact :

int CALLBACK SnapThreadCallback(BYTE *pBuffer, LPVOID lpContext)

{

return TRUE

}

 

So, the pointer pBuffer is useless, and yet the example application shows the image data.Is this pointer useless or not ?????

 

Excuse me if these questions are maybe stupid, but this callback is the only function where I can see a pointer which looks like image data.

Is it possible that the CameraPlay() subfunction of the dll writes datas somewhere and that I can not see it and where ? (I don't have the source code of the camera dll)

 

Thanks for your answer.

0 Kudos
Message 17 of 19
(2,187 Views)

A callback function is by definition not implemented by the API that uses that callback, but by the software that uses that API. However you can't create a callback function in LabVIEW. This means you have to create an intermediate piece of software (a wrapper DLL) that can provide that callback function and translate the data provided by the API through this callback function in a way that LabVIEW can access. The technically most promissing way would be the aformentioned PostLVUserEvent() LabVIEW API and if you search for this term you will find more than one example of how to use that function.

BUT!!! Those examples are not written for your specific camera nor use case, and require some fundamental understanding about C programming in general and function pointers in special. That understanding does not seem to exist so be prepared to get a steep learning curve. In the end it's you who needs to do this, as there is nobody who will give you a precooked solution.

 

And getting something that seems to work right is only the starting point. Making it something that works reliably under all different possible use cases is an even higher challange and requires even more of an understanding of the low level details of C programming. So if this is for an educational project I encourage you to go ahead and get your feet wet, but if it is for a more production type application, I would recommend you to seek commercial support from someone who knows about this kind of stuff from real experience.

Rolf Kalbermatter
My Blog
0 Kudos
Message 18 of 19
(2,173 Views)

 


MathieuSG wrote: 
So, the pointer pBuffer is useless, and yet the example application shows the image data.Is this pointer useless or not ?????

 

Excuse me if these questions are maybe stupid, but this callback is the only function where I can see a pointer which looks like image data.

Is it possible that the CameraPlay() subfunction of the dll writes datas somewhere and that I can not see it and where ? (I don't have the source code of the camera dll)


As Rolf explained, you need to write the callback function yourself.  In the example pBuffer is not used, but that does not mean it is useless.  It looks to me like they provide example code showing how you would use the callback to modify the image data, but that code is disabled (through use of the #if...#endif) so that if you run the example code it will show you the normal camera image.  I can imagine that otherwise a user might run the example code and wonder why the image does not show up properly.  If you have access to a C compiler you should try building and running the example code.  Then, change "#if 0" to "#if 1" and rebuild so that you can see the effect of modifying the image data array in the callback.

 

0 Kudos
Message 19 of 19
(2,167 Views)