11-24-2008 10:37 PM
Requirement : open a file and lock some region of the file and convert the file handle to File stream.
Firstly,
I tried opening the file using a CVI function call "open", tried locking the file using the Windows SDK functions LockFile/LockFileEx and failed because of the file handle returned by "open/OpenFile" is not matching with the Windows SDK functions LockFile/LockFileEx file handle. But I was able to convert the file handle to File stream using fdopen() function.
Secondly,
I tried opening the file using the Windows SDK function "CreateFile" and able to lock the file using Windows SDK functions LockFile/LockFileEx. But when i tried to convert the file handle to File stream using the function fdopen(), I am getting error.
Pl, can any one help me out this problem in achieving the above stated requirement.
11-25-2008 02:55 AM
i am afraid this will be near impossible: windows SDK, CVI library and C runtime are quite different beast. unless you know the internals of the last 2 libraries, there is no way to be certain that nothing will break. and if you find a solution now, there is absolutely no warranty that it will be portable and will not break later, at the next release of the compiler for example.
that's exactly why i always thought the C stdio library is flawed: there is absolutely no way to extend its functionalities. the FILE type is opaque with no documented low-level interface. if you want to add a new file type (like being able to use standard output functions on a com port) you have to write a wrapper around the C library and use this wrapper everywhere instead of the standard calls. (i wrote one once, that i since use every time i envision that i will have to perform i/o on different kind of peripherals).
i don't know if anybody here knows Ada (the programming language), but its streaming library is the best i have ever seen: complete, well-defined (including error codes), user extendable, with a clear separation between formatting and input/output... it is exactly what every streaming library should be.
11-25-2008 04:42 AM
Hi,
I remember that the FILE struct (returned by fopen function) has an integer handle field.
Can you try to use it for the functions you mentioned?
I am also curious about the results 😉
11-25-2008 04:56 AM
to be complete, here is the definition for the FILE structure, straight from "stdio.h":
typedef struct {
struct _mode mode;
struct _state state;
int handle;
unsigned char *buffer;
unsigned char *bufferEnd;
unsigned char *next;
unsigned char *readEnd;
unsigned char *writeEnd;
unsigned char *readEndSave;
unsigned char pushedBack[6];
char numPushedBack;
unsigned char defaultBuf;
unsigned char *tmpname;
void *lock;
int consoleHandle; /* Console handle for host stdio */
} FILE;
WARNING: use at your own risk. how the file is opened is not documented (surely CreateFile, but who knows). absolutely not portable across compilers. since it is declared in "stdio.h" (which is strange given the way the standard library is written), it may not change in the future, but i personally would not count on that...
11-26-2008 02:56 AM
Mr Ramjeev
Hope your problem is solved. Don't hesitate to contact us in case of further issues.
Gaurav
11-26-2008 03:12 AM