iPodTest.cpp:iTunesMobileDeviceDLL.dll调用举例

// iPodTest.cpp : Defines the entry point for the console application.
//

#ifndef _WIN32_WINNT            // Allow use of features specific to Windows XP or later.                   
#define _WIN32_WINNT 0x0501     // Change this to the appropriate value to target other versions of Windows.
#endif                                          

#pragma warning(disable:4996)

#include <stdio.h>
#include <tchar.h>
#include <afxwin.h>         // MFC core and standard components

#include "MobileDevice.h"

typedef enum { IPOD_STATE_UNCONNECTED = 0, IPOD_STATE_CONNECTED, IPOD_STATE_READY } t_iPodState;

// State variables
volatile t_iPodState iPodState = IPOD_STATE_UNCONNECTED;
volatile struct am_device *iPodDev = NULL;
CFStringRef iPodAFCName;
struct afc_connection *iPodAFC;
struct afc_connection *iPodConnection;

// Dll dynamic loading data
char *piTunesMobileDevicePath = NULL;
HINSTANCE iTunesDll = NULL;

// Dll routines
tf_AMDeviceNotificationSubscribe        AMDeviceNotificationSubscribe;
tf_AMDeviceConnect                              AMDeviceConnect;
tf_AMDeviceDisconnect                   AMDeviceDisconnect;
tf_AMDeviceIsPaired                             AMDeviceIsPaired;
tf_AMDeviceValidatePairing              AMDeviceValidatePairing;
tf_AMDeviceStartSession                 AMDeviceStartSession;
tf_AMDeviceStartService                 AMDeviceStartService;
tf_AMDeviceStopSession                  AMDeviceStopSession;
tf_AFCConnectionOpen                            AFCConnectionOpen;
tf_AFCDeviceInfoOpen                            AFCDeviceInfoOpen;
tf_AFCDirectoryOpen                             AFCDirectoryOpen;
tf_AFCDirectoryRead                             AFCDirectoryRead;
tf_AFCDirectoryClose                            AFCDirectoryClose;
tf_AFCFileInfoOpen                              AFCFileInfoOpen;
tf_AFCKeyValueRead                              AFCKeyValueRead;
tf_AFCKeyValueClose                             AFCKeyValueClose;
tf_AFCFileRefOpen                               AFCFileRefOpen;
tf_AFCFileRefClose                              AFCFileRefClose;
tf_AFCFileRefRead                               AFCFileRefRead;
tf_AFCFileRefWrite                              AFCFileRefWrite;
tf_AFCRemovePath                                        AFCRemovePath;
tf_AFCDirectoryCreate                   AFCDirectoryCreate;
tf_AFCRenamePath                                        AFCRenamePath;
tf_AFCGetFileInfo                                       AFCGetFileInfo;

