在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
[IOSDevice4Delphi] IOS设备连接文件管理库 For Delphi 转载请注明地址:http://www.cnblogs.com/lahcs/archive/2013/04/19/3031172.html 首先申明:这个库是我根据网上的C/C++/C#代码进行代码翻译的结果。目前并不完善,仅处理了一些基础简单的操作,例如:设备信息读取、目录读取、文件上传下载删除,同时可能存在部分bug。代码及Demo仅作下载学习使用,请勿用于非法用途。 如果有修改或更新,烦请转发我一份,共同学习。 参考代码或资料: The iPhone wiki :http://theiphonewiki.com/wiki/MobileDevice_Library (目前最详细的资料,提供了相关函数的详细解释及用法) Manzana :http://manzana.googlecode.com/ (开源C#项目,可能已经被关闭,请各位自行搜索下载) 基本原理使用Windows版本iTunes提供的 iTunesMobileDevice.dll 动态链接库,调用其相关函数达到操作IOS设备的目的,函数导出表国内很多人都有发出来,但是较少有人进行详细解释。 我这里仅仅是通过参考The iPhone wiki上提供的C/C++的头文件,同时参考了Manzana等开源C#项目相关代码,翻译转换成Delphi,学习而已,各位大牛勿喷。。。
附iTunesMobileDevice.dll函数导出表:
View Code
Demo效果展示:
IOSDevice4Delphi核心代码: AMoblieDeviceModule.pas
View Code
1 {*******************************************************} 2 { } 3 { IOS Device Management Class } 4 { } 5 { author : LAHCS } 6 { } 7 { E-Mail : [email protected] } 8 { } 9 { QQ : 307643816 } 10 { } 11 { Copy Right (C) 2013 } 12 { } 13 {*******************************************************} 14 { ReferenceList: 15 [The iPhone wiki] http://theiphonewiki.com/wiki/MobileDevice_Library 16 [Manzana] http://manzana.googlecode.com/ 17 } 18 unit AMoblieDeviceModule; 19 20 interface 21 22 uses 23 Windows, SysUtils, IniFiles, AMobileDevice, AMoblieDeviceFuncModule, AMoblieDeviceModuleDef; 24 25 type 26 TDeviceConnectEvent = procedure(Sender: TObject;Device: TAMoblieDevice) of object; 27 28 TAMobileDeviceModule = class(TObject) 29 private 30 FLastErrCode : Cardinal; 31 FOnDeciveConnect : TDeviceConnectEvent; 32 FOnDeviceDisconnect : TDeviceConnectEvent; 33 private 34 hiTunesMobileDeviceModule : HMODULE; 35 hCoreFoundationModule : HMODULE; 36 p_AMDeviceNotification : p_am_device_notification; 37 private 38 FDeviceList : THashedStringList; 39 private 40 function GetDevice(Value: Integer):TAMoblieDevice; 41 function GetDeviceCount:Integer; 42 private 43 procedure DoOnNotificationCallBack(value: p_am_device_notification_callback_info); 44 procedure DoOnDeviceConnectNotice(device: p_am_device); 45 procedure DoOnDeviceDisConnectNotice(device: p_am_device); 46 procedure DoOnDeviceOtherNotice(device: p_am_device); 47 public 48 function InitialModule():Boolean; 49 function Subscribe():Boolean; 50 public 51 constructor Create; 52 destructor Destroy; override; 53 public 54 property OnDeviceConnect : TDeviceConnectEvent 55 read FOnDeciveConnect 56 write FOnDeciveConnect; 57 58 property OnDeviceDisconnect : TDeviceConnectEvent 59 read FOnDeviceDisconnect 60 write FOnDeviceDisconnect; 61 62 property Item[index:Integer]:TAMoblieDevice 63 read GetDevice; 64 65 property Count : Integer 66 read GetDeviceCount; 67 end; 68 69 var 70 lpAMobileDeviceModule : TAMobileDeviceModule; 71 72 implementation 73 74 function SetDllDirectory(lpPathName:PWideChar): Bool; stdcall; external 'kernel32.dll' name 'SetDllDirectoryW'; 75 76 function GetArrayStr(Value:array of UCHAR):string; 77 var 78 i : Integer; 79 begin 80 Result := ''; 81 for i:= 0 to Length(Value) - 1 do 82 begin 83 Result := Result + IntToHex(Value[i],2); 84 end; 85 end; 86 87 procedure AMDeviceNotificationCallback( 88 value: p_am_device_notification_callback_info);cdecl; 89 begin 90 lpAMobileDeviceModule.DoOnNotificationCallBack(value); 91 end; 92 93 procedure LoadiTunesMobileDeviceModule(var hiTunesMobileDeviceModule:HMODULE;var fun_pointer:Pointer;fun_name:PChar); 94 begin 95 if hiTunesMobileDeviceModule = 0 then 96 Exit; 97 fun_pointer := GetProcAddress(hiTunesMobileDeviceModule,fun_name); 98 if fun_pointer = nil then 99 begin 100 FreeLibrary(hiTunesMobileDeviceModule); 101 hiTunesMobileDeviceModule := 0; 102 end; 103 end; 104 105 { TAMobileDeviceModule } 106 107 constructor TAMobileDeviceModule.Create; 108 begin 109 FDeviceList := THashedStringList.Create; 110 FuncModule := TAMoblieDeviceFuncModule.Create; 111 end; 112 113 destructor TAMobileDeviceModule.Destroy; 114 var 115 i : Integer; 116 begin 117 for i:= 0 to FDeviceList.Count - 1 do 118 TAMoblieDevice(FDeviceList.Objects[i]).Destroy; 119 120 FDeviceList.Clear; 121 FuncModule.Destroy; 122 inherited; 123 end; 124 125 function TAMobileDeviceModule.InitialModule:Boolean; 126 var 127 strEnvironmentPath : WideString; 128 FMDS_PATH , FAAS_PATH : WideString; 129 begin 130 Result := False; 131 FLastErrCode := $FFFFFFFF; 132 133 strEnvironmentPath := GetEnvironmentVariable('CommonProgramFiles'); 134 135 FMDS_PATH := strEnvironmentPath + MDS_PATH; 136 FAAS_PATH := strEnvironmentPath + AAS_PATH; 137 138 SetDllDirectory(PWideChar(FAAS_PATH)); 139 140 hiTunesMobileDeviceModule := LoadLibraryW(PWideChar(FMDS_PATH + 'iTunesMobileDevice.dll')); 141 if hiTunesMobileDeviceModule <> 0 then 142 begin 143 LoadiTunesMobileDeviceModule(hiTunesMobileDeviceModule,@FuncModule.lpf_AMDeviceNotificationSubscribe,'AMDeviceNotificationSubscribe'); 144 LoadiTunesMobileDeviceModule(hiTunesMobileDeviceModule,@FuncModule.lpf_AMDeviceNotificationUnsubscribe,'AMDeviceNotificationUnsubscribe'); 145 LoadiTunesMobileDeviceModule(hiTunesMobileDeviceModule,@FuncModule.lpf_AMDeviceConnect,'AMDeviceConnect'); 146 LoadiTunesMobileDeviceModule(hiTunesMobileDeviceModule,@FuncModule.lpf_AMDeviceDisconnect,'AMDeviceDisconnect'); 147 LoadiTunesMobileDeviceModule(hiTunesMobileDeviceModule,@FuncModule.lpf_AMDeviceIsPaired,'AMDeviceIsPaired'); 148 LoadiTunesMobileDeviceModule(hiTunesMobileDeviceModule,@FuncModule.lpf_AMDeviceValidatePairing,'AMDeviceValidatePairing'); 149 LoadiTunesMobileDeviceModule(hiTunesMobileDeviceModule,@FuncModule.lpf_AMDeviceStartSession,'AMDeviceStartSession'); 150 LoadiTunesMobileDeviceModule(hiTunesMobileDeviceModule,@FuncModule.lpf_AMDeviceStopSession,'AMDeviceStopSession'); 151 LoadiTunesMobileDeviceModule(hiTunesMobileDeviceModule,@FuncModule.lpf_AMDeviceStartService,'AMDeviceStartService'); 152 LoadiTunesMobileDeviceModule(hiTunesMobileDeviceModule,@FuncModule.lpf_AMDeviceCopyValue,'AMDeviceCopyValue'); 153 LoadiTunesMobileDeviceModule(hiTunesMobileDeviceModule,@FuncModule.lpf_AFCConnectionOpen,'AFCConnectionOpen'); 154 LoadiTunesMobileDeviceModule(hiTunesMobileDeviceModule,@FuncModule.lpf_AFCConnectionClose,'AFCConnectionClose'); 155 LoadiTunesMobileDeviceModule(hiTunesMobileDeviceModule,@FuncModule.lpf_AFCDirectoryOpen,'AFCDirectoryOpen'); 156 LoadiTunesMobileDeviceModule(hiTunesMobileDeviceModule,@FuncModule.lpf_AFCDirectoryRead,'AFCDirectoryRead'); 157 LoadiTunesMobileDeviceModule(hiTunesMobileDeviceModule,@FuncModule.lpf_AFCDirectoryClose,'AFCDirectoryClose'); 158 LoadiTunesMobileDeviceModule(hiTunesMobileDeviceModule,@FuncModule.lpf_AFCDirectoryCreate,'AFCDirectoryCreate'); 159 LoadiTunesMobileDeviceModule(hiTunesMobileDeviceModule,@FuncModule.lpf_AFCDeviceInfoOpen,'AFCDeviceInfoOpen'); 160 LoadiTunesMobileDeviceModule(hiTunesMobileDeviceModule,@FuncModule.lpf_AFCFileInfoOpen,'AFCFileInfoOpen'); 161 LoadiTunesMobileDeviceModule(hiTunesMobileDeviceModule,@FuncModule.lpf_AFCKeyValueRead,'AFCKeyValueRead'); 162 LoadiTunesMobileDeviceModule(hiTunesMobileDeviceModule,@FuncModule.lpf_AFCKeyValueClose,'AFCKeyValueClose'); 163 LoadiTunesMobileDeviceModule(hiTunesMobileDeviceModule,@FuncModule.lpf_AFCRemovePath,'AFCRemovePath'); 164 LoadiTunesMobileDeviceModule(hiTunesMobileDeviceModule,@FuncModule.lpf_AFCRenamePath,'AFCRenamePath'); 165 LoadiTunesMobileDeviceModule(hiTunesMobileDeviceModule,@FuncModule.lpf_AFCFileRefOpen,'AFCFileRefOpen'); 166 LoadiTunesMobileDeviceModule(hiTunesMobileDeviceModule,@FuncModule.lpf_AFCFileRefClose,'AFCFileRefClose'); 167 LoadiTunesMobileDeviceModule(hiTunesMobileDeviceModule,@FuncModule.lpf_AFCFileRefRead,'AFCFileRefRead'); 168 LoadiTunesMobileDeviceModule(hiTunesMobileDeviceModule,@FuncModule.lpf_AFCFileRefWrite,'AFCFileRefWrite'); 169 LoadiTunesMobileDeviceModule(hiTunesMobileDeviceModule,@FuncModule.lpf_AFCFlushData,'AFCFlushData'); 170 LoadiTunesMobileDeviceModule(hiTunesMobileDeviceModule,@FuncModule.lpf_AFCFileRefSeek,'AFCFileRefSeek'); 171 LoadiTunesMobileDeviceModule(hiTunesMobileDeviceModule,@FuncModule.lpf_AFCFileRefTell,'AFCFileRefTell'); 172 LoadiTunesMobileDeviceModule(hiTunesMobileDeviceModule,@FuncModule.lpf_AFCFileRefSetFileSize,'AFCFileRefSetFileSize'); 173 end 174 else 175 Exit; 176 177 hCoreFoundationModule := LoadLibraryW(PWideChar(FAAS_PATH + 'CoreFoundation.dll')); 178 if hCoreFoundationModule <> 0 then 179 begin 180 LoadiTunesMobileDeviceModule(hCoreFoundationModule,@FuncModule.lpf_CFStringCreateWithCString,'CFStringCreateWithCString'); 181 LoadiTunesMobileDeviceModule(hCoreFoundationModule,@FuncModule.lpf_CFPropertyListCreateFromXMLData,'CFPropertyListCreateFromXMLData'); 182 LoadiTunesMobileDeviceModule(hCoreFoundationModule,@FuncModule.lpf_CFPropertyListCreateXMLData,'CFPropertyListCreateXMLData'); 183 Result := True; 184 end 185 else 186 Exit; 187 end; 188 189 function TAMobileDeviceModule.Subscribe: Boolean; 190 begin 191 Result := False; 192 FLastErrCode := FuncModule.lpf_AMDeviceNotificationSubscribe(AMDeviceNotificationCallback,0,0,0,@p_AMDeviceNotification); 193 if FLastErrCode = 0 then 194 Result := True; 195 end; 196 197 procedure TAMobileDeviceModule.DoOnNotificationCallBack( 198 value: p_am_device_notification_callback_info); 199 begin 200 case value.msg of 201 ADNCI_MSG_CONNECTED : DoOnDeviceConnectNotice(value.dev); 202 ADNCI_MSG_DISCONNECTED : DoOnDeviceDisConnectNotice(value.dev); 203 ADNCI_MSG_UNKNOWN : DoOnDeviceOtherNotice(value.dev); 204 end; 205 end; 206 207 procedure TAMobileDeviceModule.DoOnDeviceConnectNotice( 208 device: p_am_device); 209 var 210 ADevice : TAMoblieDevice; 211 strHashKey : string; 212 intDeviceIndex : Integer; 213 begin 214 intDeviceIndex := 0; 215 strHashKey := inttoHex(device.device_id,8) + inttoHex(device.product_id,8); 216 if FDeviceList.Find(strHashKey,intDeviceIndex) then 217 begin 218 ADevice := TAMoblieDevice(FDeviceList.Objects[intDeviceIndex]); 219 if Assigned(ADevice) then 220 begin 221 ADevice.Device := device; 222 ADevice.ReConnect := True; 223 end; 224 end 225 else 226 begin 227 ADevice := TAMoblieDevice.Create(device); 228 FDeviceList.AddObject(strHashKey,ADevice); 229 end; 230 231 if Assigned(FOnDeciveConnect) then 232 FOnDeciveConnect(Self,ADevice); 233 end; 234 235 procedure TAMobileDeviceModule.DoOnDeviceDisConnectNotice( 236 device: p_am_device); 237 var 238 ADevice : TAMoblieDevice; 239 strHashKey : string; 240 intDeviceIndex : Integer; 241 begin 242 intDeviceIndex := 0; 243 strHashKey := inttoHex(device.device_id,8) + inttoHex(device.product_id,8); 244 if FDeviceList.Find(strHashKey,intDeviceIndex) then 245 begin 246 ADevice := TAMoblieDevice(FDeviceList.Objects[intDeviceIndex]); 247 if Assigned(ADevice) then 248 begin 249 ADevice.DisConnect; 250 if Assigned(FOnDeviceDisconnect) then 251 FOnDeviceDisconnect(Self,ADevice); 252 end; 253 end; 254 end; 255 256 procedure TAMobileDeviceModule.DoOnDeviceOtherNotice(device: p_am_device); 257 var 258 ADevice : TAMoblieDevice; 259 strHashKey : string; 260 intDeviceIndex : Integer; 261 begin 262 intDeviceIndex := 0; 263 strHashKey := inttoHex(device.device_id,8) + inttoHex(device.product_id,8); 264 if FDeviceList.Find(strHashKey,intDeviceIndex) then 265 begin 266 ADevice := TAMoblieDevice(FDeviceList.Objects[intDeviceIndex]); 267 if Assigned(ADevice) then 268 begin 269 ADevice.DisConnect; 270 end; 271 end; 272 end; 273 274 function TAMobileDeviceModule.GetDevice(Value: Integer): TAMoblieDevice; 275 begin 276 if (Value < 0) or (Value > FDeviceList.Count - 1) then 277 begin 278 Result := nil; 279 Exit; 280 end; 281 Result := TAMoblieDevice(FDeviceList.Objects[Value]); 282 end; 283 284 function TAMobileDeviceModule.GetDeviceCount: Integer; 285 begin 286 Result := FDeviceList.Count; 287 end; 288 289 end. AMoblieDeviceFuncModule.pas
View Code
1 {*******************************************************} 2 { } 3 { iTunesMobileDevice Function Class } 4 { } 5 { author : LAHCS } 6 { } 7 { E-Mail : [email protected] } 8 { } 9 { QQ : 307643816 } 10 { } 11 { Copy Right (C) 2013 } 12 { } 13 {*******************************************************} 14 { ReferenceList: 15 [The iPhone wiki] http://theiphonewiki.com/wiki/MobileDevice_Library 16 [Manzana] http://manzana.googlecode.com/ 17 } 18 unit AMoblieDeviceFuncModule; 19 20 interface 21 22 uses 23 AMoblieDeviceModuleDef; 24 25 type 26 27 TAMoblieDeviceFuncModule = class(TObject) 28 public 29 lpf_CFStringCreateWithCString : FunType_CFStringCreateWithCString; 30 lpf_CFPropertyListCreateFromXMLData : FunType_CFPropertyListCreateFromXMLData; 31 lpf_CFPropertyListCreateXMLData : FunType_CFPropertyListCreateXMLData; 32 public 33 lpf_AMDeviceNotificationSubscribe : FunType_AMDeviceNotificationSubscribe; 34 lpf_AMDeviceNotificationUnsubscribe : FunType_AMDeviceNotificationUnsubscribe; 35 lpf_AMDeviceConnect : FunType_AMDeviceConnect; 36 lpf_AMDeviceDisconnect : FunType_AMDeviceDisconnect; 37 lpf_AMDeviceIsPaired : FunType_AMDeviceIsPaired; 38 lpf_AMDeviceValidatePairing : FunType_AMDeviceValidatePairing; 39 lpf_AMDeviceStartSession : FunType_AMDeviceStartSession; 40 lpf_AMDeviceStopSession : FunType_AMDeviceStopSession; 41 lpf_AMDeviceStartService : FunType_AMDeviceStartService; 42 lpf_AMDeviceCopyValue : FunType_AMDeviceCopyValue; 43 public 44 lpf_AFCConnectionOpen : FunType_AFCConnectionOpen; 45 lpf_AFCConnectionClose : FunType_AFCConnectionClose; 46 lpf_AFCDirectoryOpen : FunType_AFCDirectoryOpen; 47 lpf_AFCDirectoryRead : FunType_AFCDirectoryRead; 48 lpf_AFCDirectoryClose : FunType_AFCDirectoryClose; 49 lpf_AFCDirectoryCreate : FunType_AFCDirectoryCreate; 50 lpf_AFCDeviceInfoOpen : FunType_AFCDeviceInfoOpen; 51 lpf_AFCFileInfoOpen : FunType_AFCFileInfoOpen; 52 lpf_AFCKeyValueRead : FunType_AFCKeyValueRead; 53 lpf_AFCKeyValueClose : FunType_AFCKeyValueClose; 54 lpf_AFCRemovePath : FunType_AFCRemovePath; 55 lpf_AFCRenamePath : FunType_AFCRenamePath; 56 lpf_AFCFileRefOpen : FunType_AFCFileRefOpen; 57 lpf_AFCFileRefClose : FunType_AFCFileRefClose; 58 lpf_AFCFileRefRead : FunType_AFCFileRefRead; 59 lpf_AFCFileRefWrite : FunType_AFCFileRefWrite; 60 lpf_AFCFlushData : FunType_AFCFlushData; 61 lpf_AFCFileRefSeek : FunType_AFCFileRefSeek; 62 lpf_AFCFileRefTell : FunType_AFCFileRefTell; 63 lpf_AFCFileRefSetFileSize : FunType_AFCFileRefSetFileSize; 64 end; 65 66 var 67 FuncModule : TAMoblieDeviceFuncModule; 68 69 implementation 70 71 end. AMoblieDeviceModuleDef.pas
View Code
全部评论
专题导读
上一篇:DelphiXE5forAndroid(四)发布时间:2022-07-18下一篇:win10+vs2013+cuda7.5+cuDnnv4+caffe-windows+matlab+python2环境配置发布时间:2022-07-18热门推荐
热门话题
阅读排行榜
|
请发表评论