Jump to content
Alexander Kocian

DLL with Bundle input crashes

Recommended Posts

The task is to process a bundle by a DLL embedded in LABVIEW. LABVIEW crashes even if the coded is most simple.

For example, I created an array of double, bundled it and plugged it into the DLL.  The parameters of the DLL are "Adapt to Type". The prototype in Labview is "void  pointertest(void *source);"

The C program:

// dllmain.cpp : Defines the entry point for the DLL application.
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include "Header.h"


extern "C" __declspec(dllexport)void pointertest(LVCluster *source);

BOOL APIENTRY DllMain( HMODULE hModule,
                       DWORD  ul_reason_for_call,
                       LPVOID lpReserved
                     )
{
    switch (ul_reason_for_call)
    {
    case DLL_PROCESS_ATTACH:
    case DLL_THREAD_ATTACH:
    case DLL_THREAD_DETACH:
    case DLL_PROCESS_DETACH:
        break;
    }
    return TRUE;
}
void pointertest(LVCluster *source) {
    source->dimSize = 2;
    source->arg[0] = 3.1;
    source->arg[1] = 4.2;
}

 The header: Header.h

#pragma pack(push,1)
typedef struct {
    __int64 dimSize;
    double arg[2];
}LVCluster;
#pragma pack(pop)

The LABVIEW VI is attached.  I checked the foren. However, I cannot figure out what I did wrong. Please give me a hint.

Your support is highly appreciated.

pointertest.vi

Share this post


Link to post
Share on other sites

Have you tried right-clicking on the Call Library Function Node and choosing "Create C File"? That will give you a starting point with the correct function prototype. It appears that since your cluster contains only one element, the cluster is ignored, and you're passing only the array. You can see this by creating a C file from the existing configuration, then wiring the array directly without the bundle and creating a C file - the resulting C prototype is the same. Since you have configured the Call Library Function Node to pass the array parameter as an array data pointer, the value passed to the function is a pointer to the first element of the array, NOT a pointer to the LVCluster structure you've defined. If your function will not resize the array, then this works fine, and you can change your function to (although you might want to add a parameter for the array size):

void pointertest(double *source) {
    source[0] = 3.1;
    source[1] = 4.2;
}

However, if you plan to resize the array within your DLL, then you need a more complex solution. One possibility is to change the Call Library Function Node configuration to pass the parameter as "Handles by Value" so that you can use the LabVIEW memory manager functions such as NumericArrayResize.

Why are you bundling the array into a cluster at all here?

Share this post


Link to post
Share on other sites

Thank you for giving me feedback.  Creating C-File by Call Library Function Node gives

Quote

/* Call Library source file */

#include "extcode.h"

void pointertest(void source[]);

void pointertest(void source[])
{

    /* Insert code here */

}

Therefore Labview also crashed when I change the function prototype from

Quote

void pointertest(LVCluster *source)

to

Quote

void pointertest(double *source)

Once this simple example works, the task will be to bundle a complex combination of data types.

Share this post


Link to post
Share on other sites

Can you attach a ZIP archive containing all the pieces of your project - the code for the shared library, the compiled DLL, and the LabVIEW VIs?

In the Call Library Function Node configuration, try changing the Error Checking level to Maximum, and see if it gives you any helpful error messages.

Share this post


Link to post
Share on other sites

I duplicated your code, including building a DLL, and now I can duplicate the crash as well. My apologies for leading you partly in the wrong direction. I see that despite the generated C file, the array is not getting passed as a simple pointer, unlike what I expected. With some help from this thread, I've confirmed that although Create C File generates different function prototypes depending on how you pass the cluster parameter, LabVIEW in fact still passes the parameter the same way. The following code, based on Create C File with the parameter passed as Handle by Value, works for me (even if when you actually call the function it's configured to pass the parameter as an Array Data Pointer):

#pragma pack(push,1)
typedef struct {
	int dimSize;
	double elt[1];
} TD2;
typedef TD2 **TD2Hdl;

typedef struct {
	TD2Hdl elt1;
} TD1;
#pragma pack(pop)

