LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

How can I use functions from main-programm in a DLL

Hi,
I have a main programm and a DLL. The functions from the DLL are exported and use from the main program with pointer on functions

Example:

    //
    int    (DLLExports _stdcall *IniFile_ReadfromFileDLL)    (INIFILE, char *);

    //
    Call.IniFile_ReadfromFileDLL = (int (DLLExports _stdcall *)(INIFILE, char *))
                        GetProcAddress((HMODULE)DLLHandle, "IniFile_ReadfromFileDLL");
    if(!Call.IniFile_ReadfromFileDLL)
        return -2;

    //
    if(Call.IniFile_ReadfromFileDLL)
        //
        rw = (*Call.IniFile_ReadfromFileDLL)(Ini_File, pathName);


BUT, how can I use functions from the main programm in the DLL ???

Do I have to make the same or is there an other solution ?

Thanks
Oliver
0 Kudos
Message 1 of 10
(5,260 Views)

Why not put the functions you want to call from both places into the DLL?  You can then call them from the EXE or from the DLL.

Why do you need to use pointers to functions?  If you export them from your DLL and statically link them to your exe, your can call the functions directly and don't need to LoadLibrary and GetProcAddress.

0 Kudos
Message 2 of 10
(5,245 Views)
Also see this FAQ for info on how to use DLLs.

Bilal Durrani
NI
0 Kudos
Message 3 of 10
(5,241 Views)
hello AI S,
we can't link the DLL with a static library because we use an external compiler (Watcom) which is not more supported by CVI. So, it is difficult to create an import library.
Additionnally a static link import library is not so flexibel.

1) For example: If your DLL is missing your program will not start. With LoadLibrary and a dynamically loaded DLL you can decide what to do if the DLL is missing. Your program will run and you can decide to "lock" only some functions which are in the DLL.

2) Also it is possible to save the DLL in a directory and load it over the FilePath. With a static linked DLL you will have more problems.

To the general problem:
First I tried to put the function also to a DLL but this is not running. It is the toolbox.c and the inifile.c from NI. I was not able to put the into a DLL and run them with a program which is compiled with an external compiler.
So my idea was to create a main program, add the functions to this main program and put all the rest of the functions in the DLL and compile them with the external compiler.

I need this external compiler because of performance reasons. Watcom is producing a very efficient code

Greetings
Oliver

0 Kudos
Message 4 of 10
(5,238 Views)
Oliver,

One possible solution would be for your main program to pass to your DLL a function pointer for the function that the DLL needs to call. Your DLL can then call the function via this function pointer.

Luis
0 Kudos
Message 5 of 10
(5,234 Views)
Hello Luis,
if I understand right, I have to export the functions in the main program and the DLL calls them via function pointers ?

That means, I work in the same way like the main program calls the DLL functions. OK.

But isn't there an other way ? How is this normally done when a DLL needs functions from the main program. Is this the only way ?

Oliver
0 Kudos
Message 6 of 10
(5,230 Views)
You can export functions from an application executable just as from a DLL, but you wouldn't normally do that in this case. You would normally pass the function pointers from the main program to the DLL via the parameters of an additional DLL function call. The DLL can then store these pointers as required for later use.
 
I'd strongly suggest making the functions passed in this way _stdcall rather than _cdecl, since it is a pretty safe bet that the Watcom _cdecl calling convention is different to the Microsoft or Borland one.
--
Martin
Certified CVI Developer
0 Kudos
Message 7 of 10
(5,209 Views)
Hello Martin,
that's an interesting idea I never thought about. I will try it.

Yes, I am using the _stdcall for all my functions.

Do you know Watcom ? Are you also a user ?


Thanks for the idea
Oliver
0 Kudos
Message 8 of 10
(5,186 Views)
I've not used Watcom on 32 bit Windows platforms. It had useful 32 bit extender technology in DOS and 16-bit Windows though.
--
Martin
Certified CVI Developer
0 Kudos
Message 9 of 10
(5,169 Views)

Just starting to twiddle with the Open Watcom myself and found this thread interesting, so I did a search at www.openwatcom.org for Calling Convention...

At www.openwatcom.org/index.php/Calling_Conventions i found this, which, I believe, is GOOD news:

= = = begin:  referenced text = = =

Specifying Calling Conventions

The first important fact to realize is that calling conventions are not part of the language standards. Calling conventions are, by nature, platform specific, while C and C++ language standards are not. There are two common de facto standards of designating calling conventions. The traditional way employs type qualifiers; their usage is similar to const or volatile keywords. For example:

extern int __cdecl foo( char * );

In this example, foo is a function taking a pointer to char as an argument and returning int; the function uses the __cdecl calling convention (also known as the C calling convention). Other calling conventions supported by Open Watcom C and C++ compilers are __stdcall, __syscall, __fortran, __pascal, and the default calling convention, __watcall.

The other method of specifying calling convention is using the __declspec type qualifier. The following declaration is equivalent to the one above:

__declspec(__cdecl) extern int foo( char * );

This syntax was first supported in Microsoft C compilers in the early 1990s; most compilers that target Windows support it. Note that the __declspec qualifier has more uses than specifying calling conventions; such uses are not discussed here.

Most of the calling convention specifiers have aliases, for instance _cdecl (for __cdecl) or _System (for __syscall). Use of aliases with no underscores or a single underscore and lowercase letter is discouraged, because such identifiers are not implementation-reserved and may conflict with user code. See the C/C++ User's Guide for further details.

= = = end:  referenced text = = =

This is after "open watcom 1.7" was released in August 2007, I do not know if earlier versions do this had this option.

 

It would also be nice if the "LabVIEW Embedded Development"  and  Watcom would work together, too. Has anyone tried this combination ??

 

 

0 Kudos
Message 10 of 10
(4,781 Views)