LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

build a .dll in Visual Studio C++ 2008 and use it in labview

Solved!
Go to solution
Highlighted

Hello

 

For a few days I've been trying to create a .dll file with c code and use it with a call library function node in LabView. But I could never do it successfully. I use Visual Studio 2008 and labview 8.6.

 

I had a look at a few tutorials, but they were written for different Visual Studio versions. And because I am a newbie to Visual Studio, it was to difficult for me to do it in an other version than the turtrial is written for.  Though I have lots of experience in programming c for atmel processors(AVR Studio).

 

Is there anybody who could help me step by step trough Visual Studio C++ 2008 and later also help me to setup my call library function node?

 

I'm form Switzerland, so my English may have some mistakes. Sorry for this.

 

 

 What I've done until now:

 

* I opened VisualStudio 2008 and created a new project.

 

* For this i used the template "class library" (because i live in switzerland, i have german versions of visual studio and labview. So probably it's not called exactly "class library" in the English version)

 

* The project is created and I can insert my functions.

 

* but here I have already some difficulties: In my tutorials they say that a function has to be defined like this:

 

type WINAPI name(arguments)

{

    code

}

 

for example

 

unsigned int WINAPI multiply(unsigned char a, unsigned char b)

{

   unsigned int c;

   c = a * b;

   return c;

}

 

 

but when I try to build the project i get the errormessage:

 

syntaxerror: missing ; before 'multiply'

 

* when I delete WINAPI, it creates the dll file

 

* but when i try to open this dll with a "call library function node " in labview, I get a labview-errormessage, that my function multiply can't be found in the dll.

 

 

I would be happy if somebody could help me with this.

0 Kudos
Message 1 of 9
(3,646 Views)
hello Kresley,

you must export the function in order to be able to call it from an external application like LabVIEW.
For your function it would look like this:
__declspec(dllexport) unsigned int __stdcall multiply(unsigned char a, unsigned char b)

Then you should be able to call the function from LabVIEW.

Kind regards,

Robert H
NI germany
0 Kudos
Message 2 of 9
(3,631 Views)

Take a look at this http://digital.ni.com/public.nsf/allkb/BDB89D1B9EEA138E86256CCD005C3AE7

You will find an example here .....\examples\dll\hostname\hostname.vi



Besides which, my opinion is that Express VIs Carthage must be destroyed deleted
(Sorry no Labview "brag list" so far)
0 Kudos
Message 3 of 9
(3,629 Views)

Hello and thank you very much for your answers

 

Right now I tried RobertH's solution. I'm gona try it also with Coq rouge's link.

 

 

To RobertH's way:

 

I inserted the line __declspec(dllexport) unsigned int __stdcall multiply(unsigned char a, unsigned char b);

 

and my file looked like this:

 

#include "stdafx.h"

 

__declspec(dllexport) unsigned int __stdcall multiply(unsigned char a, unsigned char b);

unsigned int multiply(unsigned char a, unsigned char b)

{

unsigned int c;

c = a * b;

return c;

}

 

When I tried to compile, i got an errormessage:

error C2373: 'multiply': Neudefinition; unterschiedliche Modifizierer

 

in English:

error C2373 'multiply': redefinition; different modifier

 

I could fix this error by editing the line

unsigned int multiply(unsigned char a, unsigned char b)

into

unsigned int __stdcall multiply(unsigned char a, unsigned char b)

 

but i have no idea if this is the correct way. Maybe this is wrong? Now i could compile.

I also could open the function multiply in LabView. I created at the call library function node the inputs and outputs. LabView described the functionprototype as:

uint16_t ?multiply@@YGIEE@Z(uint8_t a, uint8_t b);

 

I could start the programm, but I got a LabView errormessage:

 

Error 1097 occurred at Call Library Function Node in extcodetest.vi

 

Possible reason(s):

LabVIEW:  An exception occurred within the external code called by a Call Library Function Node. The exception may have corrupted LabVIEW's memory. Save any work to a new location and restart LabVIEW.

 

I tried both calling conventions, c and stdcall 

 

At least, i could start the LabView Programm. Thats already a littel success. But i think i will need more help.

 

 

Now I'm also gonna try Coq rouge's link.

0 Kudos
Message 4 of 9
(3,615 Views)
Solution
Accepted by topic author Kresley

You need to adorn your declarations with extern "C". LabVIEW only handles C DLLs; it cannot handle C++ DLLs, which is what you'll get if you don't have extern "C". I don't understand why you're trying to use "stdcall". For a regular DLL just stick to C-calls.

 

The link provided by Coq rouge is for Visual C++ 6, which is a very old version. I would suggest looking at this instead, as it deals with Visual Studio 2005.

0 Kudos
Message 5 of 9
(3,608 Views)

Meanwhile i tried Coq rouge's link. Unfortunately Visual Studio 2008 and 6.0 are pretty different. I guess I couldn't manage to du everything correctly. In the End LabView couldn't find my function in the dll. But thank you anyway, Coq rouge, I apprechiate your help.

 

Now i tried the newest post, whitch is a tutorial for visual Studio 2005. This tutorial is smiliar to what I tried before, when I follwoed RobertH's advice. LabView found the function in the dll. But I got also the same errormesage again:

 

Error 1097 occurred at Call Library Function Node in extcodetest.vi

 

Possible reason(s):

LabVIEW:  An exception occurred within the external code called by a Call Library Function Node. The exception may have corrupted LabVIEW's memory. Save any work to a new location and restart LabVIEW.

 

I have a question. What kind of dll exactly do i need? Unfortunately, the project-wizard changed from version to version. Maybe I open a wrong projecttemplate. Visual Studio gives me only the option to create a "class-library" -project (German original name: Klassenbibliothek Projekt). But in the tutorials with the older VS it was always called (dll project). Could it be, that I choose the wrong type of project in the beginning? VS 2008 doesnt offer tho option dll-project anymore. it gives me only the option "class-library"

 

I have really no knowledge about dll-files. So I have to ask: could there be a difference between "dll-project" and "class-library-project"?

 

I also ask, because samercurio says that i have to take care if i have c or c++. "class-library" which i used sounds for me like c++.

0 Kudos
Message 6 of 9
(3,586 Views)

using this link http://zone.ni.com/devzone/cda/tut/p/id/4877 I made a working dll using Visual studio 2005. And I have not made a dll in almost 10 years 😉

But the code do have a bug

 

replace

BOOL WINAPI  DllMain (
            HANDLE    hModule,
            DWORD     dwFunction,
            LPVOID    lpNot)
{
    return TRUE;
}

 

with

BOOL APIENTRY DllMain( HMODULE hModule,

DWORD ul_reason_for_call,

LPVOID lpReserved

)

{

return TRUE;

}

 

Or since you have 2008 use the code that shows up then the wizard is finished

on my pc this showed up

// test4.cpp : Defines the entry point for the DLL application.

//

#include "stdafx.h"

 

#ifdef _MANAGED

#pragma managed(push, off)

#endif

BOOL APIENTRY DllMain( HMODULE hModule,

DWORD ul_reason_for_call,

LPVOID lpReserved

)

{

return TRUE;

}

 



Besides which, my opinion is that Express VIs Carthage must be destroyed deleted
(Sorry no Labview "brag list" so far)
0 Kudos
Message 7 of 9
(3,573 Views)

Juhuiiiii (Swiss-German for Yiipeee) I did it 🙂

 

The problem was, that I made a mistake when using the project creating wizard. Because the wizzard looks a bit different than in 2005 I always took the wrong option. VS2008 offers the option create a Class-LIbrary-Project. That sounded very much like dll for me.

Now with the Tutorial unter http://decibel.ni.com/content/docs/DOC-1690 it worked. Finally I can coose if I want to write my LabView code in text or in graphics.

 

Thanks for all!

 

Cheers, David

0 Kudos
Message 8 of 9
(3,541 Views)

I thought I read a contradiction above, but maybe not.  I have Microsoft Visual Studio 2008 (Professional version), and I have LabIVEW 2009.  Due to absolutely maddening (unsuccessful) results in LabVIEW trying to get MIME-formatted messages built, I'm thinking of using some MVS'08 libraries that hopefully help construct properly formatted MIME (http) messages.  I'm not exactly sure it will do what I need, but before I invest (due to being extremely rusty in C++) time in this, I'm hoping someone can answer a couple of questions for me.

 

(1) Can I build a C++ (not C) DLL, and (2) Can I call a C++ DLL from LabVIEW?

 

 

0 Kudos
Message 9 of 9
(2,873 Views)