托管C#到非托管DLL [英] Managed C# to unmanaged DLL

查看:90
本文介绍了托管C#到非托管DLL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个将C#中的char *传递给C ++中的库。我已经尝试过

传递字符串并执行[MarshaAs(UnmanagedType.LPStr)]字符串myVal

并且它不起作用。我还尝试通过声明

我的方法不安全并传递一个指针但它仍然无法正常工作来传递一个不安全的电话。


任何帮助都会感谢。


问候,

Nach


代码:


[DllImport(StreamLibPath)]

unsafe静态extern uint writeStream(int handle,char * pBuf,uint

size,uint actualWrittenSize);

unsafe public void callStream()

{

char * data

writeStream(_myHandle,data,writeSize,writtenSize));

}

解决方案

< na ************* @ gmail .comwrote in message

news:11 ********************** @ j72g2000cwa.googlegr oups.com ...


您好,

我有一个将C#中的char *传递给C ++中的库。我已经尝试过

传递字符串并执行[MarshaAs(UnmanagedType.LPStr)]字符串myVal

并且它不起作用。我还尝试通过声明

我的方法不安全并传递一个指针但它仍然无法正常工作来传递一个不安全的电话。


任何帮助都会感谢。


问候,

Nach


代码:


[DllImport(StreamLibPath)]

unsafe静态extern uint writeStream(int handle,char * pBuf,uint

size,uint actualWrittenSize);


unsafe public void callStream()

{

char * data

writeStream(_myHandle,data,writeSize,writtenSize) );

}



检查API使用的字符编码类型并应用适当的MarshalAs

attibute。

另请注意,我很确定realWrittenSize应该通过参数传递。


这是将字符串作为宽传递的签名字符串,actualWrittenSize

应该包含调用后写的字符(我猜)。

static extern uint writeStream(int handle,[MarshaAs(UnmanagedType.LPWStr)] string pBuf,

uint size,out uint actualWrittenSize);

Willy。


< ciach>


exacly" writeStream"看看C ++?


pozdrawiam


Przemek Sulikowski


检查什么样的API使用字符编码并应用


相应的MarshalAs attibute。

另请注意,我很确定actualWrittenSize应该传递

byref。


这是将字符串作为宽字符串传递的签名,

actualWrittenSize应该包含电话后写的字符(我猜b $ b)。

static extern uint writeStream(int handle,

[MarshaAs(UnmanagedType.LPWStr)] string pBuf,uint size,out uint

actualWrittenSize);



或者如果char *不表示字符串但字节数组


static extern uint writeStream(int handle,IntPtr pBuf,uint尺寸,ref uint

actualWrittenSize);


并像这样使用


uint len = 70;

uint rlen = 0;

byte [] bytes = new byte [len];

//填充字节

IntPtr ptr = Marshal.AllocHGlobal(len);

Marshal.Copy(bytes,0,ptr,len);

writeStream(handle,ptr,len,ref rlen);

Marshal.FreeHGlobal(ptr);


pozdrawiam


Przemek Sulikowski


Hi,
I have a to pass a char* from C# to a library in C++. I have tried
passing string and doing [MarshaAs(UnmanagedType.LPStr)]string myVal
and it didn''t work . I also tried to pass an unsafe call by declaring
my method as unsafe and passing a pointer and it still doesn''t work.

Any help will be appreciated.

Regards,
Nach

Code:

[DllImport(StreamLibPath)]
unsafe static extern uint writeStream(int handle, char* pBuf, uint
size, uint actualWrittenSize);
unsafe public void callStream()
{
char* data
writeStream(_myHandle, data, writeSize, writtenSize));
}

解决方案

<na*************@gmail.comwrote in message
news:11**********************@j72g2000cwa.googlegr oups.com...

Hi,
I have a to pass a char* from C# to a library in C++. I have tried
passing string and doing [MarshaAs(UnmanagedType.LPStr)]string myVal
and it didn''t work . I also tried to pass an unsafe call by declaring
my method as unsafe and passing a pointer and it still doesn''t work.

Any help will be appreciated.

Regards,
Nach

Code:

[DllImport(StreamLibPath)]
unsafe static extern uint writeStream(int handle, char* pBuf, uint
size, uint actualWrittenSize);
unsafe public void callStream()
{
char* data
writeStream(_myHandle, data, writeSize, writtenSize));
}

Check what kind of character encoding is used by the API and apply the appropriate MarshalAs
attibute.
Note also that I''m quite sure that actualWrittenSize should be passed byref.

Here is the signature that passes the string as a Wide character string, actualWrittenSize
should contain the characters written after the call (I guess).
static extern uint writeStream(int handle, [MarshaAs(UnmanagedType.LPWStr)] string pBuf,
uint size, out uint actualWrittenSize);
Willy.


<ciach>

how exacly "writeStream" looks in C++ ?

pozdrawiam

Przemek Sulikowski


Check what kind of character encoding is used by the API and apply the

appropriate MarshalAs attibute.
Note also that I''m quite sure that actualWrittenSize should be passed
byref.

Here is the signature that passes the string as a Wide character string,
actualWrittenSize should contain the characters written after the call (I
guess).
static extern uint writeStream(int handle,
[MarshaAs(UnmanagedType.LPWStr)] string pBuf, uint size, out uint
actualWrittenSize);

or if char* doesnt means string but byte array

static extern uint writeStream(int handle, IntPtr pBuf, uint size, ref uint
actualWrittenSize);

and use it like this

uint len = 70;
uint rlen = 0;
byte[] bytes = new byte[len];
//fill the bytes
IntPtr ptr = Marshal.AllocHGlobal(len);
Marshal.Copy(bytes, 0, ptr, len);
writeStream(handle, ptr, len, ref rlen);
Marshal.FreeHGlobal(ptr);

pozdrawiam

Przemek Sulikowski


这篇关于托管C#到非托管DLL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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