将C#中的值传递给C ++ dll [英] Passing values from C# from/to a C++ dll

查看:114
本文介绍了将C#中的值传递给C ++ dll的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C ++中有这个,我喜欢从c#调用它来获取值

但是我失败了。如果你能给我一些信息会很好。我在bb.net尝试了它b $ b,但是我在C#

中使用的方式几乎和VB一样,但它确实不起作用。


c ++ :( csp2.dll)

NoMangle long DLL_IMPORT_EXPORT csp2TimeStamp2Str(unsigned char

* Stamp,char * value,long nMaxLength);



VB.net :(这样可以正常工作)


声明函数csp2TimeStamp2Str Lib" csp2.dll" (ByRef Stamp As

Byte,ByVal value As String,ByVal nMaxLength As Integer)as Integer


Dim nRC as Integer

Dim arrbyteBarcode(99)As Byte''100个元素

Dim nBytesRead As Integer

Dim bstrTmp As New VB6.FixedLengthString(50)


nBytesRead = csp2GetPacket(arrbyteBarcode(0),i,100)

nRC = csp2TimeStamp2Str(arrbyteBarcode(nBytesRead - 4),bstrTmp.Value,

Len(bstrTmp.Value))

TextBox1.text = VB.Left(bstrTmp.Value,20)


C#:(这个不工作:()


[System.Runtime.InteropServices.DllImport(" csp2.DLL")] static extern

int csp2TimeStamp2Str (字节值,字符串Stamp,int nMaxLength);


int nRC,nBytesRead;

byte [] arrbyteBarcode = new byte [100];

FixedLengthString bstrTmp = new FixedLengthString(50);


nBytesRead = csp2GetPacket(arrbyteBarcode [0],i,100);

bstrTmp =" " ;;


nRC = csp2TimeStamp2Str(arrbyteBarcode [nBytesRead-4],

bstrTmp.Value,bstrTmp.Value.Length);

TextBox1.text = bstrTmp.Value.toString();

Hi, I have this in C++ and I like to call it from c# to get the value
but I fail. it will be good if you can give me some information. I
tried it in VB.net it works but I use almost the same way as VB in C#
but it doens''t work.

c++: (csp2.dll)
NoMangle long DLL_IMPORT_EXPORT csp2TimeStamp2Str(unsigned char
*Stamp, char *value, long nMaxLength);


VB.net: (this works correctly)

Declare Function csp2TimeStamp2Str Lib "csp2.dll" (ByRef Stamp As
Byte, ByVal value As String, ByVal nMaxLength As Integer) As Integer

Dim nRC As Integer
Dim arrbyteBarcode(99) As Byte ''100 elements
Dim nBytesRead As Integer
Dim bstrTmp As New VB6.FixedLengthString(50)

nBytesRead = csp2GetPacket(arrbyteBarcode(0), i, 100)

nRC = csp2TimeStamp2Str(arrbyteBarcode(nBytesRead - 4), bstrTmp.Value,
Len(bstrTmp.Value))
TextBox1.text= VB.Left(bstrTmp.Value, 20)


C#: (this doesn''t work :( )

[System.Runtime.InteropServices.DllImport("csp2.DLL ")] static extern
int csp2TimeStamp2Str(byte value, string Stamp, int nMaxLength);

int nRC, nBytesRead;
byte[] arrbyteBarcode= new byte[100];
FixedLengthString bstrTmp = new FixedLengthString(50);

nBytesRead = csp2GetPacket(arrbyteBarcode[0], i, 100);
bstrTmp=" ";

nRC = csp2TimeStamp2Str(arrbyteBarcode[nBytesRead-4],
bstrTmp.Value, bstrTmp.Value.Length);
TextBox1.text= bstrTmp.Value.toString();

推荐答案

" Bob Yang" < bo ****** @ gmail.com写信息

新闻:11 ********************* @ d55g2000hsg。 googlegro ups.com ...
"Bob Yang" <bo******@gmail.comwrote in message
news:11*********************@d55g2000hsg.googlegro ups.com...

我在C ++中有这个,我喜欢从c#调用它来获取值

但我失败了。如果你能给我一些信息会很好。我在bb.net尝试了它b $ b,但是我在C#

中使用的方式几乎和VB一样,但它确实不起作用。


c ++ :( csp2.dll)

NoMangle long DLL_IMPORT_EXPORT csp2TimeStamp2Str(unsigned char

* Stamp,char * value,long nMaxLength);



VB.net :(这样可以正常工作)


