Linking error in calling a function exporting from a DLL 
Author Message
 Linking error in calling a function exporting from a DLL

Dear all,

I have some code as following:
===================================================
#include <windows.h>
#include <stdio.h>
#include <winbase.h>
__declspec(dllimport) int GetStructString(int,int,char *);

void main()
{
        int nRet;
        char strTemp[100];
        HINSTANCE hInstDll;

        hInstDll=LoadLibrary("xxxxx.dll");
         nRet=GetStructString(0,0,strTemp);

        if(nRet==1)
           printf("Retrieved data:%s",strTemp);

        if(hInstDll!=NULL) FreeLibrary(hInstDll);

Quote:
}

===================================================
When compiling, something wrong....

--------------------Configuration: SMBiosList - Win32 Release-----------
Linking...
SMBiosList.obj : error LNK2001: unresolved external symbol
__imp__GetStructString
Release/SMBiosList.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.

SMBiosList.exe - 2 error(s), 0 warning(s)

Any idea?
Thanks in advance.

Regards,
Eric Hsu

Sent via Deja.com http://www.*-*-*.com/
Before you buy.



Sun, 23 Mar 2003 12:38:16 GMT  
 Linking error in calling a function exporting from a DLL
I take it that 'GetStructString' is a function in the DLL you're
attempting to load dynamically?

If so then you need to locate the 'GetStructString' function in the
DLL using the GetProcAddress() function.  Then you call it using the
returned pointer.

For example (with error checking omitted):

   typedef int (*PFNGETSTRUCSTRING)(int, int, char*);

   int main()
   {
      int nRet;
      char szTemp[100];
      HINSTANCE hInstDll;

      hInstDll = ::LoadLibrary("xxxxx.dll");
      FARPROC fp = ::GetProcAddress(hInstDll, "GetStructString");
      PFNGETSTRUCTSTRING pfn = (PFNGETSTRUCTSTRING)fp;
      (*fp)(0, 0,strTemp);
      return 0;
   }

Note that if the 'GetStructString' function name is mangled in the DLL
(e.g. not exported using extern "C") then you'll have to adjust the
string name of the function you pass to GetProcAddress appropriately.

Joe O'


Quote:
> Dear all,

> I have some code as following:
> ===================================================
> #include <windows.h>
> #include <stdio.h>
> #include <winbase.h>
> __declspec(dllimport) int GetStructString(int,int,char *);

> void main()
> {
> int nRet;
> char strTemp[100];
> HINSTANCE hInstDll;

> hInstDll=LoadLibrary("xxxxx.dll");
>          nRet=GetStructString(0,0,strTemp);

> if(nRet==1)
>    printf("Retrieved data:%s",strTemp);

> if(hInstDll!=NULL) FreeLibrary(hInstDll);
> }
> ===================================================
> When compiling, something wrong....

> --------------------Configuration: SMBiosList - Win32
Release-----------
> Linking...
> SMBiosList.obj : error LNK2001: unresolved external symbol
> __imp__GetStructString
> Release/SMBiosList.exe : fatal error LNK1120: 1 unresolved externals
> Error executing link.exe.

> SMBiosList.exe - 2 error(s), 0 warning(s)

> Any idea?
> Thanks in advance.

> Regards,
> Eric Hsu

> Sent via Deja.com http://www.deja.com/
> Before you buy.



Sun, 23 Mar 2003 03:00:00 GMT  
 Linking error in calling a function exporting from a DLL

I take it you want to dynamically load the DLL and get the function. You are
missing GetProcAddress step. The way you wrote it you try to link
statically.

typedef  int (CALLBACK *GetStructString_t)(int,int,char *);

hInstDll=LoadLibrary("xxxxx.dll");
GetStructString_t pGetStructString =
    (GetStructString_t)GetProcAddress(hInstDll, "GetStructString");
nRet=pGetStructString(0,0,strTemp);

Notice my use of CALLBACK in function type declaration. It will expand to
__stdcall (of course you should also specify CALLBACK when you implement the
function in the DLL). You should always state the calling convention
explicitly on functions exported from DLLs - you never now what the default
convention is on the compiler one uses to link with your DLL.
--
With best wishes,
    Igor Tandetnik

Quote:

> Dear all,

> I have some code as following:
> ===================================================
> #include <windows.h>
> #include <stdio.h>
> #include <winbase.h>
> __declspec(dllimport) int GetStructString(int,int,char *);

> void main()
> {
> int nRet;
> char strTemp[100];
> HINSTANCE hInstDll;

> hInstDll=LoadLibrary("xxxxx.dll");
>          nRet=GetStructString(0,0,strTemp);

> if(nRet==1)
>    printf("Retrieved data:%s",strTemp);

> if(hInstDll!=NULL) FreeLibrary(hInstDll);
> }
> ===================================================
> When compiling, something wrong....

> --------------------Configuration: SMBiosList - Win32 Release-----------
> Linking...
> SMBiosList.obj : error LNK2001: unresolved external symbol
> __imp__GetStructString
> Release/SMBiosList.exe : fatal error LNK1120: 1 unresolved externals
> Error executing link.exe.

> SMBiosList.exe - 2 error(s), 0 warning(s)

> Any idea?
> Thanks in advance.

> Regards,
> Eric Hsu

> Sent via Deja.com http://www.deja.com/
> Before you buy.



Sun, 23 Mar 2003 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Calling an exported function of a dll which are exported Stdcall calling convention

2. Error calling function exported from a DLL

3. Error calling function exported from a DLL

4. Error calling function exported from a DLL

5. DLL Error - File is linked to missing export BSPTEST.EXE:_EdrCenterTextA@12

6. Linking against exported functions in a DLL

7. Dumpbin, exported Dll Functions and calling them?

8. Getting a DLL to call one of it's own exported functions

9. Help: Need to export VC 5.0 function in DLL to be called from VB 5.0

10. Calling C function exported by dll

11. LINK.EXE linked to missing export MSDIS100.DLL

12. Exporting a C function from a DLL that exports C++ classes

 

 
Powered by phpBB® Forum Software