extern "C" __declspec(dllexport) void pointertest(TD1 *arg1);

void pointertest(TD1 *arg1) {
	(*arg1->elt1)->elt[0] = 3.1;
	(*arg1->elt1)->elt[1] = 4.2;
}

Note that you should not set the dimSize element directly in your C code; if you need to resize the array, use NumericArrayResize as I mentioned before.

Share this post


Link to post
Share on other sites
#pragma pack(push,1)
typedef struct {
	int dimSize;
	double elt[1];
} TD2;
typedef TD2 **TD2Hdl;

typedef struct {
	TD2Hdl elt1;
} TD1;
#pragma pack(pop)

extern "C" __declspec(dllexport) void pointertest(TD1 *arg1);

MgErr pointertest(TD1 *arg1)
{
	if (!arg1->elt1 || (*arg1->elt1)->dimSize < 2)
	   return mgArgErr;
                                 
	(*arg1->elt1)->elt[0] = 3.1;
	(*arg1->elt1)->elt[1] = 4.2;
}

Defensive programming would use at least this extra code. Note the extra test that the handle is not NULL before testing the dimSize, since the array handle itself can be legitimately NULL, if you happen to assign an empty array to it on the diagram

Altneratively you should really make sure to properly resize the array with LabVIEW manager functions before attempting to write into them, just as ned mentioned:

MgErr pointertest(TD1 *arg1)
{
    MgErr err = NumericArrayResize(fD, 1, (UHandle*)&arg1->elt1, 2);
    if (err == noErr)
    {
        (*arg1->elt1)->elt[0] = 3.1;
        (*arg1->elt1)->elt[1] = 4.2;
    }
    return err;
}

 

Edited by rolfk

Share this post


Link to post
Share on other sites

Ned, your method works. Only the declaration int I had to change against  __int64.  thank you. Now, I understand how Labview stores clusters.

rolfk, my VisualC compiler says that the syntax MgErr is unknown.Do I need to consider a special library?

Share this post


Link to post
Share on other sites

MgErr is defined in "extcode.h" found in the "cintools" directory within your LabVIEW installation. When you create a C file from a Call Library Function Node, you'll notice it includes that file - it includes definitions of standard LabVIEW data types to avoid any confusion over things such as the size of an integer.

Share this post


Link to post
Share on other sites
On 4/11/2017 at 10:01 PM, Alexander Kocian said:

Ned, your method works. Only the declaration int I had to change against  __int64.  thank you. Now, I understand how Labview stores clusters.

rolfk, my VisualC compiler says that the syntax MgErr is unknown.Do I need to consider a special library?

Something about the __int64 sounds very wrong!

In fact the definition of the structure should really be like this with the #pragma pack() statements replaced with the correct LabVIEW header files. 

#include "extcode.h"

// Some stuff

#include "lv_prolog.h"
typedef struct {
	int32 dimSize;
	double elt[1];
} TD2;
typedef TD2 **TD2Hdl;

typedef struct {
    TD2Hdl elt[1];
} TD1;
#include "lv_epilog.h"

// Remaining code

This is because on 32-bit LabVIEW for Windows, structures are packed, but on 64-bit LabVIEW for Windows, they are not. The "lv_prolog.h" file sets the correct packing instruction depending on the platform as defined in "platdefines.h" which is included inside "extcode.h".

The __int64 only seems to solve the problem, but by accident. It works by the virtue of LabVIEW only using the lower 32 bits of that number anyway and the fact that x86 CPUs are little endian, so the lower 32-bit of the int64 also happen to be in the same location as the full 32-bit value LabVIEW really expects. But it will go wrong catastrophically if you ever try to compile this code for 32-bit LabVIEW.

And if you call any of the LabVIEW manager function defined in "extcode.h" such as the NumericArrayResize() you will also need to link your project with labview.lib (or labviewv.lib for the 32-bit case) inside the cintools directory. As long as you only use datatypes and macros from "extcode.h", this doesn't apply though.

Edited by rolfk

Share this post


Link to post
Share on other sites

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.