LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Write file via DLL

Hi,

I'm trying to write a file on a pocket pc with labview pda module and a selfwritten dll. The file gets created but its still empty after the writing process.
It works when I use the same c-routines in an ordinary c-program. Could there be a permission problem? Any ideas?

Here is my c-code of the dll:
#include <stdio.h>

#define DLLEXPORT    __declspec(dllexport)
#define EXTERNC        extern "C"

FILE *file;

EXTERNC DLLEXPORT void openFile(char* path) {
    filefopen(path, "a+b");    //Append binary data to file
}

EXTERNC DLLEXPORT void closeFile() {
    fclose(file);
}

EXTERNC DLLEXPORT int appendData(double *time, float spectre[], float rms[], unsigned short int *code) {
    int numwritten = 0, error = 0;

    if(file != NULL) {
        numwritten = fwrite(time, sizeof(double), 1, file);
        if(numwritten != 1) error = 1;  

        numwritten = fwrite(spectre, sizeof(float), 51, file);
        if(numwritten != 51) error = 1;   

        numwritten = fwrite(rms, sizeof(float), 16, file);
        if(numwritten != 16) error = 1;   

        numwritten = fwrite(code, sizeof(unsigned short int), 1, file);
        if(numwritten != 1) error = 1;   
    }
    else
        error = 2;                           
   
    return error;
}
0 Kudos
Message 1 of 7
(4,014 Views)
There is a bug in your c-code

filefopen(path, "a+b");

should be

file=fopen(path, "a+b");

Well, okay, that was probably a typo.

What error is returned when you call appendData?

I think you have to pass the file pointer to the appendData function. I recommend that you return the file pointer in the openFile call. Then pass it into the appendData call. You should also pass it to the closeFile function.

