刚去的新公司分配了我一个项目需求,将PPT文件(包括*.ppt和*.pptx)转换成多张png图片。由于以前只有native C++的经验,在网上逛了多圈后,发现都是使用C#实现这个功能的,被这个需求折磨了几天后,今天终于把这个问题解决了。所以在此记录下解决这个项目需求的总结。
C#的程序代码在非托管的C++环境中使用有三种方式:1.平台调用技术(P/Invoke)。2. C++ Interop。3.COM Interop。
我使用的是C++ Interop方法,也就是使用C++/CLI语法将C#的动态库封装成C++/CLI的动态库,然后在native C++程序中就可以调用C++/CLI的动态库了。以下是我的一些做法:
1.我使用的IDE工具是VS2015,从网上找到了一份PPT转化成图片的C#动态库。链接如下:https://www.cnblogs.com/kksguijiao/articles/9099103.html
将这份C#的代码进行了一些简单的修改符合了我的需求。修改后的代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Aspose.Cells;
using ESBasic;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
//using System.Threading.Tasks;
// 这个是去掉水印的
using System.Runtime.CompilerServices;
using System.Reflection;
using System.Xml;
namespace LicenseHelper
{
public static class ModifyInMemory
{
private static string AsposeList = "Aspose.3D.dll, Aspose.BarCode.dll, Aspose.BarCode.Compact.dll, Aspose.BarCode.WPF.dll, Aspose.Cells.GridDesktop.dll, Aspose.Cells.GridWeb.dll, Aspose.CAD.dll, Aspose.Cells.dll, Aspose.Diagram.dll, Aspose.Email.dll, Aspose.Imaging.dll, Aspose.Note.dll, Aspose.OCR.dll, Aspose.Pdf.dll, Aspose.Slides.dll, Aspose.Tasks.dll, Aspose.Words.dll";
public static void ActivateMemoryPatching()
{
Assembly[] arr = AppDomain.CurrentDomain.GetAssemblies();
foreach (Assembly assembly in arr)
{
if (AsposeList.IndexOf(assembly.FullName.Split(',')[0] + ".dll") != -1)
ActivateForAssembly(assembly);
}
AppDomain.CurrentDomain.AssemblyLoad += new AssemblyLoadEventHandler(ActivateOnLoad);
}
private static void ActivateOnLoad(object sender, AssemblyLoadEventArgs e)
{
if (AsposeList.IndexOf(e.LoadedAssembly.FullName.Split(',')[0] + ".dll") != -1)
ActivateForAssembly(e.LoadedAssembly);
}
private static void ActivateForAssembly(Assembly assembly)
{
MethodInfo miLicensed1 = typeof(ModifyInMemory).GetMethod("InvokeMe1", BindingFlags.NonPublic | BindingFlags.Static);
MethodInfo miLicensed2 = typeof(ModifyInMemory).GetMethod("InvokeMe2", BindingFlags.NonPublic | BindingFlags.Static);
MethodInfo miEvaluation = null;
Dictionary<string, MethodInfo> miDict = new Dictionary<string, MethodInfo>()
{
{"System.DateTime" , miLicensed1},
{"System.Xml.XmlElement", miLicensed2}
};
Type[] arrType = null;
bool isFound = false;
int nCount = 0;
try
{
arrType = assembly.GetTypes();
}
catch (ReflectionTypeLoadException err)
{
arrType = err.Types;
}
foreach (Type type in arrType)
{
if (isFound) break;
if (type == null) continue;
MethodInfo[] arrMInfo = type.GetMethods(BindingFlags.NonPublic | BindingFlags.Static);
foreach (MethodInfo info in arrMInfo)
{
if (isFound) break;
try
{
string strMethod = info.ToString();
if ((strMethod.IndexOf("(System.Xml.XmlElement, System.String)") > 0) && (miDict.ContainsKey(info.ReturnType.ToString())))
{
miEvaluation = info;
MemoryPatching(miE