声明函数csp2TimeStamp2Str Lib" csp2.dll" (ByRef Stamp As

Byte,ByVal value As String,ByVal nMaxLength As Integer)as Integer


Dim nRC as Integer

Dim arrbyteBarcode(99)As Byte''100个元素

Dim nBytesRead As Integer

Dim bstrTmp As New VB6.FixedLengthString(50)


nBytesRead = csp2GetPacket(arrbyteBarcode(0),i,100)

nRC = csp2TimeStamp2Str(arrbyteBarcode(nBytesRead - 4),bstrTmp.Value,

Len(bstrTmp.Value))

TextBox1.text = VB.Left(bstrTmp.Value,20)


C#:(这个不工作:()


[System.Runtime.InteropServices.DllImport(" csp2.DLL")] static extern

int csp2TimeStamp2Str (字节值,字符串Stamp,int nMaxLength);


int nRC,nBytesRead;

byte [] arrbyteBarcode = new byte [100];

FixedLengthString bstrTmp = new FixedLengthString(50);


nBytesRea d = csp2GetPacket(arrbyteBarcode [0],i,100);

bstrTmp =" " ;;


nRC = csp2TimeStamp2Str(arrbyteBarcode [nBytesRead-4],

bstrTmp.Value,bstrTmp.Value.Length);

TextBox1.text = bstrTmp.Value.toString();
Hi, I have this in C++ and I like to call it from c# to get the value
but I fail. it will be good if you can give me some information. I
tried it in VB.net it works but I use almost the same way as VB in C#
but it doens''t work.

c++: (csp2.dll)
NoMangle long DLL_IMPORT_EXPORT csp2TimeStamp2Str(unsigned char
*Stamp, char *value, long nMaxLength);


VB.net: (this works correctly)

Declare Function csp2TimeStamp2Str Lib "csp2.dll" (ByRef Stamp As
Byte, ByVal value As String, ByVal nMaxLength As Integer) As Integer

Dim nRC As Integer
Dim arrbyteBarcode(99) As Byte ''100 elements
Dim nBytesRead As Integer
Dim bstrTmp As New VB6.FixedLengthString(50)

nBytesRead = csp2GetPacket(arrbyteBarcode(0), i, 100)

nRC = csp2TimeStamp2Str(arrbyteBarcode(nBytesRead - 4), bstrTmp.Value,
Len(bstrTmp.Value))
TextBox1.text= VB.Left(bstrTmp.Value, 20)


C#: (this doesn''t work :( )

[System.Runtime.InteropServices.DllImport("csp2.DLL ")] static extern
int csp2TimeStamp2Str(byte value, string Stamp, int nMaxLength);

int nRC, nBytesRead;
byte[] arrbyteBarcode= new byte[100];
FixedLengthString bstrTmp = new FixedLengthString(50);

nBytesRead = csp2GetPacket(arrbyteBarcode[0], i, 100);
bstrTmp=" ";

nRC = csp2TimeStamp2Str(arrbyteBarcode[nBytesRead-4],
bstrTmp.Value, bstrTmp.Value.Length);
TextBox1.text= bstrTmp.Value.toString();




..... csp2TimeStamp2Str(byte [] value,StringBuilder Stamp, int nMaxLength);


int nRC,nBytesRead;

byte [] arrbyteBarcode = new byte [100];

StringBuilder sb = new StringBuilder(",",100);

nBytesRead = csp2GetPacket(arrbyteBarcode [0],i,100);

ArraySegment< byteas = new ArraySegment< byte> ;(arrbyteBarcode,

nBytesRead-4,4); // [2]

nRC = csp2TimeStamp2Str(as.Array,sb,sb.Length); // [3]

TextBox1.text = sb.toString();


首先你必须将一个byte []作为第一个参数传递给函数,你是
按值传递数组的第一个字节。请注意,VB代码也是有缺陷的,你应该传递一个字节[]而不是一个字节,这个代码

将在64位Windows上失败! />
秒[1]摆脱VB6依赖,并使用StringBuilder传递一个

固定字符串缓冲区。不知道你为什么要通过 "在调用

这个函数的时候可以使用char。

[2]和[3]用于从原始数组中获取字节数组。


Willy。



..... csp2TimeStamp2Str(byte[] value, StringBuilder Stamp, int nMaxLength);

