简体   繁体   English

如何从C#中的非托管DLL文件检索XML文件流

[英]How to retrieve XML file stream from unmanaged DLL file in C#

I've received an unmanaged DLL file from a client. 我从客户端收到了一个非托管的DLL文件。 This DLL file has an important method that returns an XML stream. 该DLL文件具有返回XML流的重要方法。 I am not sure how to 'catch' this XML stream because the DLL file is pretty much undocumented. 我不确定如何“捕获”此XML流,因为该DLL文件几乎没有记录。 This is what I know: 这是我所知道的:

  1. I know how to call an unmanaged DLL function, however I am unfamiliar with structs and how to retrieve data from the function using a struct. 我知道如何调用非托管DLL函数,但是我不熟悉结构以及如何使用结构从函数中检索数据。
  2. The 'documentation' says: You can obtain an XML stream handle from the function OpenXmlBridge “文档”说:您可以从函数OpenXmlBridge获取XML流句柄。

This is where I am stuck at right now: 这就是我现在停留的地方:

[DllImport("UnmanagedDllFile.DLL")]
public static extern void OpenXmlBridge();

I know that this returns void when executed but I am at a loss right here. 我知道执行该操作后将返回void ,但我对此感到茫然。 I also know that this function exeists because when I replace OpenXmlBridge with some random stuff, I get errors. 我也知道这个函数的存在是因为当我用一些随机的东西替换OpenXmlBridge时,我得到了错误。 So until now, I guess I've been on the right track. 所以直到现在,我猜我一直在正确的轨道上。 I haven't been able to find out what to do exactly from here. 我一直无法从这里找出要做什么。

I anyone could be able to point me into the right direction so I can retrieve the XML stream handle from this function. 我每个人都可以指出正确的方向,这样我就可以从此函数检索XML流句柄。 Any help or tips are greatly appreciated. 任何帮助或提示,我们将不胜感激。

You can use UnmanagedMemoryStream class. 您可以使用UnmanagedMemoryStream类。 In that case your wrapper whould look like: 在这种情况下,您的包装器将如下所示:

[DllImport("UnmanagedDllFile.DLL")]
public static extern IntPtr OpenXmlBridge();

And the code to access it would be something like: 和访问它的代码将是这样的:

var length = 0 //stream length here
var pointer = NativeWrapper.OpenXmlBridge();
using(var ms = new UnmanagedMemoryStream((Byte*)pointer, length){
  var xDocument = new XmlDocument();
  xDocument.Load(ms); 
  //process document
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM