Some WIN32 porting by Farooq Zaman.
diff --git a/ChangeLog b/ChangeLog
index e127fa8..9765157 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,13 @@
+2006-11-29  Linus Walleij <[email protected]>
+
+	* src/libusb-glue.c: remove some pointless #include <>
+	  clutter, call usb_set_configuration() as needed by
+	  Windows.
+	* src/libmtp.c: various fixups to enable use of Windows
+	  <io.h> include file.
+	* README.windows.txt: added a quick porting guide for 
+	  MSVC by Farooq Zaman.
+
 2006-11-28  Linus Walleij <[email protected]>
 
 	* src/libmtp.h.in: removed filetype mapping API. We can handle
diff --git a/Makefile.am b/Makefile.am
index 12b4e1e..a8b110e 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -3,7 +3,7 @@
 
 pkgconfigdir=$(libdir)/pkgconfig
 pkgconfig_DATA=libmtp.pc
-EXTRA_DIST=libmtp.pc libmtp.sh COPYING
+EXTRA_DIST=libmtp.pc libmtp.sh COPYING README.windows.txt
 noinst_DATA=libmtp.usermap libmtp.rules libmtp.fdi
 libmtp.usermap: examples/hotplug
 	examples/hotplug > libmtp.usermap
diff --git a/README.windows.txt b/README.windows.txt
new file mode 100644
index 0000000..53d2e75
--- /dev/null
+++ b/README.windows.txt
@@ -0,0 +1,86 @@
+This file was created by Farooq Zaman <[email protected]>

+as a guide to practical Windows porting of libmtp using MSVC (Microsoft

+Visual C++). Some details may have changed, e.g. the things that could be 

+fixed in libmtp sources by #ifdef __WIN32__ macros have been folded back 

+in. This has been tested on Windows 2000.

+

+

+libmtp.c

+========

+	1. Include <io.h> file.

+	2. Comment out <sys/mman.h> file.

+	3. Line# 2115 replace "open" with "_open" and replace "S_IRWXU|S_IRGRP" with "_S_IREAD".

+	4. Line# 2126 replace "close" with "_close".

+	5. Line# 2283 replace "open" with "_open" and add one more closing ")" at the end of _open API.

+	6. Line# 2294 replace "close" with "_close".

+	7. Line# 2502 and Line# 2513 repeat steps 5 and 6.

+libmtp.h

+========

+	1. replace <usb.h> and <stdint.h> with "usb.h" and "stdint.h" respectively.

+

+libusb-glue.c

+=============

+	1. Comment out <getopt.h>, <unistd.h>, <utime.h> and <sys/mman.h> includes.

+	2. Replace <usb.h> with "usb.h"

+	3. Line# 537 Add usb_set_configuration(device_handle, dev->config->bConfigurationValue); before claiming USB 	interface.

+

+ptp.c

+======

+	1. Comment out <config.h> and <unistd.h> include macro.

+	2. Include "libmtp.h" file.

+	3. Line# 484 remove "inline" keyword from the function.

+

+ptp.h

+=====

+	1. replace <iconv.h> with "iconv.h".

+

+ptp-pack.c

+==========

+	1. replace <iconv.h> with "iconv.h".

+	2. Include "stdint.h" and "ptp.h" files. Windows C doesn't have "stdint.h" file. I took this file from Cygwin.

+	3. Remove "inline" keyword with all the functions in this file.

+

+gphoto2-endian.h

+================

+	1. replace <arpa/inet.h> with <winsock.h> .

+

+

+stdint.h

+========

+        1. MSVC doesn't have stdint.h file as part of its C. Get this file from someother source(e.g Cygwin in my case). And 	replace "long long" with "__int64".

+

+byteswap.h

+==========

+        1. MSVC doesn't have this file too. I created this file myself by just copying bswap_16(), bswap_32() and bswap_64() 	macros from Linux /usr/include/bits/byteswap.h file. 

+

+

+

+Libraries

+=========

+        You will need two libraries for windows "libusb" and "libiconv".

+	1. You can download libusb from

+ 		http://libusb-win32.sourceforge.net/#downloads

+	Download the Device driver version, unpack the archive and get libusb.lib and libusb0.dll files.

+        Place libusb.lib in the libs folder and libusb0.dll file in the debug folder of MSVC project.

+	Also get libusb.h file and add it to your project.

+

+	2. You can Download libiconv-2.dll file from 

+		http://sourceforge.net/project/showfiles.php?group_id=114505&package_id=147572&release_id=356820

+	and libiconv.lib file from 

+		http://gnuwin32.sourceforge.net/packages/libiconv.htm	

+	Download Developer files and get libiconv.lib file.

+	Place libiconv.lib in the libs folder and libicon-2.dll in debug folder of MSVC project. 

+	Also get libiconv.h file and add it to your project.

+

+

+	3. Goto Project->settings. In the "link" tab and "General" category, add "libusb.lib" and "libiconv.lib" to

+	   "Object/library modules".

+

+	4. Change Category to Input and add your libs folder Path to "Additional Library Path".

+

+libusb filter Driver

+====================

+	You will need libusb driver for libusb to function properly. 

+	Get libusb filter driver version from this link

+		http://libusb-win32.sourceforge.net/#downloads

+	Download the filter version and install it.
\ No newline at end of file
diff --git a/src/libmtp.c b/src/libmtp.c
index 50bf18e..72b8a5a 100644
--- a/src/libmtp.c
+++ b/src/libmtp.c
@@ -12,17 +12,20 @@
  * The files libusb-glue.c/.h are just what they say: an
  * interface to libusb for the actual, physical USB traffic.
  */