int nRC, nBytesRead;
byte[] arrbyteBarcode= new byte[100];
StringBuilder sb = new StringBuilder(" ", 100);
nBytesRead = csp2GetPacket(arrbyteBarcode[0], i, 100);
ArraySegment<byteas = new ArraySegment<byte>(arrbyteBarcode,
nBytesRead-4, 4); // [2]
nRC = csp2TimeStamp2Str(as.Array, sb, sb.Length); // [3]
TextBox1.text= sb.toString();

First you have to pass a byte[] as first parameter to the function, you are
passing the first byte of the array by value. Note that the VB code is
flawed too, you should pass a byte[] not a refrence to a byte, this code
will fail on 64 bit Windows!
Second [1]get rid of the VB6 dependency and use a StringBuilder to pass a
fixed string buffer. Not sure why you are passing a " " char when calling
this function though.
[2] and [3] are used to get a byte array segment out of the original array.

Willy.



.... csp2TimeStamp2Str(byte [] value, StringBuilder Stamp,int nMaxLength);


int nRC,nBytesRead;

byte [] arrbyteBarcode = new byte [100];

StringBuilder sb = new StringBuilder("",100);

nBytesRead = csp2GetPacket(arrbyteBarcode [0],i,100);

ArraySegment< byteas = new ArraySegment< byte>(arrbyteBarcode,

nBytesRead-4,4); // [2]

nRC = csp2TimeStamp2Str(as.Array,sb,sb.Length); // [3]

TextBox1.text = sb.toString();
.... csp2TimeStamp2Str(byte[] value, StringBuilder Stamp, int nMaxLength);

int nRC, nBytesRead;
byte[] arrbyteBarcode= new byte[100];
StringBuilder sb = new StringBuilder(" ", 100);
nBytesRead = csp2GetPacket(arrbyteBarcode[0], i, 100);
ArraySegment<byteas = new ArraySegment<byte>(arrbyteBarcode,
nBytesRead-4, 4); // [2]
nRC = csp2TimeStamp2Str(as.Array, sb, sb.Length); // [3]
TextBox1.text= sb.toString();



[snip]

[snip]


[2]和[3]用于从原始的

数组中获取字节数组段。
[2] and [3] are used to get a byte array segment out of the original
array.



不,它不是。 ArraySegment< T> .Array是整个数组,而不是子集。

No, it doesn''t. ArraySegment<T>.Array is the entire array, not a subset.


谢谢!然而,它确实不起作用。 sb.ToString()变成了一个

空间


我在这里捕获一些屏幕和代码: http://docs.google.com/Doc?id=dd5djd97_44d5nz2p


另一个问题,这与真正无关,它是如何处理的/>
访问sb的值出去参考或外出? c#自己处理这个

吗?我只是想知道如何在纯c#中使用它而没有c ++

没有全局变量也没有ref或out。
Thank you! However, it doens''t really work. sb.ToString() turns a
space " "

I capture some screens and code here: http://docs.google.com/Doc?id=dd5djd97_44d5nz2p

another question, this is not really related, how does it handle
access the value to "sb" wiht out "ref" or "out"? does c# handle this
by itself? I just wonder how to use it in the pure c# without c++
without global variables nor ref or out.

" Bob Yang" < bobya ... @ gmail.com写信息


新闻:11 ********************* @ d55g2000hsg .googlegro ups.com ...

"Bob Yang" <bobya...@gmail.comwrote in message

news:11*********************@d55g2000hsg.googlegro ups.com...



我在C ++中有这个,我喜欢从c#调用它来获取价值

但是我失败了。如果你能给我一些信息会很好。我在VB.net中尝试了它的b $ b它可以工作,但是我在C#

中使用的方式与VB几乎相同,但它确实不起作用。
Hi, I have this in C++ and I like to call it from c# to get the value
but I fail. it will be good if you can give me some information. I
tried it in VB.net it works but I use almost the same way as VB in C#
but it doens''t work.


c ++ :( csp2.dll)

NoMangle long DLL_IMPORT_EXPORT csp2TimeStamp2Str(unsigned char

*邮票,字符*值,长nMaxLength);
c++: (csp2.dll)
NoMangle long DLL_IMPORT_EXPORT csp2TimeStamp2Str(unsigned char
*Stamp, char *value, long nMaxLength);


VB.net :(这是正常的)
VB.net: (this works correctly)


声明函数csp2TimeStamp2Str Lib" csp2.dll" (ByRef Stamp As

Byte,ByVal value As String,ByVal nMaxLength As Integer)As Integer
Declare Function csp2TimeStamp2Str Lib "csp2.dll" (ByRef Stamp As
Byte, ByVal value As String, ByVal nMaxLength As Integer) As Integer


Dim nRC As Integer