void init()
{
        HKEY hSetting = NULL;
        DWORD length = 0;
        CString path;
        int pos;

        // Adds iTunesMobileDevice.dll folder to the path, from the registry:
        if (::RegCreateKey(HKEY_LOCAL_MACHINE, _T("SOFTWARE\\Apple Inc.\\Apple Mobile Device Support\\Shared"), &hSetting) != ERROR_SUCCESS)
                throw "iTunesMobileDevice library not found";
        if (::RegQueryValueEx(hSetting, _T("iTunesMobileDeviceDLL"), NULL, NULL, NULL, &length) != ERROR_SUCCESS)
                throw "iTunesMobileDevice library not found";
        piTunesMobileDevicePath = new char[length+1];
        ::RegQueryValueEx(hSetting, _T("iTunesMobileDeviceDLL"), NULL, NULL, (LPBYTE)piTunesMobileDevicePath, &length);

        // Adds the folder to the current system path:  
        path.GetEnvironmentVariable("PATH");
        path = (path + ";") + piTunesMobileDevicePath;
        pos = path.ReverseFind('\\');
        if (pos >= 0)
                path = path.Left(pos);
        SetEnvironmentVariable("PATH", path);

        // Loads the DLL routines
        iTunesDll = LoadLibrary(piTunesMobileDevicePath);
        //iTunesDll = LoadLibrary("C:\\Program Files\\Common Files\\Apple\\Mobile Device Support\\bin\\MobileDevice.dll");
        if (iTunesDll) {
                AMDeviceNotificationSubscribe = (tf_AMDeviceNotificationSubscribe)GetProcAddress(iTunesDll, "AMDeviceNotificationSubscribe");
                AMDeviceConnect = (tf_AMDeviceConnect)GetProcAddress(iTunesDll, "AMDeviceConnect");
                AMDeviceDisconnect = (tf_AMDeviceDisconnect)GetProcAddress(iTunesDll, "AMDeviceDisconnect");
                AMDeviceIsPaired = (tf_AMDeviceIsPaired)GetProcAddress(iTunesDll, "AMDeviceIsPaired");
                AMDeviceValidatePairing = (tf_AMDeviceValidatePairing)GetProcAddress(iTunesDll, "AMDeviceValidatePairing");
                AMDeviceStartSession = (tf_AMDeviceStartSession)GetProcAddress(iTunesDll, "AMDeviceStartSession");
                AMDeviceStartService = (tf_AMDeviceStartService)GetProcAddress(iTunesDll, "AMDeviceStartService");
                AMDeviceStopSession = (tf_AMDeviceStopSession)GetProcAddress(iTunesDll, "AMDeviceStopSession");
                AFCConnectionOpen = (tf_AFCConnectionOpen)GetProcAddress(iTunesDll, "AFCConnectionOpen");
                AFCDeviceInfoOpen = (tf_AFCDeviceInfoOpen)GetProcAddress(iTunesDll, "AFCDeviceInfoOpen");
                AFCDirectoryOpen = (tf_AFCDirectoryOpen)GetProcAddress(iTunesDll, "AFCDirectoryOpen");
                AFCDirectoryRead = (tf_AFCDirectoryRead)GetProcAddress(iTunesDll, "AFCDirectoryRead");
                AFCDirectoryClose = (tf_AFCDirectoryClose)GetProcAddress(iTunesDll, "AFCDirectoryClose");
                AFCFileInfoOpen = (tf_AFCFileInfoOpen)GetProcAddress(iTunesDll, "AFCFileInfoOpen");
                AFCKeyValueRead = (tf_AFCKeyValueRead)GetProcAddress(iTunesDll, "AFCKeyValueRead");
                AFCKeyValueClose = (tf_AFCKeyValueClose)GetProcAddress(iTunesDll, "AFCKeyValueClose");
                AFCFileRefOpen = (tf_AFCFileRefOpen)GetProcAddress(iTunesDll, "AFCFileRefOpen");
                AFCFileRefClose = (tf_AFCFileRefClose)GetProcAddress(iTunesDll, "AFCFileRefClose");
                AFCFileRefRead = (tf_AFCFileRefRead)GetProcAddress(iTunesDll, "AFCFileRefRead");
                AFCFileRefWrite = (tf_AFCFileRefWrite)GetProcAddress(iTunesDll, "AFCFileRefWrite");
                AFCRemovePath = (tf_AFCRemovePath)GetProcAddress(iTunesDll, "AFCRemovePath");
                AFCDirectoryCreate = (tf_AFCDirectoryCreate)GetProcAddress(iTunesDll, "AFCDirectoryCreate");
                AFCRenamePath = (tf_AFCRenamePath)GetProcAddress(iTunesDll, "AFCRenamePath");
                AFCGetFileInfo = (tf_AFCGetFileInfo)GetProcAddress(iTunesDll, "AFCGetFileInfo");
        } else
                throw "iTunesMobileDevice.dll could not be loaded";
}

void notification(struct am_device_notification_callback_info *info)
{
        unsigned int msg = info->msg;
        
        //  Need more verbosity here.
        printf("[NOTIF] %d\n", msg);
        switch (msg) {
                case ADNCI_MSG_CONNECTED:
                        if (iPodState == IPOD_STATE_UNCONNECTED) {
                                iPodDev = info->dev;
                                iPodState = IPOD_STATE_CONNECTED;
                                printf("iPod is connected\n");
                        } else {
                                // Was in another state before, something bad must have happened
                        }
                        break;
                case ADNCI_MSG_DISCONNECTED:
                        if (iPodState == IPOD_STATE_CONNECTED) {
                                iPodState = IPOD_STATE_UNCONNECTED;
                                iPodDev = NULL;
                                iPodAFC = NULL;
                                iPodConnection = NULL;
                                printf("iPod is disconnected\n");
                        }
                        break;
                default:
                        break;
        }
}

