LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How to pass FILE* fp from C to LabView?

I need to pass from one vi to another the FILE pointer.
For example, I have written DLL with functions like:
long openFile( FILE *fp, char* filename, char *rw )
{ fp= fopen(filename, "r" ); return 0; }
long closeFile( FILE *fp ){ fclose(fp); return 0; }
I also have written vi's for these functions using Call Library Function Node. The attempts to use void* and char* to pass FILE* to LabView were not succesful.
How do I pass FILE* between C and LabView and within LabView?
0 Kudos
Message 1 of 5
(3,354 Views)
> I also have written vi's for these functions using Call Library
> Function Node. The attempts to use void* and char* to pass FILE* to
> LabView were not succesful.
> How do I pass FILE* between C and LabView and within LabView?

You have several options. A four byte integer like an Int32 will work,
but it isn't that protected, and if a new user adds two of them
together, that will obviously cause problems.

For a more protected datatype, you can use something like a datalog
refnum that contains an enum with your own private string in it. This
is the same technique used by the GOOP VIs. If you use a more complex
type like this, set the I/O node to adapt to type.

Greg McKaskle
0 Kudos
Message 2 of 5
(3,354 Views)
I'm not sure this is even valid when used within a C program, assume some code like this:

FILE *f = 0;
openFile(f,"happy.txt","rw");
printf("%p\n",f);

"f" will always be "0" even if the fopen works fine. You won't be able to use your "closeFile" because you've lost the address to the valid FILE*. This is because you are passing an address, but you are not changing the value at that address, instead you probably want something like this:

long openFile(File **fp, char *filename, char *rw)
{ *fp=fopen(filename, "r"); return 0; }

and then...

FILE *f;
openFILE(&f, "happy.txt", "r");
printf("%p\n",f);

will work as you expect it to.

Take a look at "extcode.h" in the "cintools" of your LabVIEW install, it has sever functions that will co
nvert strings to paths, paths to refnums, refnums to (internal, undocumented) LabVIEW file descriptors. It will also has functions that you can use to read and write to files opened by LabVIEW.
Message 3 of 5
(3,354 Views)
Thank you David! You pointed to my mistake in the previous example. I guess I am getting rusty on the use of pointers in C. Here is a complete code that works, as you suggested.
// TestConsole.cpp : Win32 Console App. MSVC++
//
#include "stdafx.h"
//
long openFile( FILE **fp, char* filename, char *rw )
{*fp= fopen(filename, rw ); return 0;}
//
long write2File( FILE **fp, char* str ){fprintf(*fp,str); return 0;}
//
long closeFile( FILE **fp ){fclose(*fp); return 0;}
//
int main(){ FILE *fp; char *rw= "w";
openFile( &fp, "hello.txt", rw );
write2File( &fp, "Hi!\n" );
closeFile(&fp);
return 0; } /* main() */
//
Back to LabView. I used LabView's read/write binary files vi's. Then, I needed to read the recorded data with my C-code and I
couldn't. I used HexView program to check the recorded file. It looks like LabView recorded data using Big-Endian notation, while my C-program was expecting Little-Endian. That's the story.
I am going to implement the functions shown above using DLL and Code Interface Node. I'll keep you posted on my progress.
Thanks again!
0 Kudos
Message 4 of 5
(3,354 Views)
No problem. Let me know if you have any other questions.

LabVIEW binary files are created in Big-Endian so that VIs will work across platform, as well as any data that they create.

You might want to change your write2File/closeFile to simply take a FILE *fp since you won't ever want to return a new/different FILE*. Better yet a "const File *fp".

It looks like you are well on your way, good luck!
0 Kudos
Message 5 of 5
(3,354 Views)