在C#项目中使用C ++库 [英] Using c++ library in c# project

查看:95
本文介绍了在C#项目中使用C ++库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好;

我在EA15条码读取器OEM设备中需要一个C#条码读取器程序.我搜寻了很多东西,但我找不到.所以我开始在我的C#项目中使用EA15 c ++库.我需要将指针发送到inputbuffer,但是我不能这样做.这就是:

*我需要使用的c ++函数:

hi all;

I need a c# barcode reader program in EA15 barcode reader oem device. I search a lot to find but i cant. So i start to use EA15 c++ library in my c# project. I need to send pointer to inputbuffer but i cant do that. Here all is:

* c++ function that i need to use :

unsigned short ControlCommand (
  unsigned char *pInputBuffer,
  unsigned long nBytesInInputBuffer,
  unsigned char *pOutputBuffer,
  unsigned long nSizeOfOutputBuffer,
  unsigned long *pnBytesReturned
);



*输入缓冲区是->
pInputBuffer:指向包含ControlCommand帧(GID FID PRM…)的输入缓冲区的指针.
*在c ++程序中,对于此功能,代码如下:



* the input buffer is -->
pInputBuffer : Pointer to the input buffer containing the ControlCommand frame (GID FID PRM …).

* in c++ program, for this function, the code is like this:

char[] InputBuffer[0]=0x20;
InputBuffer[1]=0x40;
InputBuffer[2]=0x01;



*我在C#中的代码是这样的:



* my code in c# is like this:

//Definition of dll and function -->
[DllImport("ISDC_RS.dll")]
public static extern short ControlCommand(string a, int b, string c, int d, int e);
 
//Calling function -->
string x = ControlCommand(?????, 3, ?????, 20, 0).ToString();



我可以发送数据到哪种格式的功能?

最好的问候;
Aykut



in which format that i can send data to function?

Best regards;
Aykut

推荐答案

除了Richard MacCutchan的回答很好,在解决方案1中:

您应该有一个本地DLL文件.您已经拥有它,或者可以将其制作出C ++库.

然后,您可以在C#应用程序中通过P/Invoke使用它.一些更有用的链接来学习P/Invoke:
http://en.wikipedia.org/wiki/P/Invoke [ http://msdn.microsoft.com/library/en-us/vcmxspec/html/vcmg_PlatformInvocationServices.asp [^ ].

此CodeProject文章也可能有用:
基本P/调用 [ http://en.wikipedia.org/wiki/C%2B%2B/CLI [ ^ ],
http://www.gotw.ca/publications/C++CLIRationale.pdf [ ^ ],
http://www.ecma-international.org/publications/standards/Ecma-372.htm [^ ],
http://msdn.microsoft.com/en-us/library/dtefa218 [ ^ ],
http://msdn.microsoft.com/en-us/library/3bstk3k5 [ ^ ].

-SA
In addition to the good answer by Richard MacCutchan, in Solution 1:

You should have a native DLL file. You either have it already or can make it out of your C++ library.

Then you can use it through P/Invoke in your C# application. Some more useful link to learn P/Invoke:
http://en.wikipedia.org/wiki/P/Invoke[^],
http://msdn.microsoft.com/library/en-us/vcmxspec/html/vcmg_PlatformInvocationServices.asp[^].

This CodeProject article can also be useful:
Essential P/Invoke[^].

The alternative way is using C++/CLI. You can create a mixed-mode (managed + unmanaged) project which can use your C++ library. You can wrap your unmanaged library functions in some managed "ref" C++/CLI structures or classes and make the public. This way, the output of this project can be used as a regular .NET assembly, for example, referenced by your C# project. Please see:
http://en.wikipedia.org/wiki/C%2B%2B/CLI[^],
http://www.gotw.ca/publications/C++CLIRationale.pdf[^],
http://www.ecma-international.org/publications/standards/Ecma-372.htm[^],
http://msdn.microsoft.com/en-us/library/dtefa218[^],
http://msdn.microsoft.com/en-us/library/3bstk3k5[^].

—SA


请参见
See the P/Invoke tutorial[^] for details of parameter passing between C# and C++.


这篇关于在C#项目中使用C ++库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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