void connect()
{
        struct am_device_notification *notif; 
        mach_error_t ret;
        int timeout;
        struct am_device *pDev;
        
        printf("Trying to connect iPod...\n");
        ret = AMDeviceNotificationSubscribe(notification, 0, 0, 0, ¬if);
        printf("[RET]AMDeviceNotificationSubscribe() = %d\n", ret);
        for (timeout = 0; (timeout < 1000) && (iPodState != IPOD_STATE_CONNECTED); timeout++) {
                Sleep(10);
        }
        if (iPodState != IPOD_STATE_CONNECTED)
                throw "Could not find iPod";
        pDev = (struct am_device *)iPodDev;
        // This part could possibly move to the notification routine:
        ret = AMDeviceConnect(pDev);
        printf("[RET]AMDeviceConnect() = %d\n", ret);
        if (ret) {
                // We don't handle the restore mode
                throw "Could not connect iPod";
        }
        ret = AMDeviceIsPaired(pDev);
        printf("[RET]AMDeviceIsPaired() = %d\n", ret);
        if (!ret)
                throw "Could not pair iPod";
        ret = AMDeviceValidatePairing(pDev);
        printf("[RET]AMDeviceValidatePairing() = %d\n", ret);
        if (ret)
                throw "Could not validate iPod pairing";
        ret = AMDeviceStartSession(pDev);
        printf("[RET]AMDeviceStartSession() = %d\n", ret);
        if (ret)
                throw "Could not start session";
        iPodAFCName = AMSVC_AFC2;
        ret = AMDeviceStartService(pDev, iPodAFCName, &iPodAFC, NULL);
        printf("[RET]AMDeviceStartService() = %d\n", ret);
        if (ret) {
                // Not jailbroken, tries to connect to the standard name
                iPodAFCName = AMSVC_AFC;
                ret = AMDeviceStartService(pDev, iPodAFCName, &iPodAFC, NULL);
                printf("[RET]AMDeviceStartService() = %d\n", ret);
                if (ret)
                        throw "Could not start AFC service";
        }
        ret = AFCConnectionOpen(iPodAFC, 0, &iPodConnection);
        printf("[RET]AFCConnectionOpen() = %d\n", ret);
        if (ret)
                throw "Could not start AFC connection";
        printf("Ready\n");
        iPodState = IPOD_STATE_READY;
}

void close()
{
        if (piTunesMobileDevicePath)
                delete piTunesMobileDevicePath;
        if (iTunesDll)
                FreeLibrary(iTunesDll);
        if (iPodState == IPOD_STATE_READY) {
                /* Doesn't work:
                mach_error_t ret;
                int timeout;
                ret = AMDeviceStopSession((struct am_device *)iPodDev);
                printf("[RET]AMDeviceStopSession() = %d\n", ret);
                ret = AMDeviceDisconnect((struct am_device *)iPodDev);
                printf("[RET]AMDeviceDisconnect() = %d\n", ret);
                for (timeout = 0; (timeout < 500) && (iPodState != IPOD_STATE_UNCONNECTED); timeout++) {
                        Sleep(10);
                }
                if (iPodState != IPOD_STATE_UNCONNECTED)
                        throw "Could not disconnect iPod";
                */
        }
}

int iPodCmdFileInfo(char *remoteFile)
{
        mach_error_t ret;
        struct afc_dictionary *pInfo;
        char *pKey, *pVal;
        unsigned int size = 0;
        
        //ret = AFCGetFileInfo(iPodConnection, remoteFile, &pInfo, &size);
        //printf("[RET]AFCGetFileInfo() = %d\n[RES]\tpInfo=%08X size=%08X\n", ret, (int)pInfo, size);

        ret = AFCFileInfoOpen(iPodConnection, remoteFile, &pInfo);
        printf("[RET]AFCFileInfoOpen() = %d\n", ret);
        if (ret) {
                printf("%s doesn't exist\n", remoteFile);
                return ret;
        }
        ret = AFCKeyValueRead(pInfo, &pKey, &pVal);
        while(pKey || pVal) {
                printf("[RES]\t%s = %s\n", (pKey ? pKey : "<empty>"), (pVal ? pVal : "<empty>"));
                AFCKeyValueRead(pInfo, &pKey, &pVal);
        }
        AFCKeyValueClose(pInfo);
        return 0;
}

int iPodCmdLs(char *remotePath)
{
        struct afc_directory *pDir;
        char *pEntry;
        mach_error_t ret;
        CString filename;
        
        ret = AFCDirectoryOpen(iPodConnection, remotePath, &pDir);
        printf("[RET]AFCDirectoryOpen() = %d\n", ret);
        if (ret) {
                printf("%s doesn't exist\n", remotePath);
                return ret;
        }
        while(1) {
                ret = AFCDirectoryRead(iPodConnection, pDir, &pEntry);
                if (ret) {
                        printf("[RET]AFCDirectoryRead() = %d\n", ret);
                        break;
                }
                if (!pEntry)
                        break;
                printf("[RES] %s\n", pEntry);
                /*
                filename = remotePath;
                if (filename.Right(1).Compare("/"))
                        filename += "/";
                filename += pEntry;
                iPodCmdFileInfo(filename.GetBuffer());
                */
        }
        ret = AFCDirectoryClose(iPodConnection, pDir);
        printf("[RET]AFCDirectoryClose() = %d\n", ret);
        return ret;
}

