内存映射IO(MMIO)在导入本机dll时无法在C#中工作 [英] Memory Mapped IO (MMIO) not working in C# on importing a native dll

查看:112
本文介绍了内存映射IO(MMIO)在导入本机dll时无法在C#中工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个本机DLL,它使用内存映射IO来控制并行端口等设备读取总是返回十六进制字符"C"。有没有办法解决这个问题。我正在使用DLL导入来加载DLL([DllImport(" dll name")])。
请帮助我解决这个问题

i have a native DLL that uses memory mapped IO to control the devices like parallel port, etc. The reads are always returning the hex character 'C'. is there a way to solve this issue. i'm using the DLL import to load the DLL ([DllImport("dll name")]). kindly help me to solve this problem

推荐答案

Hi Prasanth Subramani,

Hi Prasanth Subramani,

感谢您发布此处。

如果您想从C#项目调用本机DLL,您可以尝试以下方法。

If you want to call native DLL from C# project, you may try the following method.

在C ++中使用显式PInvoke(DllImport属性)

使用C ++ Interop(隐式) PInvoke)

如果你的原生dll是用C语言构建的。

If your native dll is built by C language.

使用PInvoke不适合DLL中的所有C风格函数。例如,假设mylib.dll中的函数MakeSpecial声明如下:

Using PInvoke is not appropriate for all C-style functions in DLLs. For example, suppose there is a function MakeSpecial in mylib.dll declared as follows:

char * MakeSpecial(char * pszString);

如果我们在Visual C ++应用程序中使用PInvoke,我们可能会写类似于以下内容:

If we use PInvoke in a Visual C++ application, we might write something similar to the following:

[DllImport( " mylib")]

extern" C" String * MakeSpecial([MarshalAs(UnmanagedType :: LPStr)] String ^);

这里的难点在于我们无法删除返回的非托管字符串的内存通过MakeSpecial。通过PInvoke调用的其他函数返回一个指向内部缓冲区的指针,该缓冲区不必由用户释放。在这种情况下,使用
IJW功能是显而易见的选择。

The difficulty here is that we cannot delete the memory for the unmanaged string returned by MakeSpecial. Other functions called through PInvoke return a pointer to an internal buffer that does not have to be deallocated by the user. In this case, using the IJW feature is the obvious choice.

有关详细信息,请参阅此文档。

For more information, please refer to this document.

https://msdn.microsoft.com/en-us /library/ms235282.aspx

https://blogs.msdn.microsoft.com/jonathanswift/2006/10/03/dynamically-calling-an-unmanaged-dll-from- net -c /

最好的问候,

Hart


这篇关于内存映射IO(MMIO)在导入本机dll时无法在C#中工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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