简体   繁体   English

C#如何从C ++ dll调用函数

[英]C# How to call function from C++ dll

I have the following code that is implemented in a dll file 我有以下在dll文件中实现的代码

typedef  signed __int16 TS_ELEMENT;
typedef  TS_ELEMENT *   TS_DATA;
LIBTERAVIEW_API TS_RES tsRead(TS_DATA buffer);

I would like to call this function in my C# program and use it in order to see data that is printed from it. 我想在我的C#程序中调用此函数,并使用它来查看从中打印的数据。 I have read about unmanaged structures and importing functions from DLLs; 我已经阅读了有关非托管结构和从DLL导入函数的信息。 however, I am not able to figure out the syntax of how to do this in my code. 但是,我无法在代码中弄清楚如何执行此操作的语法。 I have generated the following codes based on my readings on this topic: 根据有关该主题的阅读,我生成了以下代码:

[DllImport("libteraview.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern void tsStart();

[DllImport("libteraview.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern byte* tsRead(byte* buffer);`

The first one works however, I am not able to get the second method to run and read data as it always gives me an Unhandled Exception error. 但是第一个有效,但我无法获得第二个方法来运行和读取数据,因为它始终会给我带来未处理的异常错误。

I would appreciate any help on how to get this tsRead() function imported from a DLL. 我将对如何从DLL导入此tsRead()函数提供帮助。

Please check Return type and Parameter type for tsRead() function. 请检查tsRead()函数的返回类型和参数类型。 It should be int instead byte. 应该是int而不是字节。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM