本文介绍了我如何在C#上写一个签名的包装C ++方法有双向间接其参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一个dll的包装。 dll有一个方法,其签名类似于:

  unsigned long aMethod(char ** firstParameter,char ** secondParameter) 

aMethod返回指向所有参数的字符串指针。



我在google搜索一个教程,让我了解如何写在C#上的签名,所以框架可以做编组过程。



如何写?
你知道有关这个主题的任何教程,书或文档吗?

解决方案

p>

签名必须是这样:

  [DllImport(aDll。 dll,CallingConvention = CallingConvention.Cdecl,
CharSet = CharSet.Auto)]
static unsafe extern ulong aMethod(ref IntPtr firstParameter,
ref IntPtr secondParameter);

参数应声明为:

  IntPtr firstParameter = new IntPtr(); 
IntPtr secondParameter = new IntPtr();

调用操作如下:

  aMethod(ref firstParameter,ref secondParameter); 

与字符串相关的编组和取消组合如下:

  Marshal.PtrToStringAnsi(firstParameter)
Marshal.PtrToStringAnsi(secondParameter)

显然,这个编组是基于dll的API约定来选择的。



我不知道,但所有的问题他们源自 Charset CallingConvention 选项...


I'm writing a wrapper for a dll. The dll has a method whose signature resembles the following:

unsigned long aMethod(char **firstParameter, char **secondParameter))

aMethod returns string pointers to all parameters.

I've searching at google for a tutorial to give me insight on how to write the signature on C# so the framework can do the marshalling process.

How can it be written?Do you know about any tutorial, book or documentation on this subject?

解决方案

I got the answer:

The signature has to be like this:

[DllImport(aDll.dll, CallingConvention = CallingConvention.Cdecl, 
    CharSet = CharSet.Auto)]
static unsafe extern ulong aMethod(ref IntPtr firstParameter, 
    ref IntPtr secondParameter);

Parameters should be declared as here:

IntPtr firstParameter = new IntPtr();
IntPtr secondParameter = new IntPtr();

And invocation is done as:

aMethod(ref firstParameter, ref secondParameter);

Marshalling and unmarshalling related to the strings as here:

Marshal.PtrToStringAnsi(firstParameter)
Marshal.PtrToStringAnsi(secondParameter)

Obviously, this marshalling has been selected based on dll´s API conventions.

I´m not sure after all, but all the problems had they source in the Charset and CallingConvention options...

这篇关于我如何在C#上写一个签名的包装C ++方法有双向间接其参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 14:31