NO

Author Topic: Using a DLL from a DLL  (Read 5991 times)

jev

  • Guest
Using a DLL from a DLL
« on: August 15, 2012, 11:37:49 AM »
I have an application that uses the libMPSSE DLL as delivered by FTDI from which I obviously have no sourcecode (thus I have to use it as-is). So far so  good, this all works with a bit of fiddling around.

Part of the application though consists of a bunch of modules that I'ld like to re-use and distribute as another DLL, let's call it "mylib.dll". The reason for this is that it must be able to call them from a C# program.

The problem is the functions in mylib call functions from libMPSSE. When trying to build mylib, I get errors from POLINK that it cannot resolve the symbols from libMPSSE.

I tried everything I could think of, added a .def file that defines the MPSSE functions, tried linking in the .lib and .a files as provided by FTDI but I can't get this to build a valid DLL.

So in short: does anyone have an example showing how to solve this? Or good documentation on this?


Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2091
Re: Using a DLL from a DLL
« Reply #1 on: August 15, 2012, 04:48:39 PM »
If you generate libMPSSE.lib from this def-file, what happens ?
Code: [Select]
polib.exe /DEF:libMPSSE.def /OUT:libMPSSE.lib /MACHINE:X86
Code: [Select]
LIBRARY libMPSSE.dll
EXPORTS
"Cleanup_libMPSSE"
"FT_ReadGPIO"
"FT_WriteGPIO"
"I2C_CloseChannel"
"I2C_DeviceRead"
"I2C_DeviceWrite"
"I2C_GetChannelInfo"
"I2C_GetNumChannels"
"I2C_InitChannel"
"I2C_OpenChannel"
"Infra_TestFunction@8"
"Init_libMPSSE"
"SPI_ChangeCS"
"SPI_CloseChannel"
"SPI_GetChannelInfo"
"SPI_GetNumChannels"
"SPI_InitChannel"
"SPI_IsBusy"
"SPI_OpenChannel"
"SPI_Read"
"SPI_ReadWrite"
"SPI_Write"
« Last Edit: August 16, 2012, 05:40:29 AM by timovjl »
May the source be with you

jev

  • Guest
Re: Using a DLL from a DLL
« Reply #2 on: August 16, 2012, 10:48:21 AM »
That's exactly what I did. It creates a libMPSSE.lib just as it should, that's not the problem. I can use the resulting .lib in my application where it works just fine.

But I need to use this from my own DLL and there things go awry - the DLL won't build. The individual modules compile but it won't link it into a DLL. I added the .lib file to the DLL project and it is seached by polink, but it results in:
Code: [Select]
Building lrwpanlib.dll.
POLINK: error: Unresolved external symbol '_FT_ReadGPIO@8'.
POLINK: error: Unresolved external symbol '_FT_WriteGPIO@12'.
POLINK: error: Unresolved external symbol '_SPI_GetNumChannels@4'.
POLINK: error: Unresolved external symbol '_SPI_GetChannelInfo@8'.
POLINK: error: Unresolved external symbol '_SPI_OpenChannel@8'.
POLINK: error: Unresolved external symbol '_SPI_InitChannel@8'.
POLINK: error: Unresolved external symbol '_SPI_Write@20'.
POLINK: error: Unresolved external symbol '_SPI_Read@20'.
POLINK: fatal error: 8 unresolved external(s).
*** Error code: 1 ***

In a second step, I tried modifying the FTDI_API macro in libMPSSE.h to  __declspec(dllimport). That obiously results in different symbols to be asked for:

Code: [Select]
Building lrwpanlib.dll.
POLINK: error: Unresolved external symbol 'Cleanup_libMPSSE'.
POLINK: error: Unresolved external symbol 'FT_ReadGPIO'.
POLINK: error: Unresolved external symbol 'FT_WriteGPIO'.
POLINK: error: Unresolved external symbol 'I2C_CloseChannel'.
POLINK: error: Unresolved external symbol 'I2C_DeviceRead'.
POLINK: error: Unresolved external symbol 'I2C_DeviceWrite'.
POLINK: error: Unresolved external symbol 'I2C_GetChannelInfo'.
POLINK: error: Unresolved external symbol 'I2C_GetNumChannels'.
POLINK: error: Unresolved external symbol 'I2C_InitChannel'.
POLINK: error: Unresolved external symbol 'I2C_OpenChannel'.
POLINK: error: Unresolved external symbol 'Init_libMPSSE'.
POLINK: error: Unresolved external symbol 'SPI_ChangeCS'.
POLINK: error: Unresolved external symbol 'SPI_CloseChannel'.
POLINK: error: Unresolved external symbol 'SPI_GetChannelInfo'.
POLINK: error: Unresolved external symbol 'SPI_GetNumChannels'.
POLINK: error: Unresolved external symbol 'SPI_InitChannel'.
POLINK: error: Unresolved external symbol 'SPI_IsBusy'.
POLINK: error: Unresolved external symbol 'SPI_OpenChannel'.
POLINK: error: Unresolved external symbol 'SPI_Read'.
POLINK: error: Unresolved external symbol 'SPI_ReadWrite'.
POLINK: error: Unresolved external symbol 'SPI_Write'.
POLINK: error: Unresolved external symbol '__imp__FT_ReadGPIO@8'.
POLINK: error: Unresolved external symbol '__imp__FT_WriteGPIO@12'.
POLINK: error: Unresolved external symbol '__imp__SPI_GetNumChannels@4'.
POLINK: error: Unresolved external symbol '__imp__SPI_GetChannelInfo@8'.
POLINK: error: Unresolved external symbol '__imp__SPI_OpenChannel@8'.
POLINK: error: Unresolved external symbol '__imp__SPI_InitChannel@8'.
POLINK: error: Unresolved external symbol '__imp__SPI_Write@20'.
POLINK: error: Unresolved external symbol '__imp__SPI_Read@20'.
POLINK: fatal error: 29 unresolved external(s).
*** Error code: 1 ***
Done.
« Last Edit: August 16, 2012, 10:58:47 AM by jev »

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2091
Re: Using a DLL from a DLL
« Reply #3 on: August 16, 2012, 01:15:45 PM »
Quote
Building lrwpanlib.dll.
POLINK: error: Unresolved external symbol '_FT_ReadGPIO@8'.
That means that you are compiling dll with __stdcall calling convention and that libMPSSE.dll maybe using __cdecl calling convention.
If so you can edit libMSSE.h like this:
Code: [Select]
FTDI_API void __cdecl Init_libMPSSE(void);
FTDI_API void __cdecl Cleanup_libMPSSE(void);
...
« Last Edit: August 16, 2012, 02:36:34 PM by timovjl »
May the source be with you

jev

  • Guest
Re: Using a DLL from a DLL
« Reply #4 on: August 16, 2012, 04:59:44 PM »
That did the trick, thanks!

Is it, generally speaking, better to use the stdcall calling convention or cdecl for other languages? It needs to be used in C# which I don't know much from (yet)...  :P
« Last Edit: August 16, 2012, 05:02:44 PM by jev »