Dim arrbyteBarcode(99)As Byte''100个元素

Dim nBytesRead As Integer

Dim bstrTmp As New VB6.FixedLengthString(50)
Dim nRC As Integer
Dim arrbyteBarcode(99) As Byte ''100 elements
Dim nBytesRead As Integer
Dim bstrTmp As New VB6.FixedLengthString(50)


nBytesRead = csp2GetPacket(arrbyteBarcode(0),i,100)
nBytesRead = csp2GetPacket(arrbyteBarcode(0), i, 100)


nRC = csp2TimeStamp2Str(arrbyteBarcode(nBytesRead - 4),bstrTmp.Value,

Len(bstrTmp.Value))

TextBox1.text = VB.Left(bstrTmp。值,20)
nRC = csp2TimeStamp2Str(arrbyteBarcode(nBytesRead - 4), bstrTmp.Value,
Len(bstrTmp.Value))
TextBox1.text= VB.Left(bstrTmp.Value, 20)


C#:(这不起作用:()
C#: (this doesn''t work :( )


[System.Runtime.InteropServices.DllImport(" csp2.DLL")] static extern

int csp2TimeStamp2Str(byte value,string Stamp ,int nMaxLength);
[System.Runtime.InteropServices.DllImport("csp2.DLL ")] static extern
int csp2TimeStamp2Str(byte value, string Stamp, int nMaxLength);


int nRC,nBytesRead;

byte [] arrbyteBarcode = new byte [100];

FixedLengthString bstrTmp = new FixedLengthString(50);
int nRC, nBytesRead;
byte[] arrbyteBarcode= new byte[100];
FixedLengthString bstrTmp = new FixedLengthString(50);


nBytesRead = csp2GetPacket(arrbyteBarcode [0],i,100);

bstrTmp ="英寸;
nBytesRead = csp2GetPacket(arrbyteBarcode[0], i, 100);
bstrTmp=" ";


nRC = csp2TimeStamp2Str(arrbyteBarcode [nBytesRead-4],

bstrTmp.Value,bstrTmp.Value.Length);

TextBox1.text = bstrTmp.Value.toString();
nRC = csp2TimeStamp2Str(arrbyteBarcode[nBytesRead-4],
bstrTmp.Value, bstrTmp.Value.Length);
TextBox1.text= bstrTmp.Value.toString();



.... csp2TimeStamp2Str(byte [] value,StringBuilder Stamp,int nMaxLength);


int nRC,nBytesRead ;

byte [] arrbyteBarcode = new byte [100];

StringBuilder sb = new StringBuilder("",100);

nBytesRead = csp2GetPacket(arrbyteBarcode [0],i,100);

ArraySegment< byteas = new ArraySegment< byte>(arrbyteBarcode,

nBytesRead-4,4); // [2]

nRC = csp2TimeStamp2Str(as.Array,sb,sb.Length); // [3]

TextBox1.text = sb.toString();


首先你必须将一个byte []作为第一个参数传递给函数,你是
按值传递数组的第一个字节。请注意,VB代码也是有缺陷的,你应该传递一个字节[]而不是一个字节,这个代码

将在64位Windows上失败! />
秒[1]摆脱VB6依赖,并使用StringBuilder传递一个

固定字符串缓冲区。不知道你为什么要通过 "在调用

这个函数的时候可以使用char。

[2]和[3]用于从原始数组中获取字节数组。


Willy .- -


- -


.... csp2TimeStamp2Str(byte[] value, StringBuilder Stamp, int nMaxLength);

int nRC, nBytesRead;
byte[] arrbyteBarcode= new byte[100];
StringBuilder sb = new StringBuilder(" ", 100);
nBytesRead = csp2GetPacket(arrbyteBarcode[0], i, 100);
ArraySegment<byteas = new ArraySegment<byte>(arrbyteBarcode,
nBytesRead-4, 4); // [2]
nRC = csp2TimeStamp2Str(as.Array, sb, sb.Length); // [3]
TextBox1.text= sb.toString();

First you have to pass a byte[] as first parameter to the function, you are
passing the first byte of the array by value. Note that the VB code is
flawed too, you should pass a byte[] not a refrence to a byte, this code
will fail on 64 bit Windows!
Second [1]get rid of the VB6 dependency and use a StringBuilder to pass a
fixed string buffer. Not sure why you are passing a " " char when calling
this function though.
[2] and [3] are used to get a byte array segment out of the original array.

Willy.- -

- -



这篇关于将C#中的值传递给C ++ dll的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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