diff --git a/lib/idna.h b/lib/idna.h
index f6b24ac..7c5333f 100644
--- a/lib/idna.h
+++ b/lib/idna.h
@@ -22,6 +22,20 @@
 #ifndef _IDNA_H
 # define _IDNA_H
 
+/* libidn Windows DLL.  Only needed when this file is used in Visual
+   Studio.  Export and import happens automatically in MinGW. */
+#ifndef LIBIDN_API
+# if defined(_MSC_VER) && !defined(LIBIDN_STATIC)
+#  ifdef LIBIDN_EXPORTS
+#   define LIBIDN_API  __declspec(dllexport)
+#  else /* LIBIDN_EXPORTS */
+#   define LIBIDN_API  __declspec(dllimport)
+#  endif /* LIBIDN_EXPORTS */
+# else /* _WIN32 && !LIBIDN_STATIC */
+#  define LIBIDN_API extern
+# endif /* _WIN32 && !LIBIDN_STATIC */
+#endif /* LIBIDN_API */
+
 # ifdef __cplusplus
 extern "C"
 {
@@ -61,37 +75,37 @@ extern "C"
 #  define IDNA_ACE_PREFIX "xn--"
 # endif
 
-  extern const char *idna_strerror (Idna_rc rc);
+  LIBIDN_API const char *idna_strerror (Idna_rc rc);
 
   /* Core functions */
-  extern int idna_to_ascii_4i (const uint32_t * in, size_t inlen,
+  LIBIDN_API int idna_to_ascii_4i (const uint32_t * in, size_t inlen,
 			       char *out, int flags);
-  extern int idna_to_unicode_44i (const uint32_t * in, size_t inlen,
+  LIBIDN_API int idna_to_unicode_44i (const uint32_t * in, size_t inlen,
 				  uint32_t * out, size_t * outlen, int flags);
 
   /* Wrappers that handle several labels */
 
-  extern int idna_to_ascii_4z (const uint32_t * input,
+  LIBIDN_API int idna_to_ascii_4z (const uint32_t * input,
 			       char **output, int flags);
 
-  extern int idna_to_ascii_8z (const char *input, char **output, int flags);
+  LIBIDN_API int idna_to_ascii_8z (const char *input, char **output, int flags);
 
-  extern int idna_to_ascii_lz (const char *input, char **output, int flags);
+  LIBIDN_API int idna_to_ascii_lz (const char *input, char **output, int flags);
 
 
-  extern int idna_to_unicode_4z4z (const uint32_t * input,
+  LIBIDN_API int idna_to_unicode_4z4z (const uint32_t * input,
 				   uint32_t ** output, int flags);
 
-  extern int idna_to_unicode_8z4z (const char *input,
+  LIBIDN_API int idna_to_unicode_8z4z (const char *input,
 				   uint32_t ** output, int flags);
 
-  extern int idna_to_unicode_8z8z (const char *input,
+  LIBIDN_API int idna_to_unicode_8z8z (const char *input,
 				   char **output, int flags);
 
-  extern int idna_to_unicode_8zlz (const char *input,
+  LIBIDN_API int idna_to_unicode_8zlz (const char *input,
 				   char **output, int flags);
 
-  extern int idna_to_unicode_lzlz (const char *input,
+  LIBIDN_API int idna_to_unicode_lzlz (const char *input,
 				   char **output, int flags);
 
 # ifdef __cplusplus
diff --git a/lib/stringprep.h.in b/lib/stringprep.h.in
index 4dafb90..a6ce894 100644
--- a/lib/stringprep.h.in
+++ b/lib/stringprep.h.in
@@ -31,15 +31,19 @@ extern "C"
 #include <unistd.h>		/* ssize_t */
 #include <idn-int.h>		/* uint32_t */
 
-  /* On Windows, variables that may be in a DLL must be marked
-   * specially.  This is only active when not building libidn itself
-   * (!LIBIDN_BUILDING).  It is only used for MinGW which declare
-   * __DECLSPEC_SUPPORTED or MSVC (_MSC_VER && _DLL). */
-#if !defined (LIBIDN_BUILDING) && (defined(__DECLSPEC_SUPPORTED) || (defined(_MSC_VER) && defined(_DLL)))
-# define IDN_DLL_VAR __declspec (dllimport)
-#else
-# define IDN_DLL_VAR
-#endif
+/* libidn Windows DLL.  Only needed when this file is used in Visual
+   Studio.  Export and import happens automatically in MinGW. */
+#ifndef LIBIDN_API
+# if defined(_MSC_VER) && !defined(LIBIDN_STATIC)
+#  ifdef LIBIDN_EXPORTS
+#   define LIBIDN_API  __declspec(dllexport)
+#  else /* LIBIDN_EXPORTS */
+#   define LIBIDN_API  __declspec(dllimport)
+#  endif /* LIBIDN_EXPORTS */
+# else /* _WIN32 && !LIBIDN_STATIC */
+#  define LIBIDN_API extern
+# endif /* _WIN32 && !LIBIDN_STATIC */
+#endif /* LIBIDN_API */
 
 #define STRINGPREP_VERSION "@PACKAGE_VERSION@"
 
@@ -109,30 +113,30 @@ extern "C"
   };
   typedef struct Stringprep_profiles Stringprep_profiles;
 
-  extern IDN_DLL_VAR const Stringprep_profiles stringprep_profiles[];
+  LIBIDN_API const Stringprep_profiles stringprep_profiles[];
 
 /* Profiles */
-  extern IDN_DLL_VAR const Stringprep_table_element stringprep_rfc3454_A_1[];
-  extern IDN_DLL_VAR const Stringprep_table_element stringprep_rfc3454_B_1[];
-  extern IDN_DLL_VAR const Stringprep_table_element stringprep_rfc3454_B_2[];
-  extern IDN_DLL_VAR const Stringprep_table_element stringprep_rfc3454_B_3[];
-  extern IDN_DLL_VAR const Stringprep_table_element stringprep_rfc3454_C_1_1[];
-  extern IDN_DLL_VAR const Stringprep_table_element stringprep_rfc3454_C_1_2[];
-  extern IDN_DLL_VAR const Stringprep_table_element stringprep_rfc3454_C_2_1[];
-  extern IDN_DLL_VAR const Stringprep_table_element stringprep_rfc3454_C_2_2[];
-  extern IDN_DLL_VAR const Stringprep_table_element stringprep_rfc3454_C_3[];
-  extern IDN_DLL_VAR const Stringprep_table_element stringprep_rfc3454_C_4[];
-  extern IDN_DLL_VAR const Stringprep_table_element stringprep_rfc3454_C_5[];
-  extern IDN_DLL_VAR const Stringprep_table_element stringprep_rfc3454_C_6[];
-  extern IDN_DLL_VAR const Stringprep_table_element stringprep_rfc3454_C_7[];
-  extern IDN_DLL_VAR const Stringprep_table_element stringprep_rfc3454_C_8[];
-  extern IDN_DLL_VAR const Stringprep_table_element stringprep_rfc3454_C_9[];
-  extern IDN_DLL_VAR const Stringprep_table_element stringprep_rfc3454_D_1[];
-  extern IDN_DLL_VAR const Stringprep_table_element stringprep_rfc3454_D_2[];
+  LIBIDN_API const Stringprep_table_element stringprep_rfc3454_A_1[];
+  LIBIDN_API const Stringprep_table_element stringprep_rfc3454_B_1[];
+  LIBIDN_API const Stringprep_table_element stringprep_rfc3454_B_2[];
+  LIBIDN_API const Stringprep_table_element stringprep_rfc3454_B_3[];
+  LIBIDN_API const Stringprep_table_element stringprep_rfc3454_C_1_1[];
+  LIBIDN_API const Stringprep_table_element stringprep_rfc3454_C_1_2[];
+  LIBIDN_API const Stringprep_table_element stringprep_rfc3454_C_2_1[];
+  LIBIDN_API const Stringprep_table_element stringprep_rfc3454_C_2_2[];
+  LIBIDN_API const Stringprep_table_element stringprep_rfc3454_C_3[];
+  LIBIDN_API const Stringprep_table_element stringprep_rfc3454_C_4[];
+  LIBIDN_API const Stringprep_table_element stringprep_rfc3454_C_5[];
+  LIBIDN_API const Stringprep_table_element stringprep_rfc3454_C_6[];
+  LIBIDN_API const Stringprep_table_element stringprep_rfc3454_C_7[];
+  LIBIDN_API const Stringprep_table_element stringprep_rfc3454_C_8[];
+  LIBIDN_API const Stringprep_table_element stringprep_rfc3454_C_9[];
+  LIBIDN_API const Stringprep_table_element stringprep_rfc3454_D_1[];
+  LIBIDN_API const Stringprep_table_element stringprep_rfc3454_D_2[];
 
   /* Nameprep */
 
-  extern IDN_DLL_VAR const Stringprep_profile stringprep_nameprep[];
+  LIBIDN_API const Stringprep_profile stringprep_nameprep[];
 
 #define stringprep_nameprep(in, maxlen)			\
   stringprep(in, maxlen, 0, stringprep_nameprep)
@@ -142,25 +146,25 @@ extern "C"
 
   /* SASL */
 
-  extern IDN_DLL_VAR const Stringprep_profile stringprep_saslprep[];
-  extern IDN_DLL_VAR const Stringprep_profile stringprep_plain[];
-  extern IDN_DLL_VAR const Stringprep_profile stringprep_trace[];
+  LIBIDN_API const Stringprep_profile stringprep_saslprep[];
+  LIBIDN_API const Stringprep_profile stringprep_plain[];
+  LIBIDN_API const Stringprep_profile stringprep_trace[];
 
 #define stringprep_plain(in, maxlen)		\
   stringprep(in, maxlen, 0, stringprep_plain)
 
   /* Kerberos */
 
-  extern IDN_DLL_VAR const Stringprep_profile stringprep_kerberos5[];
+  LIBIDN_API const Stringprep_profile stringprep_kerberos5[];
 
 #define stringprep_kerberos5(in, maxlen)		\
   stringprep(in, maxlen, 0, stringprep_kerberos5)
 
   /* XMPP */
 
-  extern IDN_DLL_VAR const Stringprep_profile stringprep_xmpp_nodeprep[];
-  extern IDN_DLL_VAR const Stringprep_profile stringprep_xmpp_resourceprep[];
-  extern IDN_DLL_VAR const Stringprep_table_element stringprep_xmpp_nodeprep_prohibit[];
+  LIBIDN_API const Stringprep_profile stringprep_xmpp_nodeprep[];
+  LIBIDN_API const Stringprep_profile stringprep_xmpp_resourceprep[];
+  LIBIDN_API const Stringprep_table_element stringprep_xmpp_nodeprep_prohibit[];
 
 #define stringprep_xmpp_nodeprep(in, maxlen)		\
   stringprep(in, maxlen, 0, stringprep_xmpp_nodeprep)
@@ -169,53 +173,53 @@ extern "C"
 
   /* iSCSI */
 
-  extern IDN_DLL_VAR const Stringprep_profile stringprep_iscsi[];
+  LIBIDN_API const Stringprep_profile stringprep_iscsi[];
 
 #define stringprep_iscsi(in, maxlen)		\
   stringprep(in, maxlen, 0, stringprep_iscsi)
 
   /* API */
 
-  extern int stringprep_4i (uint32_t * ucs4, size_t * len, size_t maxucs4len,
+  LIBIDN_API int stringprep_4i (uint32_t * ucs4, size_t * len, size_t maxucs4len,
 			    Stringprep_profile_flags flags,
 			    const Stringprep_profile * profile);
-  extern int stringprep_4zi (uint32_t * ucs4, size_t maxucs4len,
+  LIBIDN_API int stringprep_4zi (uint32_t * ucs4, size_t maxucs4len,
 			     Stringprep_profile_flags flags,
 			     const Stringprep_profile * profile);
-  extern int stringprep (char *in, size_t maxlen,
+  LIBIDN_API int stringprep (char *in, size_t maxlen,
 			 Stringprep_profile_flags flags,
 			 const Stringprep_profile * profile);
 
-  extern int stringprep_profile (const char *in,
+  LIBIDN_API int stringprep_profile (const char *in,
 				 char **out,
 				 const char *profile,
 				 Stringprep_profile_flags flags);
 
-  extern const char *stringprep_strerror (Stringprep_rc rc);
+  LIBIDN_API const char *stringprep_strerror (Stringprep_rc rc);
 
-  extern const char *stringprep_check_version (const char *req_version);
+  LIBIDN_API const char *stringprep_check_version (const char *req_version);
 
 /* Utility */
 
-  extern int stringprep_unichar_to_utf8 (uint32_t c, char *outbuf);
-  extern uint32_t stringprep_utf8_to_unichar (const char *p);
+  LIBIDN_API int stringprep_unichar_to_utf8 (uint32_t c, char *outbuf);
+  LIBIDN_API uint32_t stringprep_utf8_to_unichar (const char *p);
 
-  extern uint32_t *stringprep_utf8_to_ucs4 (const char *str, ssize_t len,
+  LIBIDN_API uint32_t *stringprep_utf8_to_ucs4 (const char *str, ssize_t len,
 					    size_t * items_written);
-  extern char *stringprep_ucs4_to_utf8 (const uint32_t * str, ssize_t len,
+  LIBIDN_API char *stringprep_ucs4_to_utf8 (const uint32_t * str, ssize_t len,
 					size_t * items_read,
 					size_t * items_written);
 
-  extern char *stringprep_utf8_nfkc_normalize (const char *str, ssize_t len);
-  extern uint32_t *stringprep_ucs4_nfkc_normalize (uint32_t * str,
+  LIBIDN_API char *stringprep_utf8_nfkc_normalize (const char *str, ssize_t len);
+  LIBIDN_API uint32_t *stringprep_ucs4_nfkc_normalize (uint32_t * str,
 						   ssize_t len);
 
-  extern const char *stringprep_locale_charset (void);
-  extern char *stringprep_convert (const char *str,
+  LIBIDN_API const char *stringprep_locale_charset (void);
+  LIBIDN_API char *stringprep_convert (const char *str,
 				   const char *to_codeset,
 				   const char *from_codeset);
-  extern char *stringprep_locale_to_utf8 (const char *str);
-  extern char *stringprep_utf8_to_locale (const char *str);
+  LIBIDN_API char *stringprep_locale_to_utf8 (const char *str);
+  LIBIDN_API char *stringprep_utf8_to_locale (const char *str);
 
 #ifdef __cplusplus
 }
diff --git a/win32/include/gen-win32-headers.pl b/win32/include/gen-win32-headers.pl
index c750cbb..d934269 100644
--- a/win32/include/gen-win32-headers.pl
+++ b/win32/include/gen-win32-headers.pl
@@ -84,26 +84,7 @@ CONFIG
 
 while(<>) {
 	if(! m/^extern "C"/gi) {
-		if(s/\@PACKAGE_VERSION@/$version/gi) {
-			print;
-			print <<EXPORTS;
-
-/* ${package} Windows DLL */
-#ifndef ${upackage}_API
-# if defined(_WIN32) && !defined(${upackage}_STATIC)
-#  ifdef ${upackage}_EXPORTS
-#   define ${upackage}_API  __declspec(dllexport)
-#  else /* ${upackage}_EXPORTS */
-#   define ${upackage}_API  __declspec(dllimport)
-#  endif /* ${upackage}_EXPORTS */
-# else /* _WIN32 && !${upackage}_STATIC */
-#  define ${upackage}_API extern
-# endif /* _WIN32 && !${upackage}_STATIC */
-#endif /* ${upackage}_API */
-EXPORTS
-			next;
-		}
-		s/^(\s*)extern(\s*IDN_DLL_VAR)?/$1${upackage}_API/i;
+		s/\@PACKAGE_VERSION@/$version/gi;
 	}
 	print;
 }
