PInvoke编组.... [英] PInvoke Marshalling....

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

问题描述

问候!


我正试图在ac#app中调用此方法...

SNAPIDLL_API int __stdcall SNAPI_SetCapabilitiesBuffer( HANDLE

DeviceHandle,unsigned char * pData,long max_length);


到目前为止我有这个:

[ DllImport(Dependencies\\SNAPI.DLL)]

public static extern int SNAPI_SetVersionBuffer(IntPtr DeviceHandle,

StringBuilder pData,long max_length);


继续得到PInvokeStackImbalance错误,我认为它是第二个参数

pData因为我引用了设备处理程序而long typedef是

非常明显。


对于第二个参数我尝试了以下但没有成功:

byte []

char []

string

StringBuilder

[MarshalAs(UnmanagedType.AnsiBStr)] string


帮助!

谢谢。

Dan。

解决案
丹尼尔,


其实,这不是很明显的。在C ++中,long对应于32位

是如何在DllImport属性中编组的。总而言之,您的声明

应如下所示:


[DllImport(" Dependencies\\SNAPI.DLL",CharSet = CharSet.Ansi)]

public static extern int SNAPI_SetVersionBuffer(IntPtr DeviceHandle,

StringBuilder pData,int max_length);


另外,如果pData参数不会被API

函数写入,你可以将它作为字符串传递。

-

