How to use templates in dll?

I have the following code in a dll:

umath.h

#ifndef __UMATH_H__

#define __UMATH_H__

#include <Common.h> //my common file...

namespace nsUMath

{

class __DLL_SPEC_ UMath

{

public:

template<typename T>

static T mod(T x, T y) throw();

private:

UMath(void); //Default constructor.

UMath(const UMath&); //Copy constructor.

UMath& operator=(const UMath&); //Assignment operator.

};

}

//

extern "C"

{

__DLL_SPEC_ double mod(double x, double y) throw();

}

#endif

umath.cpp

#define __COMPILING_DLL_ //Use in common.h to compile to dll.

#include "UMath.h"

//using namespace std;

using namespace nsUMath;

template<typename T>

T UMath::mod(T x, T y) throw()

{

return(x - y * floor(x / y));

}

double UMath::mod<double>(double x, double y) throw();

main.cpp

...

m_hModMath = ::LoadLibraryEx(_t("Mathematics.dll"), NULL, 0);

if (m_hModMath == NULL)

ThrowException(::GetLastError(), __LINE__);

//

//Retrieving the address of the exported functions.

//

m_mod = reinterpret_cast<double (*)(double, double) throw()>

(::GetProcAddress(m_hModMath, _t("mod")));

if (m_mod == NULL)

ThrowException(::GetLastError(), __LINE__);

-

The

dll code compile and link OK; however, the problem is with code in

main.cpp. The GetProcAddress function returns NULL for the address

"mod." Does anybody know what am I doing wrong here.

[3485 byte] By [JuanCarlosTrimi?o] at [2007-12-22]
# 1
The

"mod" function will have an export name that is mangled by C++.

Use Dumpbin /exports on the DLL to find that name. Or use a .DEF

file to rename the export to something more manageable...

nobugz at 2007-8-30 > top of Msdn Tech,Visual C++,Visual C++ General...
# 2

Sorry, there is a typo in the UMath.cpp:

double UMath::mod<double>(double x, double y) throw(); //replace this line with the one below.

template __DLL_SPEC_

double UMath::mod<double>(double x, double y) throw();

JuanCarlosTrimi?o at 2007-8-30 > top of Msdn Tech,Visual C++,Visual C++ General...