设为首页收藏本站Access中国

Office中国论坛/Access中国论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

返回列表 发新帖
查看: 5377|回复: 4
打印 上一主题 下一主题

【原创】VSTO加载过程分析

[复制链接]
跳转到指定楼层
1#
发表于 2014-2-21 14:47:57 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
一、通用过程
1MarshalHelperConvertObjectToVariant
简述:将对象转换为Variant结构
调用:System.Variant.MarshalHelperConvertObjectToVariant
参数:o=object
输出:Variant对象(DomainCreatorString[]ExecuteCustomization

说明:
变体类型 VARIANT 用于com 设计,初衷是为达到跨语言的目标。之所以称之为变体类型是因为它的类型不是固定的,基本上可以表示COM里面的任何基本类型和常见接口指针。VARIANT类型就是一个结构体,该结构体中有一个成员是表示该变量到底是个什么变量,实际的数据是存在结构体的一个联合体成员中的。此处用处不大,主要用于跨语言设计。
Object类:
DomainCreator

发生于:Assembly.CreateInstance

String[]
发生于:CreateCustomizationDomain.CreateCustomizationDomainInternal
调用于:DomainCreator.GetAssemblyDataFromManifests
定位于:interopServiceProvider.SetProperty("RibbonTypes", list.ToArray());

DomainCreator.ExecuteCustomization
发生于:CreateCustomizationDomain.CreateCustomizationDomainInternal
调用于:ExecuteCustomization.ctor
定位于:
this._hostServiceProvider.SetProperty("VstoCustomizationType",);


2、GetCOMIUnknown


本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏1 分享分享 分享淘帖 订阅订阅
2#
 楼主| 发表于 2014-2-21 14:50:58 | 只看该作者
二、准备过程
1vstoee
说明:vstoee.dll动态链接库
加载相应版本的 VisualStudio Tools for Office Runtime,当用户打开文档级自定义项或启动外接程序时,Office 应用程序将调入 VSTOEE.dll 以执行加载 Visual Studio Tools for Office Runtime 所需的任务。
调用函数:
Private Declare Function CreateVstoSolutionMetadataKeyLib "vstoee.dll"
(
ByVal arg1 As Long,
ByVal arg2 As Long,
ByVal arg3 As Long,
ByVal arg4 As Long
) As Long
Private Declare Function GetLatestVstoRuntimeVersionLib "vstoee.dll"
(
ByVal arg1 As Long
) As Long
Private Declare Function GetValidCompatibleFrameworkLib "vstoee.dll"
(
ByVal arg1 As Long,
ByVal arg2 As Long
) As Long
Private Declare Function GetVstoSolutionMetadataLib "vstoee.dll"
(        
ByVal arg1 As Long,
ByVal arg2 As Long
) As Long
Private Declare Function GetVstoSolutionRuntimeVersionWLib "vstoee.dll"
(
ByVal arg1 As Long,
ByVal arg2 As Long,
ByVal arg3 As Long,
ByVal arg4 As Long,
ByVal arg5 As Long
) As Long
Private Declare Function GetVstoSolutionVersionWLib "vstoee.dll"
(
ByVal arg1 As Long,
ByVal arg2 As Long,
ByVal arg3 As Long
) As Long
Private Declare Function InstallSolution Lib"vstoee.dll"
(
ByVal arg1 As Long,
ByVal arg2 As Long,
ByVal arg3 As Long
) As Long
Private Declare Function InstallVstoSolutionWLib "vstoee.dll"
(
ByVal arg1 As Long,
ByVal arg2 As Long,
ByVal arg3 As Long,
ByVal arg4 As Long
) As Long
2VSTOLoader
说明:VSTOLoader.dll动态链接库
A)它为每个解决方案程序集创建一个应用程序域。
B)它执行一组安全性检查以验证解决方案程序集是否有运行的权限。
C)它加载解决方案所需的 Office .NET Framework 扩展的版本。
D)它实现 IDTExtensibility2 接口
IDTExtensibility2 Microsoft Office 应用程序的所有外接程序都必须实现的一个 COM 接口。
此接口定义应用程序为与外接程序通信而调用的方法。(相当于为VSTO类型的程序自动实现一个)
E)它实现 IManagedAddin 接口。
调用函数:
HRESULT EnsureVSTOComponent( IVSTProject *pProject );
Private Declare Function EnsureVSTOComponent Lib"VSTOLoader.dll"
(
ByVal arg1 As Long
) As Long
3、启动前界面
01】插件状态异常

