P / Invoke和out参数 [英] P/Invoke and out parameter

查看:87
本文介绍了P / Invoke和out参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到的问题很简单。


我在C DLL中有一个函数。我们称之为SomeFunction。这是SomeFunction的原型:


void SomeFunction(char * anArrayOfChars);


其中anArrayOfChars是一个out参数。


我认为我所要做的就是声明如下:


[DllImport(" someDLL",CharSet) = CharSet.Auto)]

public static extern void SomeFunction(StringBuilder sBuf)

并在Main函数中调用它:


StringBuilder sBuf = new StringBuilder(128);

SomeFunction(sBuf);


我应该有一个由某个函数填充的sBuf。

不幸的是,我得到以下异常:


对象引用未设置为对象的实例。我有什么想法吗?b $ b你做错了吗?


如果你想知道,SomeFunction只是做一个strcpy(anArrayOfChars,

你好,世界!;


提前致谢。

I''m having a problem with something I thought was quite simple.

I have a function in a C DLL. Let''s call it SomeFunction. This is the
prototype for SomeFunction:

void SomeFunction(char * anArrayOfChars);

where anArrayOfChars is an out parameter.

I thought all I had to do was declare it as follows:

[DllImport("someDLL", CharSet = CharSet.Auto)]
public static extern void SomeFunction(StringBuilder sBuf)
and within the Main function call it like that:

StringBuilder sBuf = new StringBuilder(128);
SomeFunction(sBuf);

And I should have an sBuf that was filled by some function.
Unfortunately, I''m getting the following exception:

Object Reference not set to an instance of an object. Any ideas what I''m
doing wrong?

In case you wonder, SomeFunction just does a strcpy(anArrayOfChars,
"Hello, World!);

Thanks in advance.

推荐答案

我认为我所要做的只是声明如下:

[DllImport(" someDLL",CharSet = CharSet.Auto)]
public static extern void SomeFunction( StringBuilder sBuf)
I thought all I had to do was declare it as follows:

[DllImport("someDLL", CharSet = CharSet.Auto)]
public static extern void SomeFunction(StringBuilder sBuf)




你应该在这里使用CharSet.Ansi而不是CharSet.Auto。我不知道是否会导致这样的例外。


Mattias


-

Mattias Sj?gren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com

请回复到新闻组。



You should be using CharSet.Ansi rather than CharSet.Auto here. I
don''t know if that would cause such an exception though.

Mattias

--
Mattias Sj?gren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.





" Pollux" < PO **** @ nospam.spam>在消息中写道

新闻:MP ************************ @ usenet.plus.net ...


"Pollux" <po****@nospam.spam> wrote in message
news:MP************************@usenet.plus.net...
我遇到的问题很简单。

我在C DLL中有一个函数。我们称之为SomeFunction。这是SomeFunction的原型:

void SomeFunction(char * anArrayOfChars);

其中anArrayOfChars是一个out参数。

我认为我所要做的就是声明如下:

[DllImport(" someDLL",CharSet = CharSet.Auto)]
public static extern void SomeFunction(StringBuilder sBuf)

并在Main函数中调用它:

StringBuilder sBuf = new StringBuilder(128);
SomeFunction(sBuf);

我应该有一个由某个函数填充的sBuf。
不幸的是,我得到以下异常:

对象引用未设置为对象的实例。我有什么想法做错了吗?

如果你想知道,SomeFunction只做一个strcpy(anArrayOfChars,
你好,世界!);

提前致谢。
I''m having a problem with something I thought was quite simple.

I have a function in a C DLL. Let''s call it SomeFunction. This is the
prototype for SomeFunction:

void SomeFunction(char * anArrayOfChars);

where anArrayOfChars is an out parameter.

I thought all I had to do was declare it as follows:

[DllImport("someDLL", CharSet = CharSet.Auto)]
public static extern void SomeFunction(StringBuilder sBuf)
and within the Main function call it like that:

StringBuilder sBuf = new StringBuilder(128);
SomeFunction(sBuf);

And I should have an sBuf that was filled by some function.
Unfortunately, I''m getting the following exception:

Object Reference not set to an instance of an object. Any ideas what I''m
doing wrong?

In case you wonder, SomeFunction just does a strcpy(anArrayOfChars,
"Hello, World!);

Thanks in advance.




SomeFunction的调用显然不会引发异常。你可以吗?
提供一个调用堆栈转储?


因为你返回一个ansi字符串,你需要将你的签名改为:

[DllImport(someDLL)]

public static extern void

SomeFunction([MarshalAs(UnmanagedType.LPStr)] StringBuilder sBuf)

Willy。



The exception is obviously not thrown by the call of SomeFunction. Could you
provide a call stack dump?

Because you return an ansi string, you need to change your signature into:
[DllImport("someDLL")]
public static extern void
SomeFunction([MarshalAs(UnmanagedType.LPStr)]StringBuilder sBuf)

Willy.


在文章< #T ************** @ TK2MSFTNGP15.phx.gbl>中,
wi ************* @ pandora.be 说...
In article <#T**************@TK2MSFTNGP15.phx.gbl>,
wi*************@pandora.be says...


Pollux < PO **** @ nospam.spam>在消息中写道
新闻:MP ************************ @ usenet.plus.net ...


"Pollux" <po****@nospam.spam> wrote in message
news:MP************************@usenet.plus.net...
我遇到的问题很简单。

我在C DLL中有一个函数。我们称之为SomeFunction。这是SomeFunction的原型:

void SomeFunction(char * anArrayOfChars);

其中anArrayOfChars是一个out参数。

我认为我所要做的就是声明如下:

[DllImport(" someDLL",CharSet = CharSet.Auto)]
public static extern void SomeFunction(StringBuilder sBuf)

并在Main函数中调用它:

StringBuilder sBuf = new StringBuilder(128);
SomeFunction(sBuf);

我应该有一个由某个函数填充的sBuf。
不幸的是,我得到以下异常:

对象引用未设置为对象的实例。我有什么想法做错了吗?

如果你想知道,SomeFunction只做一个strcpy(anArrayOfChars,
你好,世界!);

提前致谢。
I''m having a problem with something I thought was quite simple.

I have a function in a C DLL. Let''s call it SomeFunction. This is the
prototype for SomeFunction:

void SomeFunction(char * anArrayOfChars);

where anArrayOfChars is an out parameter.

I thought all I had to do was declare it as follows:

[DllImport("someDLL", CharSet = CharSet.Auto)]
public static extern void SomeFunction(StringBuilder sBuf)
and within the Main function call it like that:

StringBuilder sBuf = new StringBuilder(128);
SomeFunction(sBuf);

And I should have an sBuf that was filled by some function.
Unfortunately, I''m getting the following exception:

Object Reference not set to an instance of an object. Any ideas what I''m
doing wrong?

In case you wonder, SomeFunction just does a strcpy(anArrayOfChars,
"Hello, World!);

Thanks in advance.



SomeFunction的调用显然不会引发异常。你可以提供一个调用堆栈转储吗?

因为你返回一个ansi字符串,你需要将你的签名改为:
[DllImport(" someDLL")]
public static extern void
SomeFunction([MarshalAs(UnmanagedType.LPStr)] StringBuilder sBuf)
Willy。



The exception is obviously not thrown by the call of SomeFunction. Could you
provide a call stack dump?

Because you return an ansi string, you need to change your signature into:
[DllImport("someDLL")]
public static extern void
SomeFunction([MarshalAs(UnmanagedType.LPStr)]StringBuilder sBuf)

Willy.




威利,


对此我很抱歉,我应该提到这一点。调用堆栈建议

从SomeFunction抛出异常。如果我删除了strcpy

的陈述并只是返回或显示一个消息框,我就不会得到

例外。


我猜想不知何故char *并没有指向一个有效的记忆

地址,但我不知道为什么这会因为

而变得难以实现框架应该为我照顾这个吗?



Hi Willy,

Sorry about that, I should have mentioned this. The call stack suggests
that the exception was thrown from SomeFunction. If i remove the strcpy
statment and simply return or display a messagebox, I don''t get an
exception.

I would guess that somehow the char * doesn''t point to a valid memory
address, but I''m not sure why this would be hapenning since the
Framework should take care of this for me right?


这篇关于P / Invoke和out参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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