<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic How to pass user data to callback function specified in imgSessionAcquire in Machine Vision</title>
    <link>https://forums.ni.com/t5/Machine-Vision/How-to-pass-user-data-to-callback-function-specified-in/m-p/308244#M8564</link>
    <description>I'm trying to acquire images through NI-IMAQ, and am using the
low-level interface. Specifically, I would like to initiate a "grab"
operation and whenever the acquisition is complete,i.e. the buffer is
full/done, the callback function would execute. This callback function
would then be able to process the acquired image.&lt;BR /&gt;
&lt;BR /&gt;
I see from LLgrab.c and the online help that I can configure such a
callback function through "imgSessionAcquire". More notably, the
function declaration for the fall back (as specified in niimq.h) is:&lt;BR /&gt;
&lt;BR /&gt;
typedef&amp;nbsp; uInt32&amp;nbsp;&amp;nbsp; (__cdecl * CALL_BACK_PTR)(SESSION_ID boardid, IMG_ERR err, uInt32 signal, void* data);&lt;BR /&gt;
&lt;BR /&gt;
[for the non-C/C++ programmers out there, it specifies a function
returning a uInt32 (unsigned integer, 32-bit) and takes as arguments a
SESSION_ID (boardid), an IMG_ERR (err) error code, and finally, a
pointer to undefined data (void *data)].&lt;BR /&gt;
&lt;BR /&gt;
It is the last argument that I have the most interest. What exactly is
getting passed as the (void *)data, and can I set this value? I assume,
like many traditional C API's, this parameter was implemented to permit
user to pass user-specific data. This is clearly the intention as seen
in the documentation which describes the function prototype as:&lt;BR /&gt;
&lt;P class="Indent1"&gt;&lt;SPAN class="Monospace"&gt;uInt32(*function)(SESSION_ID sid, IMG_ERR 
err, uInt32 signal, void* userdata)&lt;BR /&gt;
&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class="Indent1"&gt;&lt;SPAN class="Monospace"&gt;How do I go about specifying
the value to use for userdata? I would have thought in
imgSessionAcquire() I can pass in an additional argument, specifically
the value of the (void *)data, but I checked the function prototype in
&amp;lt;niimaq.h&amp;gt; and I don't see any additional parameters there.&lt;BR /&gt;
&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class="Indent1"&gt;&lt;SPAN class="Monospace"&gt;For more background, I'm
using C++ and object-oriented programming, so I need to pass the "this"
pointer in the user data portion in order to access the instance.
Thanks.&lt;BR /&gt;
&lt;/SPAN&gt;&lt;/P&gt;
&lt;BR /&gt;
&lt;DIV&gt;&lt;/DIV&gt;</description>
    <pubDate>Mon, 09 Jan 2006 19:07:21 GMT</pubDate>
    <dc:creator>Harold Bien</dc:creator>
    <dc:date>2006-01-09T19:07:21Z</dc:date>
    <item>
      <title>How to pass user data to callback function specified in imgSessionAcquire</title>
      <link>https://forums.ni.com/t5/Machine-Vision/How-to-pass-user-data-to-callback-function-specified-in/m-p/308244#M8564</link>
      <description>I'm trying to acquire images through NI-IMAQ, and am using the
