LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

DLL Glue Code Compilation Errors

Backstory: For some reason (it says the DLL file is invalid), I am unable to generate DLL glue code/library for an old DLL file in LabWindows 2010.  I did manage to generate the DLL glue code in an older version of LabWindows (4.0) where this particuliar DLL file was originally used in a project.  

 

My dilemma: I am trying to create an object file from the glue code using LabWindows 2010.  However, I ran into some compilation errors  which I attached along with the C soure file of the DLL glue code.  I'm not sure exactly what is wrong with the syntax and I'm looking for some outside help on the topic.

 

Thanks in advance,
Sean

 

There's an error trying to attach the files, so I will just post them here:

 

#include <windows.h>
#include <string.h>     /* strcat, strcpy */

#include <dllsupp.h>    /* FreeIndirectHandle() */

static HINSTANCE DLLHandle = (HINSTANCE) 1;

/* Declare the variables that hold the addresses of the function   */
/* pointers.                                                       */
/* These variables are initialized in order to force the compiler  */
/* to put them in the DATA segment rather than in the BSS segment. */
static HINDIR __static_CloseCommunication = (HINDIR) 1;
static HINDIR __static_GetTimeDate = (HINDIR) 1;
static HINDIR __static_InitCommunication = (HINDIR) 1;
static HINDIR __static_SendVariable = (HINDIR) 1;
static HINDIR __static_GetEmbeddedInformation = (HINDIR) 1;
static HINDIR __static_PTU_MVB_Interface = (HINDIR) 1;



/* local utility function to format the error message */
/* when a function is not found in the DLL */
static void FormatErrorMessage(char *ErrMsgBuf, const char *DLLName, 
                               const char *function) 
{
    strcpy(ErrMsgBuf, "Could Not Find Function: ");
    strcat(ErrMsgBuf, function);
    strcat(ErrMsgBuf, " in ");
    strcat(ErrMsgBuf, DLLName);
}


static FreeAllIndirectHandles(void)
{
   FreeIndirectHandle(__static_CloseCommunication);
   FreeIndirectHandle(__static_GetTimeDate);
   FreeIndirectHandle(__static_InitCommunication);
   FreeIndirectHandle(__static_SendVariable);
   FreeIndirectHandle(__static_GetEmbeddedInformation);
   FreeIndirectHandle(__static_PTU_MVB_Interface);
}


