帮助从DLL调用函数并处理返回值(无法编组返回值) [英] Help calling function from DLL and processing returned value (Can not marshal return value)

查看:99
本文介绍了帮助从DLL调用函数并处理返回值(无法编组返回值)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨再次,

我正试图从专有的DLL中调用函数,但事实证明这比我想象的更难。

我有这个由USPS用C编写的W32.DLL。他们没有提供

代码所以我只有文档。

我试图调用一个名为z4date的函数,根据文档,

将日期返回为YYYYMMDD中的8字节字符串。格式。

当我使用此代码运行它时,我已经写了无法编组返回

值。

我真正担心的是,如果我在使用这些函数时遇到麻烦,那么

会返回简单数据类型,我将如何与函数进行交互

返回用户定义的数据类型?


这是我到目前为止在课堂上写的:


... 。

使用System.Runtime.InteropServices;

命名空间ZM7

{

///< summary>

/// AMS的摘要说明。

///< / summary>

公共类AMS

{

[DllImport(" C:\\zm7 \\Developm \\DLL \\ W32.DLL")]

public static extern int z4open(); //来自dll的另一个函数

[DllImport(" C:\\zm7 \\Developm \\DLL \\W32.DLL")] / * Do我需要使用

DllImport用于我正在使用的每个功能? * /

public static extern byte [] z4date();

....


public byte [] getDate( )

{

Byte [] myDate;

myDate = new Byte [8];

myDate = z4date (); //函数调用我收到错误不能

元帅返回值

返回myDate();

}


感谢任何帮助。

Hi again,
I''m trying to call functions from a proprietary DLL but it''s turned out to
be more difficult than I thought.

I have this W32.DLL which was written in C by USPS. They don''t provide the
code so I only have the documentation.
I''m trying to call a function called z4date that, according to the docs,
returns the date as "an 8-byte character string in the "YYYYMMDD" format".
When I run it with this code I''ve written , I get "Can not marshal return
value".

My real concern is that, if I''m having trouble with these functions that
return simple data types, how am I going to interact with functions that
return user-defined data types?

This is what I''ve written in the class so far:

....
using System.Runtime.InteropServices;
namespace ZM7
{
/// <summary>
/// Summary description for AMS.
/// </summary>
public class AMS
{
[DllImport("C:\\zm7\\Developm\\DLL\\W32.DLL")]
public static extern int z4open(); //another function from the dll
[DllImport("C:\\zm7\\Developm\\DLL\\W32.DLL")] /* Do I need to use
DllImport for every fiunction I''m using? */
public static extern byte[] z4date();
....

public byte[] getDate()
{
Byte[] myDate;
myDate = new Byte[8];
myDate = z4date(); //function call where I receive error "Can not
marshal return value"
return myDate();
}

Any help is appreciated.

推荐答案

天使,


你会的不能这样做。它与你的定义没有合作的原因是因为

函数返回的指针具有不确定性。确实,文档声明

它返回8个字节,但代码中没有任何内容表明

就是这种情况。


为了解决这个问题,您应该将返回类型声明为IntPtr,

然后将其传递给Marshal

类的静态PtrToStringAnsi方法。 />

唯一需要考虑的是功能

分配内存并返回它。为防止泄漏,您必须处理IntPtr指向的内存
。你是怎么做的这是

取决于DLL如何分配它,并需要通过

P / Invoke进行另一次调用。


此外,你必须使用每个声明的属性。


希望这会有所帮助。

-

- 尼古拉斯Paldino [.NET / C#MVP]

- mv*@spam.guard.caspershouse.com


" Angel" <无>在留言中写道

news:u5 ************** @ tk2msftngp13.phx.gbl ...
Angel,

You will not be able to do this. The reason that it doesn''t work with
your definitions is because the pointer that is being returned by the
function is of an indeterminate nature. True, the documentation states that
it returns eight bytes, but there is nothing in the code that indicates that
is the case.

To get around this, you should declare your return type as an IntPtr,
and then pass that to the static PtrToStringAnsi method on the Marshal
class.

The only thing to be considerate of is the fact that the function
allocated memory and returned it. To prevent a leak, you will have to
dispose of the memory pointed to by the IntPtr. How you do this is
dependent on how the DLL allocates it, and will require another call through
P/Invoke.

Also, you do have to use the attribute for every declaration.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Angel" <none> wrote in message
news:u5**************@tk2msftngp13.phx.gbl...
嗨再次,
我有这个写的W32.DLL美国邮政在C中。他们没有提供
代码所以我只有文档。
我正在尝试调用一个名为z4date的函数,根据文档,
将日期返回为 ;YYYYMMDD中的8字节字符串。格式。
当我用这段代码运行它时,我已经写了,我得到了不能编组返回
值。

我真正担心的是,如果我在使用这些返回简单数据类型的函数时遇到问题,我将如何与返回用户定义数据类型的函数进行交互?

这就是到目前为止我在课堂上写过:

...
使用System.Runtime.InteropServices;
名称空间ZM7
{
// /< summary>
/// AMS的摘要说明。
///< / summary>
公共类AMS
{DllImport(" C:\\zm7 \\Developm \\DLL \\ W32.DLL")]
public static extern int z4open(); //来自dll的另一个函数
[DllImport(" C:\\zm7 \\Developm \\DLL \\W32.DLL")] / *我需要使用吗?
我正在使用的每个功能的DllImport? * /
public static extern byte [] z4date();
...

public byte [] getDate()
{
Byte [] myDate;
myDate = new Byte [8];
myDate = z4date(); //函数调用我收到错误不能
编组返回值
返回myDate();
}

任何帮助表示赞赏。
Hi again,
I''m trying to call functions from a proprietary DLL but it''s turned out to
be more difficult than I thought.

I have this W32.DLL which was written in C by USPS. They don''t provide the
code so I only have the documentation.
I''m trying to call a function called z4date that, according to the docs,
returns the date as "an 8-byte character string in the "YYYYMMDD" format".
When I run it with this code I''ve written , I get "Can not marshal return
value".

My real concern is that, if I''m having trouble with these functions that
return simple data types, how am I going to interact with functions that
return user-defined data types?

This is what I''ve written in the class so far:

...
using System.Runtime.InteropServices;
namespace ZM7
{
/// <summary>
/// Summary description for AMS.
/// </summary>
public class AMS
{
[DllImport("C:\\zm7\\Developm\\DLL\\W32.DLL")]
public static extern int z4open(); //another function from the dll
[DllImport("C:\\zm7\\Developm\\DLL\\W32.DLL")] /* Do I need to use
DllImport for every fiunction I''m using? */
public static extern byte[] z4date();
...

public byte[] getDate()
{
Byte[] myDate;
myDate = new Byte[8];
myDate = z4date(); //function call where I receive error "Can not
marshal return value"
return myDate();
}

Any help is appreciated.



嗨天使,

如果你有* z4date *返回一个字符串。您如何期望PInvoke

将其编组为一个字节数组

而不是将该函数声明为:

[DllImport(" C) :\\zm7 \\Developm \\DLL \\ W32.DLL")]

public static extern byte [] z4date();


尝试将其声明为:

[DllImport(" C:\\ zm7 \\Developm \\DLL \\ W32.DLL" )]

公共静态外部字符串z4date();


如果需要该字节数组,则应将字符串转换为该数组。我们

不能指望编组器从任何类型转换为我们想要的任何类型。

是的,您必须为每个导入的任何功能使用DllImport

来自非托管图书馆。

-

HTH

B \ rgds

100


" Angel" <无>在留言中写道

news:u5 ************** @ tk2msftngp13.phx.gbl ...
Hi Angel,
If your have *z4date* returning a string. How do you expect PInvoke to
marshal it as an array of bytes
Instead of declare the function as:
[DllImport("C:\\zm7\\Developm\\DLL\\W32.DLL")]
public static extern byte[] z4date();

try to declare it as:
[DllImport("C:\\zm7\\Developm\\DLL\\W32.DLL")]
public static extern string z4date();

If you need that byte array you should convert the string to that array. We
cannot expect the marshaler to convert from any to any type we want.
Yes, you have to use DllImport for each and any function that you import
from unmanaged library.
--
HTH
B\rgds
100

"Angel" <none> wrote in message
news:u5**************@tk2msftngp13.phx.gbl...
嗨再次,
我有这个写的W32.DLL美国邮政在C中。他们没有提供
代码所以我只有文档。
我正在尝试调用一个名为z4date的函数,根据文档,
将日期返回为 ;YYYYMMDD中的8字节字符串。格式。
当我用这段代码运行它时,我已经写了,我得到了不能编组返回
值。

我真正担心的是,如果我在使用这些返回简单数据类型的函数时遇到问题,我将如何与返回用户定义数据类型的函数进行交互?

这就是到目前为止我在课堂上写过:

...
使用System.Runtime.InteropServices;
名称空间ZM7
{
// /< summary>
/// AMS的摘要说明。
///< / summary>
公共类AMS
{DllImport(" C:\\zm7 \\Developm \\DLL \\ W32.DLL")]
public static extern int z4open(); //来自dll的另一个函数
[DllImport(" C:\\zm7 \\Developm \\DLL \\W32.DLL")] / *我需要使用吗?
我正在使用的每个功能的DllImport? * /
public static extern byte [] z4date();
...

public byte [] getDate()
{
Byte [] myDate;
myDate = new Byte [8];
myDate = z4date(); //函数调用我收到错误不能
编组返回值
返回myDate();
}

任何帮助表示赞赏。
Hi again,
I''m trying to call functions from a proprietary DLL but it''s turned out to
be more difficult than I thought.

I have this W32.DLL which was written in C by USPS. They don''t provide the
code so I only have the documentation.
I''m trying to call a function called z4date that, according to the docs,
returns the date as "an 8-byte character string in the "YYYYMMDD" format".
When I run it with this code I''ve written , I get "Can not marshal return
value".

My real concern is that, if I''m having trouble with these functions that
return simple data types, how am I going to interact with functions that
return user-defined data types?

This is what I''ve written in the class so far:

...
using System.Runtime.InteropServices;
namespace ZM7
{
/// <summary>
/// Summary description for AMS.
/// </summary>
public class AMS
{
[DllImport("C:\\zm7\\Developm\\DLL\\W32.DLL")]
public static extern int z4open(); //another function from the dll
[DllImport("C:\\zm7\\Developm\\DLL\\W32.DLL")] /* Do I need to use
DllImport for every fiunction I''m using? */
public static extern byte[] z4date();
...

public byte[] getDate()
{
Byte[] myDate;
myDate = new Byte[8];
myDate = z4date(); //function call where I receive error "Can not
marshal return value"
return myDate();
}

Any help is appreciated.



Stoitcho,


将返回值声明为字符串的问题是

在堆上为非托管领域中的字符串分配的内存现在将无法正确释放,并导致内存泄漏。

-

- Nicholas Paldino [.NET / C#MVP]

- mv*@spam.guard .caspershouse.com


Stoitcho Goutsev(100)[C#MVP]" < 10*@100.com>在消息中写道

新闻:eL ************** @ TK2MSFTNGP10.phx.gbl ...
Stoitcho,

The problem with declaring the return value as a string is that the
memory allocated on the heap for the string in the unmanaged realm will now
not be released correctly, and cause a memory leak.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Stoitcho Goutsev (100) [C# MVP]" <10*@100.com> wrote in message
news:eL**************@TK2MSFTNGP10.phx.gbl...
嗨天使,而不是将该函数声明为:
[DllImport(" C:\\ zm7 \\Developm \\ \\\DLL \\W32.DLL")]
public static extern byte [] z4date();

尝试将其声明为:
[DllImport(" ; C:\\zm7 \\Developm \\DLL \\ W32.DLL")]
public static extern string z4date();

如果你需要该字节数组,您应该将字符串转换为该数组。
我们不能指望封送程序从任何类型转换为我们想要的任何类型。

是的,您必须为每个以及从非托管库导入的任何函数使用DllImport。
-
HTH
B \ _ggds
100

天使 <无>在消息中写道
新闻:u5 ************** @ tk2msftngp13.phx.gbl ...
Hi Angel,
If your have *z4date* returning a string. How do you expect PInvoke to
marshal it as an array of bytes
Instead of declare the function as:
[DllImport("C:\\zm7\\Developm\\DLL\\W32.DLL")]
public static extern byte[] z4date();

try to declare it as:
[DllImport("C:\\zm7\\Developm\\DLL\\W32.DLL")]
public static extern string z4date();

If you need that byte array you should convert the string to that array. We cannot expect the marshaler to convert from any to any type we want.
Yes, you have to use DllImport for each and any function that you import
from unmanaged library.
--
HTH
B\rgds
100

"Angel" <none> wrote in message
news:u5**************@tk2msftngp13.phx.gbl...
嗨再次,
我'我试图从一个专有的DLL调用函数,但结果是
比我想象的要困难。

我有这个由USPS编写的W32.DLL 。他们没有提供
代码所以我只有文档。
我正在尝试调用一个名为z4date的函数,根据文档,
将日期返回为 ;YYYYMMDD中的8字节字符串。
格式。当我使用这段代码运行它时,我已经写了不能编组
返回值。

我真正担心的是,如果我遇到问题这些函数可以返回简单的数据类型,我将如何与返回用户定义的数据类型的函数进行交互?

这就是我在到目前为止的课程:

...
使用System.Runtime.InteropServices;
命名空间ZM7
{
///< summary>
/// AMS的摘要描述。
///< / summary>
公共类AMS
{DllImport(" C:\\ zm7) \\Developm \\DLL \\ W32.DLL")]
public static extern int z4open(); //来自dll的另一个函数
[DllImport(" C:\\zm7 \\Developm \\DLL \\W32.DLL")] / *我需要使用吗?
我正在使用的每个功能的DllImport? * /
public static extern byte [] z4date();
...

public byte [] getDate()
{
Byte [] myDate;
myDate = new Byte [8];
myDate = z4date(); //函数调用我收到错误可以
没有编组返回值
返回myDate();
}

任何帮助表示赞赏。
Hi again,
I''m trying to call functions from a proprietary DLL but it''s turned out to be more difficult than I thought.

I have this W32.DLL which was written in C by USPS. They don''t provide the code so I only have the documentation.
I''m trying to call a function called z4date that, according to the docs,
returns the date as "an 8-byte character string in the "YYYYMMDD" format". When I run it with this code I''ve written , I get "Can not marshal return value".

My real concern is that, if I''m having trouble with these functions that
return simple data types, how am I going to interact with functions that
return user-defined data types?

This is what I''ve written in the class so far:

...
using System.Runtime.InteropServices;
namespace ZM7
{
/// <summary>
/// Summary description for AMS.
/// </summary>
public class AMS
{
[DllImport("C:\\zm7\\Developm\\DLL\\W32.DLL")]
public static extern int z4open(); //another function from the dll
[DllImport("C:\\zm7\\Developm\\DLL\\W32.DLL")] /* Do I need to use
DllImport for every fiunction I''m using? */
public static extern byte[] z4date();
...

public byte[] getDate()
{
Byte[] myDate;
myDate = new Byte[8];
myDate = z4date(); //function call where I receive error "Can not marshal return value"
return myDate();
}

Any help is appreciated.




这篇关于帮助从DLL调用函数并处理返回值(无法编组返回值)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