LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

pointer to instance in dll

I'm writing a dll wrapper for rendering library functions. Some of these functions create pointers to class instances and other functions take those pointers as input. I just need to be able to get the pointer value of an instance from a dll into LabVIEW so that I can pass it to the next dll call. I do not intend to use the pointer in LabVIEW. My understanding is that I can simply pass the pointer by value into LabVIEW as a U32 or I32 (not sure which one), then pass that value into the next dll call. However, I always get 0.

My dll function which should pass the pointer value to LabVIEW looks something like this:
_declspec(dllexport) void function (vtkRenderWindow *renWin)
{
renWin = vtkRenderWindow::New(); //creates new instance of type vtkRenderWindow
}

What am I doing wrong?

Thank you in advance,
Louis Ferreira
0 Kudos
Message 1 of 10
(3,611 Views)


@lou 83 wrote:
I'm writing a dll wrapper for rendering library functions. Some of these functions create pointers to class instances and other functions take those pointers as input. I just need to be able to get the pointer value of an instance from a dll into LabVIEW so that I can pass it to the next dll call. I do not intend to use the pointer in LabVIEW. My understanding is that I can simply pass the pointer by value into LabVIEW as a U32 or I32 (not sure which one), then pass that value into the next dll call. However, I always get 0.

My dll function which should pass the pointer value to LabVIEW looks something like this:
_declspec(dllexport) void function (vtkRenderWindow *renWin)
{
renWin = vtkRenderWindow::New(); //creates new instance of type vtkRenderWindow
}

What am I doing wrong?

Thank you in advance,
Louis Ferreira




You should pass the value as int32 as you have found (I prefer uInt32 for pointers but that is mostly cosmetic) but as a pointer (by reference) instead. A C function can not return a value to the caller in a variable which is not declared and implemented by reference.

Rolf Kalbermatter
Rolf Kalbermatter
My Blog
0 Kudos
Message 2 of 10
(3,609 Views)

@rolfk wrote:

You should pass the value as int32 as you have found (I prefer uInt32 for pointers but that is mostly cosmetic) but as a pointer (by reference) instead. A C function can not return a value to the caller in a variable which is not declared and implemented by reference.

Rolf Kalbermatter




On the second point (that a C function cannot return a value to the caller not declared & implemented by reference)... is the original source code listed above correct? It looks right to me (not that that means anything)--are you saying that it's one of the properties in the LabVIEW window where the DLL options are set to pass either a value or a pointer to a value? Or are you saying that the source code above is incorrect?

I pulled up a random Windows DLL to look at... LabVIEW has the option to allow the arguments to be passed by "Value" or by "Pointer to Value", except for the first argument originally labeled "return type", which must have a standard default since the property does not appear.
0 Kudos
Message 3 of 10
(3,603 Views)


@m3nth wrote:

@rolfk wrote:

You should pass the value as int32 as you have found (I prefer uInt32 for pointers but that is mostly cosmetic) but as a pointer (by reference) instead. A C function can not return a value to the caller in a variable which is not declared and implemented by reference.

Rolf Kalbermatter




On the second point (that a C function cannot return a value to the caller not declared & implemented by reference)... is the original source code listed above correct? It looks right to me (not that that means anything)--are you saying that it's one of the properties in the LabVIEW window where the DLL options are set to pass either a value or a pointer to a value? Or are you saying that the source code above is incorrect?

I pulled up a random Windows DLL to look at... LabVIEW has the option to allow the arguments to be passed by "Value" or by "Pointer to Value", except for the first argument originally labeled "return type", which must have a standard default since the property does not appear.



No the source code is quite fine.
vtkRenderWindow is an object and as such really just a pointer. This pointer is passed by reference (the little star before renWin) in order for the function to be able to return the pointer value to the caller.

If you take instead

_declspec(dllexport) void function (vtkRenderWindow renWin)

then the object pointer is passe by value and while you could reassign new values to this object pointer inside the function the caller would not see this change to the pointer at all (and in the case of object pointers most probably crash terribly after return since the original object is likely gone and replaced by a new object but the caller would still try to access the virtual table and worse its public data of the original object).

The LabVIEW Call Library Node has a configuration option for skalar types, the "Pass" selection box in which you specify if a parameter is to be passed by Value or by Reference. Basically since vtkRenderWindow is already a pointer which you want to represent as an uInt32 as far as LabVIEW is concerned, but you want to be able to have the function return such a pointer to you you also have to make sure that LabVIEW treats this "pointer" accordingly by making LabVIEW pass this pointer by reference. Other functions in the DLL are likely to accept the object as

_declspec(dllexport) void function (vtkRenderWindow renWin)

as long as they are not reallocating such an object and here you would have to make sure to pass the uInt32 by Value to the function.

Rolf Kalbermatter
Rolf Kalbermatter
My Blog
Message 4 of 10
(3,566 Views)
Thank you for your advice. It has cleared up a few things for me. I have tried the "Pointer to Value" option in the call library node, but I still get 0 for my object pointer. A small portion of the dll wrapper that I wrote to access the pointer is again repeated below:

_declspec(dllexport) void function (vtkRenderWindow *renWin)
{
renWin = vtkRenderWindow::New(); //creates new instance of type vtkRenderWindow
}