int iPodCmdFileRead(char *remoteFile)
{
        mach_error_t ret;
        struct afc_dictionary *pInfo;
        char *pKey, *pVal;
        unsigned int size = 0, total = 0, len;
        afc_file_ref handle;
        unsigned char buffer[16];
        unsigned int i;

        // Gets the file size
        ret = AFCFileInfoOpen(iPodConnection, remoteFile, &pInfo);
        if (ret) {
                printf("%s doesn't exist\n", remoteFile);
                return ret;
        }
        ret = AFCKeyValueRead(pInfo, &pKey, &pVal);
        while(pKey || pVal) {
                printf("\t%s = %s\n", pKey, pVal);
                if (pKey == NULL || pVal == NULL)
                        break;
                if (!stricmp(pKey, "st_size")) {
                        size = atoi(pVal);
                        break;
                }
                AFCKeyValueRead(pInfo, &pKey, &pVal);
        }
        AFCKeyValueClose(pInfo);
        if (size == 0) {
                printf("%s has a null size\n", remoteFile);
                return 1;
        }

        // Opens the file for reading
        ret = AFCFileRefOpen(iPodConnection, remoteFile, AFC_FILEMODE_READ, 0, &handle);
        if (ret != MDERR_OK) {
                printf("[RET]AFCFileRefOpen() = %d\n", ret);
                return ret;
        }
        
        // Gets the contents by chunks
        while (total < size) {
                len = min(size - total, sizeof(buffer));
                ret = AFCFileRefRead(iPodConnection, handle, buffer, &len);
                if (ret != MDERR_OK) {
                        printf("[RET]AFCFileRefRead() = %d\n", ret);
                        break;
                }
                if (!len)
                        break;
                for (i = 0; i < len; i++)
                        printf("%02X ", buffer[i]);
                printf("%*s| ", (sizeof(buffer)-len)*3+1, " ");
                for (i = 0; i < len; i++)
                        printf("%c", buffer[i] >= 32 ? buffer[i] : ' ');
                putchar('\n');
                total += len;
        }
        AFCFileRefClose(iPodConnection, handle);
        return 0;
}

void test()
{
        char buffer[512];
        while(1) {
                printf("> ");
                gets(buffer);
                if (!*buffer)
                        break;
                if (buffer[0] == '?') {
                        printf("File info on %s:\n", buffer+1);
                        iPodCmdFileInfo(buffer+1);
                } else if (!strncmp(buffer, "r ", 2)) {
                        printf("Reading file %s:\n", buffer+2);
                        iPodCmdFileRead(buffer+2);
                } else {
                        printf("Directory contents of %s:\n", buffer);
                        iPodCmdLs(buffer);
                }
        }
}