/* Load the DLL and get the addresses of the functions */
unsigned short __Initialize32BitDLLGlue(const char *DLLName, char *ErrMsgBuf)
{
    FARPROC fp;
    char *funcname;

    DLLHandle = LoadLibrary(DLLName);
    if (DLLHandle < HINSTANCE_ERROR) {
        strcpy(ErrMsgBuf, "Failed To Load DLL:");
        strcat(ErrMsgBuf, DLLName);
        return FALSE;
        }

    if (!(fp = GetProcAddress(DLLHandle, 
         "CLOSECOMMUNICATION"))){
        funcname = "CLOSECOMMUNICATION";
        goto FunctionNotFoundError;
        }

    if (!(__static_CloseCommunication = GetIndirectFunctionHandle(fp,
                                                                  INDIR_WORD, 
                                                                  INDIR_ENDLIST)
      ))
        goto IndirectFunctionError;

    if (!(fp = GetProcAddress(DLLHandle, "GETTIMEDATE"))){
        funcname = "GETTIMEDATE";
        goto FunctionNotFoundError;
        }

    if (!(__static_GetTimeDate = GetIndirectFunctionHandle(fp,INDIR_PTR,
                                                           INDIR_PTR,
                                                           INDIR_PTR,
                                                           INDIR_PTR,
                                                           INDIR_PTR,
                                                           INDIR_PTR,
                                                           INDIR_ENDLIST)))
        goto IndirectFunctionError;

    if (!(fp = GetProcAddress(DLLHandle, 
         "INITCOMMUNICATION"))){
        funcname = "INITCOMMUNICATION";
        goto FunctionNotFoundError;
        }

    if (!(__static_InitCommunication = GetIndirectFunctionHandle(fp,
                                                                 INDIR_WORD, 
                                                                 INDIR_PTR,
                                                                 INDIR_WORD, 
                                                                 INDIR_WORD, 
                                                                 INDIR_WORD, 
                                                                 INDIR_WORD, 
                                                                 INDIR_ENDLIST)
      ))
        goto IndirectFunctionError;

    if (!(fp = GetProcAddress(DLLHandle, "SENDVARIABLE"))){
        funcname = "SENDVARIABLE";
        goto FunctionNotFoundError;
        }

    if (!(__static_SendVariable = GetIndirectFunctionHandle(fp,
                                                            INDIR_DWORD, 
                                                            INDIR_DWORD, 
                                                            INDIR_DWORD, INDIR_DWORD, 
                                                            INDIR_ENDLIST)
      ))
        goto IndirectFunctionError;

    if (!(fp = GetProcAddress(DLLHandle, 
         "GETEMBEDDEDINFORMATION"))){
        funcname = "GETEMBEDDEDINFORMATION";
        goto FunctionNotFoundError;
        }

    if (!(__static_GetEmbeddedInformation = GetIndirectFunctionHandle(fp,
                                                                      INDIR_PTR,
                                                                      INDIR_PTR,
                                                                      INDIR_PTR,
                                                                      INDIR_PTR,
                                                                      INDIR_PTR,
                                                                      INDIR_ENDLIST)
      ))
        goto IndirectFunctionError;

    if (!(fp = GetProcAddress(DLLHandle, 
         "PTU_MVB_INTERFACE"))){
        funcname = "PTU_MVB_INTERFACE";
        goto FunctionNotFoundError;
        }

    if (!(__static_PTU_MVB_Interface = GetIndirectFunctionHandle(fp,
                                                                 INDIR_WORD, 
                                                                 INDIR_PTR,
                                                                 INDIR_PTR,
                                                                 INDIR_ENDLIST)
      ))
        goto IndirectFunctionError;

    return DLLHandle;

IndirectFunctionError:
    strcpy(ErrMsgBuf, "Could not allocate Indirect Function Handle");
    FreeAllIndirectHandles();
    FreeLibrary(DLLHandle);
    return FALSE;

FunctionNotFoundError:
    FormatErrorMessage(ErrMsgBuf, DLLName, funcname);
    FreeAllIndirectHandles();
    FreeLibrary(DLLHandle);
    return FALSE;
}



void __Close32BitDLLGlue(HINSTANCE handle)
{
   FreeLibrary(handle);
   FreeAllIndirectHandles();
}



/* Glue Code for each of the DLL functions */



short CloseCommunication(short _arg0)
{
    short retval;
    unsigned short cw387;

    cw387 = Get387CW();
    retval = (short) InvokeIndirectFunction(__static_CloseCommunication, 
                                            _arg0);
    Set387CW(cw387);
    return retval;
}


short GetTimeDate(short *_arg0, short *_arg1, short *_arg2, short *_arg3, 
                  short *_arg4, short *_arg5)
{
    short retval;
    unsigned short cw387;

    cw387 = Get387CW();
    retval = (short) InvokeIndirectFunction(__static_GetTimeDate, _arg0, 
                                            _arg1, _arg2, _arg3, _arg4, 
                                            _arg5);
    Set387CW(cw387);
    return retval;
}


short InitCommunication(short _arg0, char *_arg1, short _arg2, short _arg3, 
                        short _arg4, short _arg5)
{
    short retval;
    unsigned short cw387;

    cw387 = Get387CW();
    retval = (short) InvokeIndirectFunction(__static_InitCommunication, 
                                            _arg0, _arg1, _arg2, _arg3, 
                                            _arg4, _arg5);
    Set387CW(cw387);
    return retval;
}


short SendVariable(int _arg0, int _arg1, double _arg2)
{
    short retval;
    unsigned short cw387;

    cw387 = Get387CW();
    retval = (short) InvokeIndirectFunction(__static_SendVariable, _arg0, 
                                            _arg1, * (((unsigned *) &_arg2
                                            ) + 1), * (unsigned *) &_arg2);
    Set387CW(cw387);
    return retval;
}


short GetEmbeddedInformation(char *_arg0, char *_arg1, char *_arg2, 
                             char *_arg3, float *_arg4)
{
    short retval;
    unsigned short cw387;

    cw387 = Get387CW();
    retval = (short) InvokeIndirectFunction(__static_GetEmbeddedInformation, 
                                            _arg0, _arg1, _arg2, _arg3, 
                                            _arg4);
    Set387CW(cw387);
    return retval;
}