02】开始创建过程

里程碑:从此处开始进入正题

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
3#
 楼主| 发表于 2014-2-21 14:56:33 | 只看该作者
本帖最后由 faunus 于 2014-2-21 15:00 编辑

二、创建过程
1、创建AppDomain对像
调用:System.AppDomain.CreateDomain
参数:
friendlyName="D:\ProgramFiles\Microsoft Office\Office14\powerpnt.exe"
Evidence securityInfo=
AppDomainSetup=info
PermissionSet grantSet=
params StrongName[] fullTrustAssemblies=
调用:
InternalCreateDomain
   -->nCreateDomain
      -->extern AppDomain nCreateDomain
说明:
AppDomain对象非创建产生("NotSupported_Constructor")...
2、加载AppDomain
调用:System.AppDomain.SetupDomain
参数:
allowRedirects= true
path="D:\ProgramFiles\Microsoft Office\Office14\"
configFile="D:\ProgramFiles\Microsoft Office\Office14\powerpnt.exe.config"
propertyNames=null
propertyValues=
过程:
PartialTrustVisibleAssemblies调用过程中通过GetNativeHandle获得AppDomain地址。
-->创建AppDomainSetup

-->调用InitializeCompatibilityFlags,
-->调用InitializeDomainSecurity-->调用SetupDomainSecurity
1、由Appdomain加载Runtime程序集
调用:System.Appdomain.Load
参数:assemblyString=Microsoft.VisualStudio.Tools.Office.Runtime,Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
返回:Assembly

说明:(重要)
加载符合当前版本的Runtime环境。其中DefinedTypes包含引用类型。
1
调用:System.Reflection.ICustomQueryInterface.GetInterface
参数:
Iid={17156360-2f1a-384a-bc52-fde93c215c5b}
ppv=0【out】

CreateInstanceSlow
InternalGetSatelliteAssembly
初始化FrameWork,选择地区环境

1、由Runtime程序集创建DomainCreator对象
调用:Assembly.CreateInstance
参数:typeName=”Microsoft.VisualStudio.Tools.Office.Runtime.DomainCreator”
输出:DomainCreator对象

说明:
DomainCreator对象实例将被用来创建AddinAppdomain,是最关键的类。
其中最为重要的两个过程,前者调用后者:
IDomainCreator.CreateCustomizationDomain
CreateCustomizationDomainInternal

2、调用DomainCreator创建AppDomain对象
调用:
Microsoft.VisualStudio.Tools.Office.Runtime
.DomainCreator.IDomainCreator.CreateCustomizationDomain
参数:
    solutionLocation="file:///d:/我的文档/visual studio 2010/Projects/P10/P10/bin/Debug/"
    manifestName="P10.vsto|vstolocal"
    documentName=null
    showUIDuringDeployment=ture
    hostServiceProvider=14485477PowerPoint句柄】
    executor=0out,插件执行地址】


本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
4#
 楼主| 发表于 2014-2-21 14:56:59 | 只看该作者
本帖最后由 faunus 于 2014-2-21 14:58 编辑

2-1、创建前准备
调用:IDomainCreator.CreateCustomizationDomain
过程:
【01】参始化代码工厂CodeMarkers
调用:Microsoft.Internal.Performance.CodeMarkers.InitPerformanceDll
参数:CodeMarkerApp=CodeMarkerApp.VSTORTPERF
参数:rootKey="Software\Microsoft\VisualStudio\10.0"
关键:检查注册表【Software\Microsoft\VisualStudio\10.0\Performance】,反回
【02】检查以下参数
hostServiceProvider、solutionLocation、manifestName
【03】进入正题,调用CreateCustomizationDomainInternal
2-2、开始创建
调用:CreateCustomizationDomainInternal
参数:(增加)
     useFastPath=false

输出:AppDomain

说明:
创建完成后,返回UI线程(主线程)

4、创建COM指针
调用:GetCOMIUnknown
参数:fIsBeingMarshalled=false
输出:空COM指针
说明:备用

