Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Labview Code Interface Reference Manual.pdf
Скачиваний:
35
Добавлен:
29.05.2015
Размер:
1 Мб
Скачать
☆

Chapter 3 CIN Advanced Topics

not need to pass a function number, because GetProcAddress returns the address of the desired function.

This example uses InvokeIndirectFunction to call the desired DLL function, passing the number of the routine that the example calls as the last parameter. With a DLL made by a compiler other than the Watcom C compiler, you don’t need to pass the function number, because GetProcAddress returns the address of the desired function.

Notice that at each stage of calling the DLL, the code checks for errors and returns an error code if it fails.

Notice also that LabVIEW strings are different from C strings. C strings are terminated with a null character. LabVIEW strings are not null-terminated; instead, they begin with a four byte value that indicates the length of the string. Because the DLL expects C strings, this example creates temporary buffers for the C strings using DSNewPtr, and then uses SPrintf to copy the LabVIEW string into the temporary buffers. You might consider modifying the DLL to accept LabVIEW strings instead, because that would require no temporary copies of the strings.

Compiling the CIN

Following is the LabVIEW makefile for this CIN. It assumes that the

.c file is named msgbox.c, the makefile is named msgbox.lvm, and the three pathnames for the directives codeDir, cinToolsDir, and wcDir are set correctly.

name=msgbox

type=CIN

codeDir=c:\labview\examples\cins\dll

cinToolsDir=c:\labview\cintools

wcDir=c:\wc

!include $(cinToolsDir)\generic.mak

The following command line prompt compiles the CIN.

c:> wmake /f msgbox.lvm

LabVIEW Code Interface Reference Manual

3-22

© National Instruments Corporation

Chapter 3 CIN Advanced Topics

Optimization

To optimize the performance of this CIN call LoadLibrary during the CINLoad routine, and call FreeLibrary during the CINUnload routine. This keeps the overhead of loading and unloading the DLL from affecting your run-time performance. The following code shows the modifications you need to make to CINRun, CINLoad, and CINUnload to implement this optimization.

HANDLE hDLL = NULL;

CIN MgErr CINLoad(RsrcFile rf)

{

hDLL = LoadLibrary("msgbxdll.dll"); return noErr;

}

CIN MgErr CINRun(LStrHandle message,

LStrHandle winTitle, int32 *err)

{

 

FARPROC

addr = NULL;

HINDIR

hMessageBox;

int

cb;

char

*messageCStr = NULL,

 

*winTitleCStr = NULL;

MgErr

cinErr = noErr;

*err=0;

if (hDLL < HINSTANCE_ERROR) {

*err = 1;/* LoadLibrary failed */ goto out;

}

addr = GetProcAddress(hDLL,"Win386LibEntry"); if (!addr) {

*err = 2;/* GetProcAddress failed */ goto out;

}

hMessageBox = GetIndirectFunctionHandle(

addr,

INDIR_PTR,

INDIR_PTR,

© National Instruments Corporation

3-23

LabVIEW Code Interface Reference Manual

Chapter 3 CIN Advanced Topics

INDIR_WORD,

INDIR_ENDLIST );

if (!hMessageBox) {

/* GetIndirectFunctionHandle failed */ *err = 3;

goto out;

}

if (!(messageCStr = DSNewPtr(LStrLen(*message)+1))) {

/* mem errs are serious-stop execution */ cinErr=mFullErr;

goto out;

}

if (!(winTitleCStr = DSNewPtr(LStrLen(*winTitle)+1))) {

/* mem errs are serious-stop execution */ cinErr=mFullErr;

goto out;

}

SPrintf(messageCStr, (CStr) "%P", *message);

SPrintf(winTitleCStr, (CStr) "%P", *winTitle);

cb = (WORD)InvokeIndirectFunction( hMessageBox, messageCStr, winTitleCStr,

0x1 );

out:

if (messageCStr) DSDisposePtr(messageCStr);

if (winTitleCStr) DSDisposePtr(winTitleCStr);

return cinErr;

}

CIN MgErr CINUnload(void)

{

LabVIEW Code Interface Reference Manual

3-24

© National Instruments Corporation

Chapter 3 CIN Advanced Topics

if (hDLL) FreeLibrary(hDLL);

return noErr;

}

© National Instruments Corporation

3-25

LabVIEW Code Interface Reference Manual

Chapter 3 CIN Advanced Topics

LabVIEW Code Interface Reference Manual

3-26

© National Instruments Corporation

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]