- Nicholas Paldino [.NET / C#MVP]

- mv*@spam.guard.caspershouse.com

Daniel Bass < da *********** @ blueCAPSbottle.comFIRSTwrote in message

news:u8 ************** @ TK2MSFTNGP03.phx。 gbl ...


问候!


我试图在ac#app中调用此方法...


SNAPIDLL_API int __stdcall SNAPI_SetCapabilitiesBuffer(HANDLE

DeviceHandle,unsigned char * pData,long max_length);


所以我有这个:

[DllImport(" Dependencies\\SNAPI.DLL)]]

public static extern int SNAPI_SetVersionBuffer(IntPtr DeviceHandle,

StringBuilder pData,long max_length);


继续得到PInvokeStackImbalance错误,我认为这是第2个
参数pData因为我已经引用了设备处理程序而且很长的

typedef非常明显。


对于第二个参数我尝试过以下但没有成功:

byte []

char []

stri ng $>
StringBuilder

[MarshalAs(UnmanagedType.AnsiBStr)] string


帮助!

谢谢。

Dan。



Nicholas,


很棒,感谢您的快速回复!


pData是我创建的缓冲区,然后传入函数进行填充。

参数作为StringBuilder是否会对此进行编组数据是否正确?


谢谢。

Dan。


Nicholas Paldino [.NET / C#MVP] " < mv*@spam.guard.caspershouse.com写在

消息新闻:ec ************* @ TK2MSFTNGP03.phx.gbl ...


Daniel,


实际上,这并不是很明显。在C ++中,long对应于

32位整数,在C#中是一个整数。此外,您应该声明如何在DllImport属性中封送您的

字符串。总而言之,您的

声明应如下所示:


[DllImport(" Dependencies\\SNAPI.DLL",CharSet = CharSet.Ansi)]

public static extern int SNAPI_SetVersionBuffer(IntPtr DeviceHandle,

StringBuilder pData,int max_length);


另外,如果pData参数不会被API

函数写入,你可以将其作为字符串传递。


-

- Nicholas Paldino [.NET / C#MVP]

- mv*@spam.guard。 caspershouse.com


" Daniel Bass" < da *********** @ blueCAPSbottle.comFIRSTwrote in message

news:u8 ************** @ TK2MSFTNGP03.phx。 gbl ...


>问候!

我正在尝试在ac#app中调用此方法...

SNAPIDLL_API int __stdcall SNAPI_SetCapabilitiesBuffer(HANDLE
DeviceHandle,unsigned char * pData,long max_length);

到目前为止我有这个:
[DllImport (Dependencies\\SNAPI.DLL)]
public static extern int SNAPI_SetVersionBuffer(IntPtr DeviceHandle,
StringBuilder pData,long max_length);

继续获取PInvokeStackImbalance错误,我认为它是第二个参数pData,因为我已经引用了设备处理程序而且很长的
typedef非常明显。

对于第二个参数我'尝试了以下但没有成功:
byte []
char []
string
StringBuilder
[MarshalAs(UnmanagedType.AnsiBStr)] string

帮助!
谢谢。
Dan。




Daniel,


是的,如果API函数要写入缓冲区,那么传递一个

StringBuilder将导致数据被封送回给你正确。

-

- Nicholas Paldino [.NET / C#MVP]

- mv*@spam.guard.caspershouse.com


Daniel Bass < da *********** @ blueCAPSbottle.comFIRSTwrote in message

news:%2 **************** @ TK2MSFTNGP05 .phx.gbl ...


Nicholas,


非常好,谢谢你的回复!


pData是我创建的缓冲区,然后传入函数进行填充。

参数作为StringBuilder是否正确编组了这些数据?


谢谢。

Dan。


" Nicholas Paldino [.NET / C#MVP]" < mv*@spam.guard.caspershouse.com写了
消息新闻中的
:ec ************* @ TK2MSFTNGP03.phx.gbl ...


> Daniel,

实际上,它并不是很明显。在C ++中,long对应于一个32位整数,在C#中,它是一个int。此外,您应该声明如何在DllImport属性中封送您的字符串。总而言之,您的
声明应如下所示:

[DllImport(" Dependencies\\SNAPI.DLL",CharSet = CharSet.Ansi)] public static extern int SNAPI_SetVersionBuffer(IntPtr DeviceHandle,
StringBuilder pData,int max_length);

另外,如果pData参数不会由API
函数写入,你可以把它作为字符串传递。

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

" Daniel Bass" < da *********** @ blueCAPSbottle.comFIRSTwrote in message
新闻:u8 ************** @ TK2MSFTNGP03.phx.gbl ...


>>问候!

我正在尝试在ac#app中调用此方法...

SNAPIDLL_API int __stdcall SNAPI_SetCapabilitiesBuffer(HANDLE
DeviceHandle,unsigned char * pData,long max_length);

到目前为止我有这个:
[DllImport(& ;依赖项\\SNAPI.DLL")]
public static extern int SNAPI_SetVersionBuffer(IntPtr DeviceHandle,
StringBuilder pData,long max_length);

继续获取PInvokeStackImbalance错误,我认为它是第二个参数pData,因为我已经引用了设备处理程序而且很长的
typedef非常明显。

对于第二个参数我是''我试过以下没有成功:
byte []
char []
string
StringBuilder
[MarshalAs(UnmanagedType.AnsiBStr)] string

帮助!
谢谢。
Dan。





Greetings!

I''m trying to call this method in a c# app...

SNAPIDLL_API int __stdcall SNAPI_SetCapabilitiesBuffer(HANDLE
DeviceHandle, unsigned char *pData, long max_length);

So far I''ve got this:
[DllImport("Dependencies\\SNAPI.DLL")]
public static extern int SNAPI_SetVersionBuffer(IntPtr DeviceHandle,
StringBuilder pData, long max_length);

Keep getting a PInvokeStackImbalance error and I think it''s the 2nd paramter
pData because I''ve referenced the device handler and the long typedef is
pretty obvious.

For the 2nd paramter I''ve tried the following without success:
byte[]
char[]
string
StringBuilder
[MarshalAs(UnmanagedType.AnsiBStr)] string

HELP!
Thanks.
Dan.

解决方案

Daniel,

Actually, it''s not pretty obvious. In C++, long corresponds to a 32-bit
integer, which in C#, is an int. Also, you should declare how your strings
are marshaled in the DllImport attribute. All-in-all, your declaration
should look like this:

[DllImport("Dependencies\\SNAPI.DLL", CharSet=CharSet.Ansi)]
public static extern int SNAPI_SetVersionBuffer(IntPtr DeviceHandle,
StringBuilder pData, int max_length);

Also, if the pData parameter is not going to be written to by the API
function, you can pass it as a string.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Daniel Bass" <da***********@blueCAPSbottle.comFIRSTwrote in message
news:u8**************@TK2MSFTNGP03.phx.gbl...

Greetings!

I''m trying to call this method in a c# app...

SNAPIDLL_API int __stdcall SNAPI_SetCapabilitiesBuffer(HANDLE
DeviceHandle, unsigned char *pData, long max_length);

So far I''ve got this:
[DllImport("Dependencies\\SNAPI.DLL")]
public static extern int SNAPI_SetVersionBuffer(IntPtr DeviceHandle,
StringBuilder pData, long max_length);

Keep getting a PInvokeStackImbalance error and I think it''s the 2nd
paramter pData because I''ve referenced the device handler and the long
typedef is pretty obvious.

For the 2nd paramter I''ve tried the following without success:
byte[]
char[]
string
StringBuilder
[MarshalAs(UnmanagedType.AnsiBStr)] string

HELP!
Thanks.
Dan.



Nicholas,

Excellent, thanks for the prompt reply!

pData is a buffer that I create, then pass into the function to populate.
Does the parameter as a StringBuilder marshall this data correctly?

Thanks.
Dan.

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote in
message news:ec*************@TK2MSFTNGP03.phx.gbl...

Daniel,

Actually, it''s not pretty obvious. In C++, long corresponds to a
32-bit integer, which in C#, is an int. Also, you should declare how your
strings are marshaled in the DllImport attribute. All-in-all, your
declaration should look like this:

[DllImport("Dependencies\\SNAPI.DLL", CharSet=CharSet.Ansi)]
public static extern int SNAPI_SetVersionBuffer(IntPtr DeviceHandle,
StringBuilder pData, int max_length);

Also, if the pData parameter is not going to be written to by the API
function, you can pass it as a string.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Daniel Bass" <da***********@blueCAPSbottle.comFIRSTwrote in message
news:u8**************@TK2MSFTNGP03.phx.gbl...

>Greetings!

I''m trying to call this method in a c# app...

SNAPIDLL_API int __stdcall SNAPI_SetCapabilitiesBuffer(HANDLE
DeviceHandle, unsigned char *pData, long max_length);

So far I''ve got this:
[DllImport("Dependencies\\SNAPI.DLL")]
public static extern int SNAPI_SetVersionBuffer(IntPtr DeviceHandle,
StringBuilder pData, long max_length);

Keep getting a PInvokeStackImbalance error and I think it''s the 2nd
paramter pData because I''ve referenced the device handler and the long
typedef is pretty obvious.

For the 2nd paramter I''ve tried the following without success:
byte[]
char[]
string
StringBuilder
[MarshalAs(UnmanagedType.AnsiBStr)] string

HELP!
Thanks.
Dan.




Daniel,

Yes, if the API function is going to write to the buffer, then passing a
StringBuilder will cause the data to be marshaled back to you correctly.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Daniel Bass" <da***********@blueCAPSbottle.comFIRSTwrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...

Nicholas,

Excellent, thanks for the prompt reply!

pData is a buffer that I create, then pass into the function to populate.
Does the parameter as a StringBuilder marshall this data correctly?

Thanks.
Dan.

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote
in message news:ec*************@TK2MSFTNGP03.phx.gbl...

>Daniel,

Actually, it''s not pretty obvious. In C++, long corresponds to a
32-bit integer, which in C#, is an int. Also, you should declare how
your strings are marshaled in the DllImport attribute. All-in-all, your
declaration should look like this:

[DllImport("Dependencies\\SNAPI.DLL", CharSet=CharSet.Ansi)]
public static extern int SNAPI_SetVersionBuffer(IntPtr DeviceHandle,
StringBuilder pData, int max_length);

Also, if the pData parameter is not going to be written to by the API
function, you can pass it as a string.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Daniel Bass" <da***********@blueCAPSbottle.comFIRSTwrote in message
news:u8**************@TK2MSFTNGP03.phx.gbl...

>>Greetings!

I''m trying to call this method in a c# app...

SNAPIDLL_API int __stdcall SNAPI_SetCapabilitiesBuffer(HANDLE
DeviceHandle, unsigned char *pData, long max_length);

So far I''ve got this:
[DllImport("Dependencies\\SNAPI.DLL")]
public static extern int SNAPI_SetVersionBuffer(IntPtr DeviceHandle,
StringBuilder pData, long max_length);

Keep getting a PInvokeStackImbalance error and I think it''s the 2nd
paramter pData because I''ve referenced the device handler and the long
typedef is pretty obvious.

For the 2nd paramter I''ve tried the following without success:
byte[]
char[]
string
StringBuilder
[MarshalAs(UnmanagedType.AnsiBStr)] string

HELP!
Thanks.
Dan.





这篇关于PInvoke编组....的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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