Jump to content

alloc and free memory in DLLs


Recommended Posts

I got a new problem with a .DLL

The Pototype in the .h-file is like this:

...cut...#ifdef CPP_CALLEXPORT int FKTNPREFIX _getUser(		char* station,				// in		char*& userName,			  // out		int& ErrorCode,		char*& ErrorString);#elseint FKTNPREFIX _getUser_c(		char* station,				/* in */		char** userName,			  /* out */			int* ErrorCode,			char** ErrorString);#endif...cut...In the running .CPP the function call is like this:...cut.... 	  printf("calling _getUser()\n");	  char station[80];	  char* userName;		  ErrorString = (char *)calloc(280,  sizeof( char ));	  userName = (char *)calloc(180,  sizeof( char ));	  printf("station: ");	  scanf("%s", station);		  ret = _getUser(						stationNr,   /* in */						userName,	/* out */						ErrorCode, 						ErrorString); 	  printf("return Value <%d>\n", ret);	  if(ret >= 0)	  {		printf("userName <%s>\n", userName);	  }	  printf("ErrorCode <%d>\n", ErrorCode);	  printf("ErrorString <%s>\n", ErrorString);	  free(userName);	  free(ErrorString);...cut...

How do I handle this calloc and free statement when calling the .DLL from LabView?

Link to comment
That's exactly what I cannot do, because the .DLL is made from a supplier and I do not have an access to the source code.

Should I change the supplier?

5349[/snapback]

would be better, because you have NO chance to call a function in a dll in LV via the call library node, if the function is not exported ...

i can imagine there could be a way with the code interface node, but my skils in C/C++ are very basic, so you better ask an expert if there is a way ...

cheers

CB

Link to comment
  • 4 weeks later...
I got a new problem with a .DLL

        ErrorString = (char *)calloc(280,  sizeof( char ));

  userName = (char *)calloc(180,  sizeof( char ));

  printf("station: ");

  scanf("%s", station);

    ret = _getUser(

      stationNr,  /* in */

      userName,    /* out */

      ErrorCode, 

      ErrorString);

    printf("return Value <%d>\n", ret);

  if(ret >= 0)

  {

    printf("userName <%s>\n", userName);

  }

  printf("ErrorCode <%d>\n", ErrorCode);

  printf("ErrorString <%s>\n", ErrorString);

      free(userName);

  free(ErrorString);

...cut...

How do I handle this calloc and free statement when calling the .DLL from LabView?

5337[/snapback]

Very simple. Create two LabVIEW strings with the correct size. You can use the Initialize Array function to create two arrays of the necessary size of U8 numbers with the value 0. Then use the Byte Array To String function to convert these arrays in a string, place a Call Library Node on the diagram to call your _getUser function and pass the two strings at the appropriate location to that Call Library Node configuring them to be a C String pointer. LabVIEW will take care about passing the correct pointer to the DLL and on return will truncate the string to the actual filled in information up to the terminating 0 byte character.

Basically the calloc is done through the Initialize Array function and the free is done implicitedly by LabVIEW as soon as the string is not anymore used in the diagram. Since the allocation and freeing of the memory is done by the caller (here LabVIEW) there is no need to use a particular memory manager instance. The function will just take whatever memory is provided to it and fill it in with whatever information it has.

Rolf Kalbermatter

Link to comment
  • 1 month later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.