EXTERNC DLLEXPORT int openFile(char* path) {
file=fopen(path, "a+b"); //Append binary data to file
return file;
}
EXTERNC DLLEXPORT int appendData(FILE *file, double *time, float spectre[], float rms[], unsigned short int *code) {
int numwritten = 0, error = 0;

if(file != NULL) {
numwritten = fwrite(time, sizeof(double), 1, file);
if(numwritten != 1) error = 1;

numwritten = fwrite(spectre, sizeof(float), 51, file);
if(numwritten != 51) error = 2;

numwritten = fwrite(rms, sizeof(float), 16, file);
if(numwritten != 16) error = 3;

numwritten = fwrite(code, sizeof(unsigned short int), 1, file);
if(numwritten != 1) error = 4;
}
else
error = -1;

return error;
jc
Mac 10.4
LV7.1
CLD
0 Kudos
Message 2 of 7
(4,005 Views)
I forgot one thing. You need to cast the pointer to int before you return it out of the openFile function, i.e.

return (int)file;
jc
Mac 10.4
LV7.1
CLD
0 Kudos
Message 3 of 7
(4,000 Views)
Since file is declared globally, it does look like your dll's code should work the way it is.

You said it works from your c-code test routine. Is the c-code test routine functionally the same as your LV code? Maybe you should include your LV code. It could be something obvious to the uninitiated eye.
jc
Mac 10.4
LV7.1
CLD
0 Kudos
Message 4 of 7
(3,997 Views)
It came to work a few days ago.

Here's my latest code:

#include <stdio.h>

#define DLLEXPORT    __declspec(dllexport)
#define EXTERNC        extern "C"

FILE *fileMesseinstellungen, *fileMessdaten[3];


/******************************************
  Die nötigen Dateistreams werden erstellt
******************************************/
EXTERNC DLLEXPORT void openFiles(int AnzahlPhasen, char* pathMesseinstellungen,
                                 char* pathMessdatenPh1, char* pathMessdatenPh2, char* pathMessdatenPh3) {
    if(AnzahlPhasen == 3) {
        fileMessdaten[0] = fopen(pathMessdatenPh1, "a+b");            //Die Messdateien für die einzelnen Phasen
        fileMessdaten[1] = fopen(pathMessdatenPh2, "a+b");            //werden erstellt und geöffnet
        fileMessdaten[2] = fopen(pathMessdatenPh3, "a+b");
    }
    else {
        fileMessdaten[0] = fopen(pathMessdatenPh1, "a+b");
    }

    fileMesseinstellungen = fopen(pathMesseinstellungen, "r+");        //Die Messeinstellungen werden geöffnet
}


/**************************************
  Alle Dateistreams werden geschlossen
**************************************/
EXTERNC DLLEXPORT void closeFiles() {
    fclose(fileMesseinstellungen);                                   
    fclose(fileMessdaten[0]);
    fclose(fileMessdaten[1]);
    fclose(fileMessdaten[2]);
}


/***********************************
  Ändert die Byteorder eines Double
***********************************/
void ChangeEndianDouble(double *val)                               
{
    int i,j;
    double temp;
    char *tempPtr = NULL;
    char *valPtr = NULL;

    temp = *val;
    tempPtr = (char *)&temp;
    valPtr = (char *)val;

    for (i=0, j=7; i<8; i++, j--)
    valPtr[i] = tempPtr[j];
}


/**********************************
  Ändert die Byteorder eines Float
**********************************/
void ChangeEndianSingle(float *val)                                   
{
    int i,j;
    float temp;
    char *tempPtr = NULL;
    char *valPtr = NULL;

    temp = *val;
    tempPtr = (char *)&temp;
    valPtr = (char *)val;

    for (i=0, j=3; i<4; i++, j--)
    valPtr[i] = tempPtr[j];
}


/************************************
  Ändert die Byteorder eines Integer
************************************/
void ChangeEndianInt(int *val)                                       
{
    int i,j;
    int temp;
    char *tempPtr = NULL;
    char *valPtr = NULL;

    temp = *val;
    tempPtr = (char *)&temp;
    valPtr = (char *)val;

    for (i=0, j=3; i<4; i++, j--)
    valPtr[i] = tempPtr[j];
}


/***************************************************
  Ändert die Byteorder eines Unsigned Short Integer
***************************************************/
void ChangeEndianU16Int(unsigned short int *val)                   
{
    int i,j;
    int temp;
    char *tempPtr = NULL;
    char *valPtr = NULL;

    temp = *val;
    tempPtr = (char *)&temp;
    valPtr = (char *)val;

    for (i=0, j=1; i<2; i++, j--)
    valPtr[i] = tempPtr[j];
}


/****************************************
  Ändert die Byteorder eines Float-Array
****************************************/
void ChangeEndianSingleArray(int arraySize, float *array)           
{
    int i;

    for (i=0; i<arraySize; i++)
        ChangeEndianSingle(&array[i]);
}


/**************************
  Schreiben der Messdatei
**************************/
EXTERNC DLLEXPORT int appendMessblock(int phase, double *zeitstempel, float spektrum[], float pegelverlauf[], unsigned short int *code) {
    int numwritten = 0, error = 0;


    if(fileMessdaten) {                                               
        ChangeEndianDouble(zeitstempel);
        numwritten = fwrite(zeitstempel, sizeof(double), 1, fileMessdaten[phase-1]);
        if(numwritten != 1) error = 1;    //Fehler beim Schreiben aufgetreten

        ChangeEndianSingleArray(51, spektrum);
        numwritten = fwrite(spektrum, sizeof(float), 51, fileMessdaten[phase-1]);
        if(numwritten != 51) error = 1;    //Fehler beim Schreiben aufgetreten

        ChangeEndianSingleArray(16, pegelverlauf);
        numwritten = fwrite(pegelverlauf, sizeof(float), 16, fileMessdaten[phase-1]);
        if(numwritten != 16) error = 1;    //Fehler beim Schreiben aufgetreten

        ChangeEndianU16Int(code);
        numwritten = fwrite(code, sizeof(unsigned short int), 1, fileMessdaten[phase-1]);
        if(numwritten != 1) error = 1;    //Fehler beim Schreiben aufgetreten
    }
    else
        error = 2;                        //Fehler beim Zugriff aufgetreten
   
    return error;
}


/********************************************************
 Schreiben der Anzahl Messungen in die Messeinstellungen
********************************************************/
EXTERNC DLLEXPORT int writeAnzahlMessungen(int *anzahl, int offset) {
    int numwritten = 0, error = 0;
   
    if(fileMesseinstellungen) {
        fseek(fileMesseinstellungen, offset, SEEK_SET); // Filestream-Position setzen

        ChangeEndianInt(anzahl);
        numwritten = fwrite(anzahl, sizeof(int), 1, fileMesseinstellungen);
        if(numwritten != 1) error = 1;                    //Fehler beim Schreiben aufgetreten
    }
    else
        error = 2;                                        //Fehler beim Zugriff aufgetreten

    return error;
}


Happy Christmas
Stefan
0 Kudos
Message 5 of 7
(3,948 Views)
I am glad to hear that your code is working, now. I reviewed your code, but I could not determine which change fixed your problem. Please, if you will, let me know what was the source of the problem?

Happy Christmass to you, too.
jc
Mac 10.4
LV7.1
CLD
0 Kudos
Message 6 of 7
(3,943 Views)
Somehow my call of the functions was wrong. I had a problem with the parameters in the call library node.
I send you my LabView Code as well.

Greetings
Stefan
0 Kudos
Message 7 of 7
(3,933 Views)