从C调用C# [英] Calling C from C#

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

问题描述

我是电气工程师的背景,因此我的知识是小的关于C#,DLL,等....我想使用C函数到一个C#。我知道有一对夫妇的帖子,但我没有找到一个是足够简单。



目前,我有C函数调用windows API读/写USB端口。首先创建一个.dll,我需要一个头文件吗?因为我得到了以下函数decleration函数到一个头。我在堆栈溢出和互联网上看到的例子只使用一个简单的.c文件,我可以摆脱头文件吗?

  __ declspec(dllexport)LMUSB_HANDLE __stdcall InitializeDevice(unsigned short usVID,
unsigned short usPID,
LPGUID lpGUID,
BOOL * pbDriverInstalled);
__declspec(dllexport)LMUSB_HANDLE __stdcall InitializeDeviceByIndex(unsigned short usVID,
unsigned short usPID,
LPGUID lpGUID,
DWORD dwIndex,
BOOL bOpenDataEndpoints,
BOOL * pbDriverInstalled);
__declspec(dllexport)BOOL __stdcall TerminateDevice(LMUSB_HANDLE hHandle);
__declspec(dllexport)BOOL __stdcall WriteUSBPacket(LMUSB_HANDLE hHandle,
unsigned char * pcBuffer,
unsigned long ulSize,
unsigned long * pulWritten);
__declspec(dllexport)DWORD __stdcall ReadUSBacket(LMUSB_HANDLE hHandle,
unsigned char * pcBuffer,
unsigned long ulSize,
unsigned long * pulRead,
unsigned long ulTimeoutMs,
HANDLE hBreak);
__declspec(dllexport)BOOL __stdcall Endpoint0Transfer(LMUSB_HANDLE hHandle,UCHAR ucRequestType,
UCHAR ucRequest,USHORT usValue,
USHORT usIndex,USHORT usLength,
PUCHAR pucBuffer,PUSHORT pusCount);第二点,我需要在cpp文件中编写__declspec(dllexport)吗?我们需要在cpp文件中编写__declspec(dllexport)。 这里是一个来自cpp的函数:

  externC__declspec(dllexport)BOOL PASCAL EXPORT TerminateDevice(LMUSB_HANDLE hUSB)

我不知道什么是BOOL PASCAL EXPORT,这个代码是从家具项目回收的。



Finaly,当DLL被正确构建。如何在C#项目中导入它?我尝试以下但未成功:

  [DllImport(lmusbdll.dll)] 



我看到你可以使用右键单击项目并添加引用,但Visual Studio弹出错误信息:

 无法添加对DLL路径的引用。请确保文件可访问,并且它是有效的程序集或COM组件。 



解决方案,但是当我尝试引用头文件时在我的c#项目。我仍然得到我不能引用该文件的消息。

解决方案

您不需要一个头文件,到__declspec(dllexport)的每个函数。如果是C ++(即.cpp文件),你需要externC。



有一个方便的程序,称为depends.exe, DLL,看看它导出什么函数,如果它取决于任何其他DLL。这可能是为什么你的DllImport失败。


I am from electric engineer background, therefore my knownledge is small about C#,DLL,etc.... I want to use c function into a C#. I know there is a couple post about that but I didn't find one that is enough simple.

Currently, I got C function call windows API to read/write on the USB port. First to create a .dll do I need a header file? Because I got the following function decleration function into a header. The examples that I saw on stack overflow and Internet only use a simple .c file, Can I get rid of the header files?

__declspec(dllexport) LMUSB_HANDLE  __stdcall InitializeDevice(unsigned short usVID,
                                    unsigned short usPID,
                                    LPGUID lpGUID,
                                    BOOL *pbDriverInstalled);
__declspec(dllexport) LMUSB_HANDLE  __stdcall InitializeDeviceByIndex(unsigned short usVID,
                                           unsigned short usPID,
                                           LPGUID lpGUID,
                                           DWORD dwIndex,
                                           BOOL bOpenDataEndpoints,
                                           BOOL *pbDriverInstalled);
__declspec(dllexport) BOOL  __stdcall TerminateDevice(LMUSB_HANDLE hHandle);
__declspec(dllexport) BOOL  __stdcall WriteUSBPacket(LMUSB_HANDLE hHandle,
                          unsigned char *pcBuffer,
                          unsigned long ulSize,
                          unsigned long *pulWritten);
__declspec(dllexport) DWORD  __stdcall ReadUSBPacket(LMUSB_HANDLE hHandle,
                         unsigned char *pcBuffer,
                         unsigned long ulSize,
                         unsigned long *pulRead,
                         unsigned long ulTimeoutMs,
                         HANDLE hBreak);
 __declspec(dllexport) BOOL  __stdcall Endpoint0Transfer(LMUSB_HANDLE hHandle, UCHAR ucRequestType,
                             UCHAR ucRequest, USHORT usValue,
                             USHORT usIndex, USHORT usLength,
                             PUCHAR pucBuffer, PUSHORT pusCount);

Second point, do I need to write __declspec(dllexport) in the cpp file? Here an function from the cpp:

extern "C" __declspec(dllexport) BOOL PASCAL EXPORT TerminateDevice(LMUSB_HANDLE hUSB)

I got no idea what "BOOL PASCAL EXPORT" does, this code is recycled from a furnisher project.

Finaly, when the DLL is properly build. How I import it in the C# project? I tried the following but without success :

[DllImport("lmusbdll.dll")]

I see that you could use right click on the project and add reference but Visual Studio pop-up an error message:

A reference to "DLL path" could not be added. Please make sure that the file is accessible, and that it is a valid assembly or COM component.

[EDIT]

I tried the following solution , but when I tried to reference the header file in my c# project. I still get the message that I cannot reference the file.

解决方案

You don't need a header file, but you will need to __declspec(dllexport) each function. And if it's C++ (i.e. a .cpp file) you'll need extern "C".

There's a handy program called depends.exe that allows you to look at a DLL and see what functions it exports and if it depends on any other DLLs. That might be why your DllImport is failing.

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

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