Whether the function forward x86 dll can only explicit link

ZH Chan 66 Reputation points
2022-09-05T14:23:31.877+00:00

There are two dll, the BaseDll.dll and MyDll.dll
In BaseDll.dll, there is a export funcion.
extern "C" __declspec(dllexport) int MyAdd(int a, int b)
{
return a + b + 1;
}
And in MyDll.dll, i use function forward

pragma comment(linker, "/EXPORT:czhAdd=BaseDll.MyAdd")

In the test exe.
I declare the function
extern "C" __declspec(dllimport) int czhAdd(int a, int b);
And use it.

When complie in x86, it occure error.
Unresolved external symbol __imp__czhAdd.
But complie succeed in x64.

C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,527 questions
0 comments No comments
{count} votes

Accepted answer
  1. RLWA32 40,286 Reputation points
    2022-09-05T15:14:27.767+00:00

    This worked for me in both 32-bit and 64-bit builds-

    BaseDll.dll -

    extern "C" __declspec(dllexport) int MyAdd(int a, int b)  
    {  
        return a + b;  
    }  
    

    MyDll.dll -

    #ifdef _WIN64  
    #pragma comment(linker, "/EXPORT:czhAdd=BaseDll.MyAdd")  
    #else  
    #pragma comment(linker, "/EXPORT:_czhAdd=BaseDll.MyAdd")  
    #endif  
      
    

    Calling .exe -

    extern "C" __declspec(dllimport) int czhAdd(int a, int b);  
      
    int main()  
    {  
    	int x = czhAdd(40, 2);  
    	return x;  
    }  
      
    

0 additional answers

Sort by: Most helpful