void main(int argc, char *argv[])
{
        try {
                init();
                connect();
                test();
                close();
        }
        catch(char *message) {
                printf("Error: %s\nAborting.\n", message);
                close();
                exit(1);
        }
}

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
开头说明:此源码并未开发完成,只是个demo。而且写法很随意(我也想搞懂iTunesMobileDevice.dll调用流程,奈何太繁琐),不喜勿喷! 1 .好像iTunesMobileDevice.dll对 太新太旧的设备读取某些功能存在着有误的问题(Iphone11系列读取颜色会返回空值0/1 ,Iphone5s的CPU架构号也会返回错误),其他没测可自行尝试 2 .整体流程是:1.调用 AMDeviceNotificationSubscribe ,函数用来及时反馈插拔设备消息                     2.调用 AMDeviceConnect ,函数用来连接设备( 到此步即可直接去获取一些基础设备信息,上图就是 )                     3.调用 AMDeviceIsPaired ,函数用来判断配对                     4.调用 AMDeviceValidatePairing ,函数用来验证配对,此函数也可做为(锁屏/信任)等判断依据                     5.调用 AMDeviceStartSession ,函数来创建会话通道,完成此步可做一些文件/高级信息(序列号/硬盘/内存/电池等) 系列操作                     注:整体流程的 成功 返回值均在源码中备注, 错误 的返回值过多没做整理,可以到CSDN( 连接在下方 )自行查询错误原因! 3 .源代码共调用了两个模块 精E模块(请自行替换),Ecallback(已开源,一搜就有源码,作者: 星锋工作室-东灿 ) 4 . 源代码或多或少都加了一些注释,功能不多虽然不多, 源代码依赖Itunes( iTunesMobileDevice.dll和CoreFoundation.dll )请自备! 5 .文献: 部分功能调用方法及声明(英文):https://bitbucket.org/tristero/mobiledeviceaccess/src/4bf38336bd6d7f5ffb23c8f331aed9eff5c720c0/MobileDevice.h?at=default#MobileDevice.h-115 部分服务/功能名查询:https://bitbucket.org/tristero/mobiledeviceaccess/src/4e3e281de0c38b7f9f053927d45a756b28134bd3/deviceValueForDomain.md?at=defaultfileviewer=file-view-default  错误返回值含义查询 :https://blog.csdn.net/Cinnazgc/article/details/858609 94
前言 本程序基于东灿的异常调试模块5.2部分版权信息如下: 1. 该模块功能是补足易错误管理器无法捕捉的异常。 2. 本模块可以截获许多异常,比如内存读写错误,非法汇编指令,算术异常,其他异常等等。 3. 本模块拥有try/catch的异常处理模型,支持多层嵌套,支持向上处理。允许用户自己设定跳过致命异常。 4. 并且支持易原错误管理器接管 5. 建议配合配套工具使用更加好用 正文: 程序原使用模块: 模块名 作者或工作室 用处 高级ApiHookEx 作者未知 HookApi,在模块中用于Hook:CreateRemoteThreadEx和CreateRemoteThread AppThreadVar 作者未知 进程内变量,线程内变量。可以在任意ec模块,任意DLL调用,全部共享 callstack 作者未知 函数调用堆栈跟踪。 disasm 作者未知 支持MMX/SSE/SSE2/SSE3/3DNow取CPU指令长度 GetThisModuleHandle 作者未知 取出当前ModuleHandle e-try/catch(即new_SEH) 星锋工作室-东灿 SEH结构体等 精简内容 简化模块(即多模块合一,但互相不影响,并且删去了重复的API,数据类型等) 增加注释:对于代码,追加了更多的注释。 部分原理理解(很多都是本人自己的理解,可能不对,若有错误,欢迎指出):AppThreadVar:工作核心:tls线程局部存储(缩写为TLS。进程中的全局变量与函数内定义的静态(static)变量,是各个线程都可以访问的共享变量。在一个线程修改的内存内容,对所有线程都生效。这是一个优点也是一个缺点。说它是优点,线程的数据交换变得非常快捷。说它是缺点,一个线程死掉了,其它线程也性命不保; 多个线程访问共享数据,需要昂贵的同步开销,也容易造成同步相关的BUG。) 部分功能:它主要是为了避免多个线程同时访存同一全局变量或者静态变量时所导致的冲突,尤其是多个线程同时需要修改这一变量时。为了解决这个问题,我们可以通过TLS机制,为每一个使用该全局变量的线程都提供一个变量值的副本,每一个线程均可以独立地改变自己的副本,而不会和其它线程的副本冲突。从线程的角度看,就好像每一个线程都完全拥有该变量。而从全局变量的角度上来看,就好像一个全局变量被克隆成了多份副本,而每一份副本都可以被一个线程独立地改变。 用途:动态TLS和静态TLS这两项技术在创建DLL的时候更加有用,这是因为DLL通常并不知道它们被链接到的应用程序的结构是什么样的。1. 如果应用程序高度依赖全局变量或静态变量,那么TLS可以成为我们的救生符。因而最好在开发中最大限度地减少对此类变量的使用,更多的依赖于自动变量(栈上的变量)和通过函数参数传入的数据,因为栈上的变量始终都是与某个特定的线程相关联的。如果不使用此类变量,那么就可以避免使用TLS。2. 但是在编写应用程序时,我们一般都知道自己要创建多少线程,自己会如何使用这些线程,然后我们就可以设计一些替代方案来为每个线程关联数据,或者设计得好一点的话,可以使用基于栈的方法(局部变量)来为每个线程关联数据 TLS分动态和静态,AppThreadVar我认为是动态TLS(调用了TlsAlloc函数) 相当于静态变量(我也只能理解到这里了) ApiHookEx和大部分的APIHook工作原理类似 e-try/catch说实话,这个模块我几乎不能理解原理(能理解的基本上是一些Api,比如MiniDumpWriteDump) 这个可能也是用于捕获异常,而且比较神奇的是,这个捕获到异常以后不会导致程序的退出,而是程序能够正常运行! 可以抛出自定义数值异常,由catch_int/catch_eq_int接收 可以抛出最后一次异常,让上层错误处理器处理 部分实例: try/catch测试:try/catch结果: 部分崩溃代码测试: 结果部分: 程序可以自己写出DMP文件: 更多功能请自己测试!!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值