-#include <string.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <fcntl.h>
-#include <sys/mman.h>
-
 #include "libmtp.h"
 #include "unicode.h"
 #include "ptp.h"
 #include "libusb-glue.h"
 
+#include <string.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#ifdef _MSC_VER // For MSVC++
+#define USE_WINDOWS_IO_H
+#include <io.h>
+#endif
+
 /* Enable enhanced MTP commands */
 #define ENABLE_MTP_ENHANCED
 
@@ -66,6 +69,7 @@
  */
 static int register_filetype(char const * const description, LIBMTP_filetype_t const id,
 			     uint16_t const ptp_id);
+static void init_filemap();
 static void flush_handles(LIBMTP_mtpdevice_t *device);
 static uint16_t map_libmtp_type_to_ptp_type(LIBMTP_filetype_t intype);
 static LIBMTP_filetype_t map_ptp_type_to_libmtp_type(uint16_t intype);
@@ -73,6 +77,14 @@
 				       char **unicstring, uint16_t property);
 static void get_track_metadata(LIBMTP_mtpdevice_t *device, uint16_t objectformat,
 			       LIBMTP_track_t *track);
+static int create_new_abstract_list(LIBMTP_mtpdevice_t *device,
+				    char const * const name,
+				    uint32_t const parenthandle,
+				    uint16_t const objectformat,
+				    char const * const suffix,
+				    uint32_t * const newid,
+				    uint32_t const * const tracks,
+				    uint32_t const no_tracks);
 static MTPPropList *New_MTP_Prop_Entry();
 static void Destroy_MTP_Prop_Entry(MTPPropList *prop);
 
@@ -2034,7 +2046,11 @@
 
   // Open file
 #ifdef __WIN32__
+#ifdef USE_WINDOWS_IO_H
+  if ( (fd = _open(path, O_RDWR|O_CREAT|O_TRUNC|O_BINARY,_S_IREAD)) == -1 ) {
+#else
   if ( (fd = open(path, O_RDWR|O_CREAT|O_TRUNC|O_BINARY,S_IRWXU|S_IRGRP)) == -1 ) {
+#endif
 #else
   if ( (fd = open(path, O_RDWR|O_CREAT|O_TRUNC,S_IRWXU|S_IRGRP)) == -1) {
 #endif
@@ -2202,7 +2218,11 @@
 
   // Open file
 #ifdef __WIN32__
+#ifdef USE_WINDOWS_IO_H
+  if ( (fd = _open(path, O_RDONLY|O_BINARY) == -1 ) {
+#else
   if ( (fd = open(path, O_RDONLY|O_BINARY) == -1 ) {
+#endif
 #else
   if ( (fd = open(path, O_RDONLY)) == -1) {
 #endif
@@ -2213,7 +2233,11 @@
   ret = LIBMTP_Send_Track_From_File_Descriptor(device, fd, metadata, callback, data, parenthandle);
 
   // Close file.
+#ifdef USE_WINDOWS_IO_H
+  _close(fd);
+#else
   close(fd);
+#endif
 
   return ret;
 }
@@ -2560,7 +2584,11 @@
 
   // Open file
 #ifdef __WIN32__
+#ifdef USE_WINDOWS_IO_H
+  if ( (fd = _open(path, O_RDONLY|O_BINARY) == -1 ) {
+#else
   if ( (fd = open(path, O_RDONLY|O_BINARY) == -1 ) {
+#endif
 #else
   if ( (fd = open(path, O_RDONLY)) == -1) {
 #endif
@@ -2571,7 +2599,11 @@
   ret = LIBMTP_Send_File_From_File_Descriptor(device, fd, filedata, callback, data, parenthandle);
 
   // Close file.
+#ifdef USE_WINDOWS_IO_H
+  _close(fd);
+#else
   close(fd);
+#endif
 
   return ret;
 }
diff --git a/src/libusb-glue.c b/src/libusb-glue.c
index d4c8647..403787a 100644
--- a/src/libusb-glue.c
+++ b/src/libusb-glue.c
@@ -9,24 +9,17 @@
  * use MTP devices. Include mtp-utils.h to use any of the ptp/mtp functions.
  *
  */
+#include "libmtp.h"
+#include "libusb-glue.h"
+#include "util.h"
 #include "ptp.h"
+
 #include <errno.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#include <getopt.h>
-#include <unistd.h>
-#include <sys/types.h>
-#include <utime.h>
-#include <sys/stat.h>
-#include <fcntl.h>
-#include <sys/mman.h>
 #include <usb.h>
 
-#include "libmtp.h"
-#include "libusb-glue.h"
-#include "util.h"
-
 /* To enable debug prints, switch on this */
 //#define ENABLE_USB_BULK_DEBUG
 
@@ -559,10 +552,16 @@
      */
     if (ptp_usb->device_flags & DEVICE_FLAG_UNLOAD_DRIVER) {
       if (usb_detach_kernel_driver_np(device_handle, dev->config->interface->altsetting->bInterfaceNumber)) {
-	perror("usb_detach_kernel_driver_np()");
+	// Totally ignore this error!
+	// perror("usb_detach_kernel_driver_np()");
       }
     }
 #endif
+    // Originally only done on Windows, but shouldn't hurt.
+    if (usb_set_configuration(device_handle, dev->config->bConfigurationValue)) {
+      perror("usb_set_configuration()");
+      return -1;
+    }
     if (usb_claim_interface(device_handle, dev->config->interface->altsetting->bInterfaceNumber)) {
       perror("usb_claim_interface()");
       return -1;