low-level interface. Specifically, I would like to initiate a "grab"
operation and whenever the acquisition is complete,i.e. the buffer is
full/done, the callback function would execute. This callback function
would then be able to process the acquired image.&lt;BR /&gt;
&lt;BR /&gt;
I see from LLgrab.c and the online help that I can configure such a
callback function through "imgSessionAcquire". More notably, the
function declaration for the fall back (as specified in niimq.h) is:&lt;BR /&gt;
&lt;BR /&gt;
typedef&amp;nbsp; uInt32&amp;nbsp;&amp;nbsp; (__cdecl * CALL_BACK_PTR)(SESSION_ID boardid, IMG_ERR err, uInt32 signal, void* data);&lt;BR /&gt;
&lt;BR /&gt;
[for the non-C/C++ programmers out there, it specifies a function
returning a uInt32 (unsigned integer, 32-bit) and takes as arguments a
SESSION_ID (boardid), an IMG_ERR (err) error code, and finally, a
pointer to undefined data (void *data)].&lt;BR /&gt;
&lt;BR /&gt;
It is the last argument that I have the most interest. What exactly is
getting passed as the (void *)data, and can I set this value? I assume,
like many traditional C API's, this parameter was implemented to permit
user to pass user-specific data. This is clearly the intention as seen
in the documentation which describes the function prototype as:&lt;BR /&gt;
&lt;P class="Indent1"&gt;&lt;SPAN class="Monospace"&gt;uInt32(*function)(SESSION_ID sid, IMG_ERR 
err, uInt32 signal, void* userdata)&lt;BR /&gt;
&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class="Indent1"&gt;&lt;SPAN class="Monospace"&gt;How do I go about specifying
the value to use for userdata? I would have thought in
imgSessionAcquire() I can pass in an additional argument, specifically
the value of the (void *)data, but I checked the function prototype in
&amp;lt;niimaq.h&amp;gt; and I don't see any additional parameters there.&lt;BR /&gt;
&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class="Indent1"&gt;&lt;SPAN class="Monospace"&gt;For more background, I'm
using C++ and object-oriented programming, so I need to pass the "this"
pointer in the user data portion in order to access the instance.
Thanks.&lt;BR /&gt;
&lt;/SPAN&gt;&lt;/P&gt;
&lt;BR /&gt;
&lt;DIV&gt;&lt;/DIV&gt;</description>
      <pubDate>Mon, 09 Jan 2006 19:07:21 GMT</pubDate>
      <guid>https://forums.ni.com/t5/Machine-Vision/How-to-pass-user-data-to-callback-function-specified-in/m-p/308244#M8564</guid>
      <dc:creator>Harold Bien</dc:creator>
      <dc:date>2006-01-09T19:07:21Z</dc:date>
    </item>
    <item>
      <title>Re: How to pass user data to callback function specified in imgSessionAcquire</title>
      <link>https://forums.ni.com/t5/Machine-Vision/How-to-pass-user-data-to-callback-function-specified-in/m-p/308896#M8587</link>
      <description>&lt;P&gt;Hi Harold,&lt;/P&gt;
&lt;P&gt;First, the bad news... The callback on imgSessionAcquire() does not let you pass user data to the callback.&amp;nbsp; &lt;/P&gt;
&lt;P&gt;Now the good news.. You can get the same behavior with imgSessionWaitSignalAsync().&amp;nbsp; Here's some example code.&lt;/P&gt;
&lt;P&gt;//--------------------------------------&lt;/P&gt;
&lt;P&gt;#include "niimaq.h"&lt;/P&gt;
&lt;P&gt;class Foo {&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public:&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; uInt32 DoSomething(SESSION_ID id, IMG_ERR err, uInt32 signal);&lt;BR /&gt;};&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;uInt32 __cdecl Callback(SESSION_ID id, IMG_ERR err, uInt32 signal, void* data) {&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Foo* myObj = reinterpret_cast&amp;lt;Foo*&amp;gt;(data);&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; return myObj-&amp;gt;DoSomething(id, err, signal);&lt;BR /&gt;}&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;main() {&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Foo* myObj = new Foo;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; SESSION_ID id;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; //... Setup grab, do other stuff, then&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; imgSessionAcquire(id, true, NULL);&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; //If you want to get a callback after each image&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; imgSessionWaitSignalAsync(id, IMG_BUF_COMPLETE, IMG_SIGNAL_STATE_RISING, Callback, static_cast&amp;lt;void*&amp;gt;(myObj));&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; //If you want to get a callback when the acquisition is complete&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; imgSessionWaitSignalAsync(id, IMG_AQ_DONE, IMG_SIGNAL_STATE_HIGH, Callback, static_cast&amp;lt;void*&amp;gt;(myObj));&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; //... Eventually clean up, etc.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; return 0;&lt;BR /&gt;}&lt;/P&gt;
&lt;P&gt;//----------------&lt;/P&gt;
&lt;P&gt;Good luck&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 10 Jan 2006 20:36:20 GMT</pubDate>
      <guid>https://forums.ni.com/t5/Machine-Vision/How-to-pass-user-data-to-callback-function-specified-in/m-p/308896#M8587</guid>
      <dc:creator>kellam</dc:creator>
      <dc:date>2006-01-10T20:36:20Z</dc:date>
    </item>
    <item>
      <title>Re: How to pass user data to callback function specified in imgSessionAcquire</title>
      <link>https://forums.ni.com/t5/Machine-Vision/How-to-pass-user-data-to-callback-function-specified-in/m-p/308946#M8591</link>
      <description>Ah, thank you very much! I knew there must be a reason why they left that extra (void *) in there.&lt;BR /&gt;
I just didn't look hard enough. Thanks again!&lt;BR /&gt;
&lt;DIV&gt;&lt;/DIV&gt;</description>
      <pubDate>Tue, 10 Jan 2006 22:29:08 GMT</pubDate>
      <guid>https://forums.ni.com/t5/Machine-Vision/How-to-pass-user-data-to-callback-function-specified-in/m-p/308946#M8591</guid>
      <dc:creator>Harold Bien</dc:creator>
      <dc:date>2006-01-10T22:29:08Z</dc:date>
    </item>
  </channel>
</rss>

