调用从C·C [英] Calling C from C#
问题描述
我是从电工程师背景,所以我knownledge很小关于C#,DLL等....我想用c函数为C#。我知道有一对夫妇的职位有关,但我没有找到一个足够简单。
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.
目前,我得到了C函数调用windows API来读取USB端口/写。首先,我需要一个头文件来创建一个.dll?因为我得到了以下功能decleration功能放入头。我对堆栈溢出和互联网看到的只是用一个简单的.c文件的例子,我可以摆脱的头文件?
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);
第二点,我需要在CPP文件中写入__declspec(dllexport)的?在这里,从一个CPP功能:
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)
我不知道什么是BOOL PASCAL EXPORT不,这code从供给者项目回收。
I got no idea what "BOOL PASCAL EXPORT" does, this code is recycled from a furnisher project.
Finaly,当DLL是正确地构建。我如何导入在C#项目?我尝试以下,但没有成功:
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")]
我看,你可以在项目中使用右键点击并添加引用,但Visual Studio中弹出的错误消息:
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.
我尝试以下解决方案的,但是当我试图引用头文件在我的C#项目。我仍然得到我不能引用的文件的消息。
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.
推荐答案
您不需要一个头文件,但你需要__declspec(dllexport)的各项功能。而如果它是C ++(即.cpp文件),你需要的externC。
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".
有一个名为方便的Depends.exe计划,让您看一个DLL,看看它的功能和出口,如果它依赖于任何其他DLL。这可能是为什么你的DllImport失败。
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屋!