LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Can't load a C++ DLL on LabWindowsCVI 9.0

Hi,

I'm making a DLL with Visual Studio C++ 2008 with just the following code:

 

//source.cpp

#include "stdafx.h"

#include "headerfile.h"

  

__declspec(dllexport) int Function(long int Device , char *Arg1, char *Arg2, char *Arg3, char *Arg4, char *Arg5)

{

MessageBox(NULL,(LPCWSTR)L
"Win32 Message",(LPCWSTR)L"Message",MB_OK | MB_ICONEXCLAMATION | MB_TOPMOST); return 42;

}

 

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

#include "stdafx.h"

BOOL APIENTRY DllMain( HMODULE hModule,

DWORD ul_reason_for_call,

LPVOID lpReserved

)

{

MessageBox(NULL,(LPCWSTR)L
"DllMain",(LPCWSTR)L"DLL",MB_OK | MB_ICONEXCLAMATION | MB_TOPMOST);
 switch (ul_reason_for_call)

{

case DLL_PROCESS_ATTACH:

case DLL_THREAD_ATTACH:

case DLL_THREAD_DETACH:

case DLL_PROCESS_DETACH: break;

}

return TRUE;

}

 

If I try to load this DLL with TestStand, the message apears, but if I try to do a GetProcAddress () on LabWindows I get NULL from this function.

 

//LabWindowsCVI

typedef int    (*MYFUNC1)(long int Device , char *Address , char *KeyCode , char *Command , char *Values , char *Response);
.............

int main (int argc, char *argv[])
{

 HINSTANCE  hinstLib;
 MYFUNC1    Funcao1;
......

 hinstLib = LoadLibrary("C:\\Users\\danielcoelho\\Desktop\\CANFiat CANcaseXL CPP\\CANFiatVector\\Debug\\CANFiatVector");
 
 // If the handle is valid, try to get the function address.
 if (hinstLib != NULL)
 {
  Funcao1 = (MYFUNC1) GetProcAddress(hinstLib, "Controlar");
  // If the function address is valid, call the function.
  if (fRunTimeLinkSuccess = (Funcao1 != NULL))
  (Funcao1)(0 , "" , "" , "" , "" , "" );

}

.......

 

I apreciate any help.

Regards,

Daniel Coelho

 

 

 

Daniel Coelho
VISToolkit - http://www.vistoolkit.com - Your Real Virtual Instrument Solution
Controlar - Electronica Industrial e Sistemas, Lda
0 Kudos
Message 1 of 8
(4,247 Views)

At the risk of stating the obvious...

 

Your function name in the dll appears to be "Function", but you are searching for one called "Controlar" with GetProcAddress().

 

Maybe this is just a typo and you have a further problem - if so can you post the actual code?

 

JR

0 Kudos
Message 2 of 8
(4,241 Views)

That's an error due to trying to change the real names and paths.

Anyways, I ziped the files.

These are the files I'm using just to test.

 

Hope it's enough.

Thank you for your time.

Daniel Coelho
VISToolkit - http://www.vistoolkit.com - Your Real Virtual Instrument Solution
Controlar - Electronica Industrial e Sistemas, Lda
Download All
0 Kudos
Message 3 of 8
(4,238 Views)

In your simple.c code, should not the load library command be as follows:

 

    hinstLib = LoadLibrary("C:\\Users\\danielcoelho\\Desktop\\mydll.dll");
 

JR

0 Kudos
Message 4 of 8
(4,225 Views)

I'm sorry, I deleted that path and I forgot to put the dll name again.

It should be like this:

 

hinstLib = LoadLibrary("C:\\Users\\danielcoelho\\Desktop\\dllname"

 

Any more hints?

I apoligize for these mistakes you're finding but thats just a failed atempt to hide private work 😉

 

 

Daniel Coelho
VISToolkit - http://www.vistoolkit.com - Your Real Virtual Instrument Solution
Controlar - Electronica Industrial e Sistemas, Lda
0 Kudos
Message 5 of 8
(4,223 Views)

I believe you're running into name mangling issues.  When VC++ creates a DLL (unless instructed otherwise), it exports the functions with names that are "mangled".  This mangling essentially creates a unique function name that incorporates the number and size of parameters to the function.  This is useful for languages with function overloading (so that software can distinguish between Sum(int x, int y) and Sum(double x, double y)), but is annoying for us plain ole C programmers.

 

Using 'dumpbin /exports CANFiatVector.dll' you can see that your function names aren't what you are expecting:

 ?Controlar@@YAHJPAD0000@Z

 ?GetError@@YAHPAD@Z

 ?Init@@YAJPAD00@Z

 ?Uninit@@YAHJPAD@Z

 ?nCANFiatVector@@3HA 

 

You could change your LoadLibrary call to use the mangled names, or you could follow one of the many online tutorials to create non-mangled DLL exports, such as:

http://www.codeguru.com/forum/showthread.php?t=231254

Hope that helps you get back on track. 

0 Kudos
Message 6 of 8
(4,212 Views)

Thank you for your quick reply.

But, before I go through name mangling issues, I have a quick question.

I had the same exact problem with a CSharp dll when doing the GetProcAddress, but I think that's due to using GetProcAddress to a .NET dll when GetProcAddress should be used for 32 bits dlls. 

Could this VC++ dll have the same issue, or is VC++ dll a 32 bits dll?

Daniel Coelho
VISToolkit - http://www.vistoolkit.com - Your Real Virtual Instrument Solution
Controlar - Electronica Industrial e Sistemas, Lda
0 Kudos
Message 7 of 8
(4,208 Views)

.NET DLLs are totally different than C++, C, or COM DLLs (such as the directX SDK, which you *can* call directly from C -- I've done it with DirectSound in CVI).  I don't know of any way to directly call a .NET DLL from CVI (or any plain C compiler), as the interface to the exported items is quite different due to managed code, etc.

 

VC++ will create either C++, C or COM DLLs, which are really almost all the same structure except for their interfaces (calling conventions, etc).  If you follow the steps that are in that article, you *can* convert your VC++ code to work fine with CVI -- I've done it many times.

0 Kudos
Message 8 of 8
(4,185 Views)