Originally, the object creation line looked like this:

vtkRenderWindow *renWin = vtkRenderWindow::New();

I moved the declaration into the function prototype so that I could access it from LabVIEW.
I know that renWin is being created properly because the code works. However, I still get 0 for the object pointer and I have tried passing by reference and by value.

Should I be casting it to a uInt32 or something like that?
Is there a C++ command that will return the value of a pointer?

Thank you again,
Louis
0 Kudos
Message 5 of 10
(3,553 Views)

@lou 83 wrote:
Thank you for your advice. It has cleared up a few things for me. I have tried the "Pointer to Value" option in the call library node, but I still get 0 for my object pointer. A small portion of the dll wrapper that I wrote to access the pointer is again repeated below:

_declspec(dllexport) void function (vtkRenderWindow *renWin)
{
renWin = vtkRenderWindow::New(); //creates new instance of type vtkRenderWindow
}

Originally, the object creation line looked like this:

vtkRenderWindow *renWin = vtkRenderWindow::New();

I moved the declaration into the function prototype so that I could access it from LabVIEW.
I know that renWin is being created properly because the code works. However, I still get 0 for the object pointer and I have tried passing by reference and by value.

Should I be casting it to a uInt32 or something like that?
Is there a C++ command that will return the value of a pointer?

Thank you again,
Louis




Make this:


_declspec(dllexport) void function (vtkRenderWindow *renWin)
{
renWin = vtkRenderWindow::New(); //creates new instance of type vtkRenderWindow
}

into this:


_declspec(dllexport) void function (vtkRenderWindow **renWin)
{
*renWin = vtkRenderWindow::New(); //creates new instance of type vtkRenderWindow
}

I assumed that vtkRenderWindow was already the pointer but it seems rather the object class. As explained earlier you can assign a value inside the function directly to a parameter but the caller will never see that value. So you need to create a reference to the value. Since vtkRenderWindow doesn't seem to be the pointer already you have to declare a pointer of the pointer as function parameter to be able to get the pointer back to the caller. In that case I would assume that all other functions will take a vtkRenderWindow *renWin parameter.

Rolf Kalbermatter

Message Edited by rolfk on 06-21-2005 10:49 AM

Rolf Kalbermatter
My Blog
0 Kudos
Message 6 of 10
(3,552 Views)
Thanks for your advice. It worked and it makes sense to me. You were right that the other functions take in a vtkRenderWindow *renWin parameter, however, I'm having trouble with the object methods. Normally I would write something like:

renWin->DoSomething();

But now it doesn't work, and I've tried

*renWin->DoSomething();
and
**renWin->DoSomething();

I keep getting an error C2227: left of '->DoSomething' must point to class/struct/union

Is there anything I can do about this?

Thanks again,
Louis
0 Kudos
Message 7 of 10
(3,532 Views)


@lou 83 wrote:
Thanks for your advice. It worked and it makes sense to me. You were right that the other functions take in a vtkRenderWindow *renWin parameter, however, I'm having trouble with the object methods. Normally I would write something like:

renWin->DoSomething();

But now it doesn't work, and I've tried

*renWin->DoSomething();
and
**renWin->DoSomething();

I keep getting an error C2227: left of '->DoSomething' must point to class/struct/union

Is there anything I can do about this?

Thanks again,
Louis




My C++ is really very limited. Have you tried something like renWin::DoSomething()? Just a wild guess.
Or maybe you declared the function prototypes as extern "C" and your C compiler removes the objectiveness of the vtkRenderWindow parameter too. Then you might try to create an explicit vtkRenderWindow * variable inside the function and assigne the incoming pointer to it. This is all guessing as I have really little knowlege about C++ other than some theoretical understanding.

Rolf Kalbermatter
Rolf Kalbermatter
My Blog
0 Kudos
Message 8 of 10
(3,514 Views)
Hi Lou,

I was just wondering how you went with implementing vtk methods in labVIEW. This was an idea i had been toying with for a while. I finally got myself to start writing a C wrapper yesterday and was successfully able to get one one the simple cone examples work within labview. One thing that Im stuck with is a way to make the vtkRenderWindow a child on the front panle. Im not sure how feasible this is as everywhere I go, tells me otherwise. Did you by any chance find a way around this??

Warm Regards
Sayuj
0 Kudos
Message 9 of 10
(3,316 Views)

"Sayuj" <x@no.email> wrote in message news:1152794411550-390901@exchange.ni.com...
Hi Lou,


I was just wondering how you went with implementing vtk methods in
labVIEW. This was an idea i had been toying with for a while. I finally
got myself to start writing a C wrapper yesterday and was successfully
able to get one one the simple cone examples work within labview. One
thing that Im stuck with is a way to make the vtkRenderWindow a child
on the front panle. Im not sure how feasible this is as everywhere I
go, tells me otherwise. Did you by any chance find a way around this??


Warm Regards
Sayuj


Can you post anything?


What is the problem in making the vtk window a child of the front panel? Can you give the creating code a parent window handler? If so, you can use windows api's to get a handle of the front panel. Or are you trying to embed the window in a front panel? That will be difficult.


Regards,


Wiebe.
0 Kudos
Message 10 of 10
(3,289 Views)