从C#调用C [英] Calling C from 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 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);
第二点,需要在cpp文件中写__declspec(dllexport)吗?这里有一个来自cpp的函数:
externC__declspec(dllexport)BOOL PASCAL EXPORT TerminateDevice(LMUSB_HANDLE hUSB)
我不知道BOOL PASCAL EXPORT是什么,这段代码是从家具项目中回收的。 >
最终,当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屋!