5、初始化CustomizationWindow
调用:
Microsoft.VisualStudio.Tools.Office.Runtime.
DomainCreator.ExecuteCustomization.IOleServiceProvider.QueryService
INativeSupport ICustomizationWindow.NativeSupport
输入:
rsid={e0a17da6-1c4c-434d-bf26-0f47dbca05d2}
riid={e0a17da6-1c4c-434d-bf26-0f47dbca05d2}
retVal=0【out,返回地址】
说明:
重写来自IServiceProvider 的接口,提新新的服务查询
Microsoft.VisualStudio.Tools.Applications.Interop.IAppInfo:应用接口
Microsoft.VisualStudio.Tools.Office.Runtime.Interop.IPersistAppInfo:原始应用接口
Microsoft.VisualStudio.Tools.Office.Runtime.Interop.ICustomizationWindow:应用接口

6、返回CustomizationWindow接口地址
调用:
Microsoft.VisualStudio.Tools.Office.Internal.
CustomizationWindow.ICustomizationWindow.NativeSupport

返回:INativeSupport


7、动行Addin
调用:
Microsoft.VisualStudio.Tools.Office.Runtime.
DomainCreator.ExecuteCustomization.IExecuteCustomization2.LoadEntryPoints
参数:serviceProvider=150399324【指针】




本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
5#
 楼主| 发表于 2014-2-21 14:59:18 | 只看该作者
第一章:创建AppDomain
1、创建AppDomain对象
【Enter】
-->  System.AppDomain
public static AppDomain CreateDomain
(
string friendlyName【??】
)
【OUT】AppDomain

【说明】
AppDomain对象非创建产生("NotSupported_Constructor")...
由VSTOLoader发起调用。?创建域用于准备PowerPoint的调用。
【CallList】
LV_1
public static AppDomainCreateDomain(string friendlyName)
LV_1_1
public static AppDomainCreateDomain
(string friendlyName, System.Security.Policy.EvidencesecurityInfo, AppDomainSetup info)
LV_1_1_1
public static AppDomainCreateDomain
(string friendlyName, System.Security.Policy.EvidencesecurityInfo, AppDomainSetup info)
LV_1_1_1_1
internal static AppDomain InternalCreateDomain
(string friendlyName,System.Security.Policy.Evidence securityInfo, AppDomainSetup info)
LV_1_1_1_1_A:创建AppDomainSetup
--> info = new AppDomainSetup();
LV_1_1_1_1_B:获得TargetFrameworkName
-->info.TargetFrameworkName= CurrentDomain.GetTargetFrameworkName();
LV_1_1_1_1_C:创建securityInfo
-->  new SecurityPermission(SecurityPermissionFlag.ControlEvidence).Demand();
-->CheckDomainCreationEvidence(info, securityInfo);
LV_1_1_1_1_D:#创建AppDomain
-->  return nCreateDomain
(
friendlyName, info, securityInfo,
(securityInfo == null) ? CurrentDomain.InternalEvidence : null,
CurrentDomain.GetSecurityDescriptor()
);
LV_1_1_1_1_D_1:#创建AppDomain
-->  [MethodImpl(MethodImplOptions.InternalCall), SecurityCritical]
internal static extern AppDomain nCreateDomain
(
string friendlyName,
AppDomainSetup setup,
System.Security.Policy.EvidenceprovidedSecurityInfo,
System.Security.Policy.EvidencecreatorsSecurityInfo,
IntPtr parentSecurityDescriptor
);
注解>    MethodImpl属性指示函数为.NET内核原生实现,直接引用进类库使用。
1、创建并填充AppDomainSetup对象
【Enter】
-->  System.AppDomain
SetupDomain
(
boolallowRedirects, 【true】
string path, 【D:\Program Files\Microsoft Office\Office14\】
string configFile,【D:\Program Files\MicrosoftOffice\Office14\powerpnt.exe.config】
string[] propertyNames,【null】
string[]propertyValues【null】
)
【OUT】


本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|站长邮箱|小黑屋|手机版|Office中国/Access中国 ( 粤ICP备10043721号-1 )  

GMT+8, 2024-4-29 23:15 , Processed in 0.145003 second(s), 29 queries .

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表