short PTU_MVB_Interface(unsigned short _arg0, unsigned short *_arg1, 
                        unsigned short *_arg2)
{
    short retval;
    unsigned short cw387;

    cw387 = Get387CW();
    retval = (short) InvokeIndirectFunction(__static_PTU_MVB_Interface, 
                                            _arg0, _arg1, _arg2);
    Set387CW(cw387);
    return retval;
}

 

 

0 Kudos
Message 1 of 5
(3,927 Views)

Hi,

    i does not have experience with this old cvi code but it seems to me that some stuff in dllsupp.h is missing or is limited for _NI_mswin16_ compilation.

    if DLL is valid 32bit library maybe it is better to write your own Glue code in "New way" (please see example at end of post)

    if it is 16bit you have big problem.Try google "16bit DLL" or "16bit DLL thunk". But i do not known if this is possible by pure CVI compiler and there is some known problem on new windows(win7_64bit and maybe other)

 

 

Example of new glue style:

    This is example for get two funtions from ws2_32.DLL(Winsock2.h)  from CVI2010 "Generate DLL Import source"

    Just include header file and change function name and type as you need.be sure to have correct call type like __stdcall

 

#include <windows.h>
#include <WinSock2.h>

/* The two macros below are used as error return codes */
/* in case the DLL does not load, or is missing one or */
/* more functions, respectively.  You must define them */
/* to whatever values are meaningful for your DLL.     */
#define kFailedToLoadDLLError     -1
#define kCouldNotFindFunction     -2

static HINSTANCE DLLHandle;

static int (__stdcall *WSAStartup_Ptr)(WORD wVersionRequested,
                                       LPWSADATA lpWSAData);
static int (__stdcall *WSACleanup_Ptr)(void);

/* Load the DLL and get the addresses of the functions */
static int LoadDLLIfNeeded(void)
{
    if (DLLHandle)
        return 0;

    DLLHandle = LoadLibrary("ws2_32.dll");
    if (DLLHandle == NULL) {
        return kFailedToLoadDLLError;
        }
    if (!(WSAStartup_Ptr = (void*) GetProcAddress(DLLHandle, "WSAStartup")))
        goto FunctionNotFoundError;

    if (!(WSACleanup_Ptr = (void*) GetProcAddress(DLLHandle, "WSACleanup")))
        goto FunctionNotFoundError;


    return 0;

FunctionNotFoundError:
    FreeLibrary(DLLHandle);
    DLLHandle = 0;
    return kCouldNotFindFunction;
}


/* Glue Code for each of the DLL functions */
int __stdcall WSAStartup(WORD wVersionRequested, LPWSADATA lpWSAData)
{
    int dllLoadError;

    if (dllLoadError = LoadDLLIfNeeded())
        return dllLoadError;
    return (*WSAStartup_Ptr)(wVersionRequested, lpWSAData);
}


int __stdcall WSACleanup(void)
{
    int dllLoadError;

    if (dllLoadError = LoadDLLIfNeeded())
        return dllLoadError;
    return (*WSACleanup_Ptr)();
}

 


0 Kudos
Message 2 of 5
(3,914 Views)

Hello,

 

What library are you using to reference "LoadDLLIfNeeded" function?

0 Kudos
Message 3 of 5
(3,154 Views)

I need help

I want communicate my micro controller from USB using DLL do you have any sample program,

i want to send and receive bulk data from controller using dll methode

 

 

below is my blocks

 

microcontroller<<-->>>usb<<---->>dll<<---->>application(lab windows cvi)

0 Kudos
Message 4 of 5
(3,008 Views)

1) Your problem is totally and completely unrelated to the topic of this post!

2) You attached to an old thread that is more than 5 years dead!

 

3) You really really need to make more effort to describe your problem! You are on a LabWindows CVI board and the only part in your problem description that is related are the two last words of your post.

We do not know what microcontroller you are using, nor what USB controller implementation that controller has. You don't mention if you have the according DLL and just don't know how to call it from LabWindows CVI, or if you expect us to provide that DLL to you free of charge and fully functional. If it is the last one, you may want to do some reality check first.

Rolf Kalbermatter  My Blog
DEMO, Electronic and Mechanical Support department, room 36.LB00.390
0 Kudos
Message 5 of 5
(2,999 Views)