LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Using GetExternalModuleAddr() to call functions in MSVisual C++

Hi All !

After converting my CVI 5.5.1 project to MS Visual C++ 6.0, I have one last problem.

I try to dynamically load functions from *.obj files using the command GetExternalModuleAddr() and LoadExternalModule().
Unfortunately the GetExternalModuleAddr() always returns an 'Undefined references' error in Visual C.
I compiled all the sources with MSVC but it won't work.
I also tried different calling conventions.

I read the article in the Programmers Reference Manual, but it didn't help a lot !

What is the function GetExternalModuleAddr() doing internally ?
Do I need some extra files to use that functions in MSVC ?
With which compiler do I have to build the *.obj files ?

Any help wou
ld be greatly appreciated

Nose
0 Kudos
Message 1 of 6
(3,825 Views)
Hi Nose (I must say that's a funny name 🙂

In VC++, you can use LoadLibrary to explicitly link to a DLL the way you were doing it with LoadExternalModule(). If you are using a MFC application however, you should use AfxLoadLibrary. MDSN states that AfxLoadLibrary handles thread synchronization before calling LoadLibrary (their prototypes are the same).
Similarly, you can call GetProcAddress instead of GetExternalModuleAddr() to get the pointer to an exported DLL function. GetProcAddress can be called once you obtain the DLL process handle from AfxLoadLibrary.

Best Regards,

Azucena Perez
National Instruments
0 Kudos
Message 2 of 6
(3,825 Views)
Nose,

I just realized that I gave you a method to load a DLL and you need to load a static OBJ file. As far as I'm concerned there is no straightforward way to load/unload OBJ files in MSVC. However since you have CVI 5.5.1, you can simply add the CVI libraries to your VC project and make the same exact function calls you were making in CVI. So here is what you have to do (I tried it and it worked for me):

1. Add the cvirt.lib and cvisupp.lib to your VC project.

2. In your Project Settings, in the Link tab (Category >> Input) make sure you type the name of these two libraries under the Object/Library Modules text box (separate their names with a space, no commas).

3. In the same Link tab, under the Additional Library Path text box, type the full path
to the CVI libraries (i.e., C:\Program Files\National Instruments\MeasurementStudio\CVI\extlib). Adding libraries to your VC project will only work if the CVI compatibility mode is set to VC++.

4.In your *.cpp file, add the following line to include the header file that defines the functions you need:
#include "C:\Program Files\National Instruments\MeasurementStudio\CVI\include\utility.h"

That's it! If for whatever reason this doesn't work, or you'd rather dont' have dependencies to CVI libraries, you could also compile your OBJ code into a DLL (much easier to port) and then use the functions I had mentioned earlier.

Let me know if this doesn't work for you.

Cheers,
Azucena
Message 3 of 6
(3,825 Views)
Hi Azucena,

thanks for your quick help !
I tried the different steps you suggested, but it didn't help. I still get an 'Undefined references' error from the function GetExternalModuleAddr().

Here again a short summary of what I'm doing :

- I've got a MSVC project which creates an application that dynamically loads functions from an file 'config.obj'. That file (config.c) is also part of the MSVC project. Let's assume the function I want to load is called To_be_loaded(). So To_be_loaded() is part of config.c.

- In the source code that loads the functions dynamically from config.obj I included the header you suggested, i.e. a header containing To_be_loaded().

- In my application I use the function LoadExternalModuleEx() to load 'config.obj' whic
h works fine.

- After that I try to get the address of To_be_loaded() using the function GetExternalModuleAddr(To_be_loaded). Now I get the error 'Undefined references'.

I already changed calling conventions without success.

Do you have any ideas what I could try next ?

Bye

Nose
0 Kudos
Message 4 of 6
(3,825 Views)
Hi Nose,

I think you are missing a C file from CVI named "refsym.c". It is located in the extlib folder of your CVI folder. I am attaching a VC project that loads an OBJ file using LoadExternalModule() and then gets the address of a function with GetExternalModuleAddrEx(). You should be able to see all the files I have included in the project as well as some of the project settings.

Hopefully it works for you.

Azucena
0 Kudos
Message 6 of 6
(3,825 Views)
Hi Azucena,

thanks for your quick help !
I tried the different steps you suggested, but it didn't help. I still get an 'Undefined references' error from the function GetExternalModuleAddr().

Here again a short summary of what I'm doing :

- I've got a MSVC project which creates an application that dynamically loads functions from an file 'config.obj'. That file (config.c) is also part of the MSVC project. Let's assume the function I want to load is called To_be_loaded(). So To_be_loaded() is part of config.c.

- In the source code that loads the functions dynamically from config.obj I included the header you suggested, i.e. a header containing To_be_loaded().

- In my application I use the function LoadExternalModuleEx() to load 'config.obj' which works f
ine.

- After that I try to get the address of To_be_loaded() using the function GetExternalModuleAddr(To_be_loaded). Now I get the error 'Undefined references'.

Do you have any ideas what I could try next ?
I already changed calling conventions without success.

Bye

Nose
0 Kudos
Message 5 of 6
(3,825 Views)