1

I'm still not completely sure about how to write correcly a DLL in Visual Studio.

file .h

#ifndef UPLOAD_H_
# define UPLOAD_H_

# ifdef UPLOAD_EXPORT
#  define UPLOAD_API __declspec(dllimport)
# else
#  define UPLOAD_API __declspec(dllexport)
# endif // UPLOAD_EXPORT

#include <Windows.h>
#include <WinInet.h>

extern "C" UPLOAD_API int uploadFTP(
    const char *...,
    const char *...,
    const char *...,
    const char *...,
    const char *...,
    const char *...);

#endif // UPLOAD_H_

file .cpp

#include "upload.h"

extern "C" UPLOAD_API int uploadFTP(
    const char *...,
    const char *...,
    const char *...,
    const char *...,
    const char *...,
    const char *...)
{
  ...
}

This actually works but on MSDN thay write on the .h file:

#ifdef MATHFUNCSDLL_EXPORTS
#define MATHFUNCSDLL_API __declspec(dllexport) 
#else
#define MATHFUNCSDLL_API __declspec(dllimport) 
#endif

That is actually the opposite of what I'm doing, and they do not specify MATHFUNCSDLL_API on the functions of .cpp file.

EDIT:

Solution -> UPLOAD_EXPORT was not correctly defined under Project Properties/C++/Preprocessor

4

1 回答 1

2

The #define statements in MSDN's .h file are the correct ones. You define UPLOAD_EXPORTS when building the DLL, so all UPLOAD_API functions will be declared as dllexport. You don't define it anywhere else, so all clients will see them as dllimport.

PS: You may want to declare your functions WINAPI as well (it means __stdcall) if you wish to use them from languages other than C and C++. Note however that using dllexport instead of a .def file exports the function names with call-convention-specific decorations (leading underscore, etc.)

于 2013-06-18T13:48:10.020 回答