用指针调用非托管代码

姆莱加

我有一个C#项目,它可以调用非托管C ++ dll。包装程序和大多数调用都可以正常运行,因此我知道我具有所有组件如何正常连接的基本结构,但是有一个特定的调用适合我。API调用需要指向结构的指针,该结构包含配置数据列表。

这里是电话:

m_status = m_XXXXBox.SetConfig(m_channelId, ref SCONFIG_LIST);

其中SCONFIG_LIST是包含数据的结构...

该问题特别与SCONFIG_LIST有关

以下是直接来自此API规范的文档:

Points to the structure SCONFIG_LIST, which is defined as follows:
typedef struct
{
unsigned long NumOfParams; /* number of SCONFIG elements */
SCONFIG *ConfigPtr; /* array of SCONFIG */
} SCONFIG_LIST
where:
NumOfParms is an INPUT, which contains the number of SCONFIG elements in the array
pointed to by ConfigPtr.
ConfigPtr is a pointer to an array of SCONFIG structures.
The structure SCONFIG is defined as follows:
typedef struct
{
unsigned long Parameter; /* name of parameter */
unsigned long Value; /* value of the parameter */
} SCONFIG

这是我在C#中定义的2种结构:

[StructLayout(LayoutKind.Sequential, Pack = 1)]
public unsafe struct SConfig
{
    public int Parameter;
    public int Value;
}



[StructLayout(LayoutKind.Sequential, Pack = 1)]
public unsafe struct SConfig_List
{
    public int NumOfParams;
    // public List<SConfig> sconfig = new List<SConfig>();  // This throws compile time error
    public List<SConfig> sconfig;
}

我知道您不能在结构体中使用字段初始值设定项,但是我似乎无法弄清楚如何从外部初始化结构体中的sconfig ...这是调用方法的代码段

      SConfig_List myConfig = new SConfig_List();
      SConfig configData = new SConfig();

      configData.Parameter = 0x04;
      configData.Value = 0x10;
      myConfig.NumOfParams = 1;
      myConfig.sconfig.Add(configData);

这会在运行时引发“对象引用未设置为对象实例”的错误,我理解此错误,因为sconfig尚未初始化-我只是不知道该怎么做...。

所以我的下一个想法是解决这个问题,我将像这样创建SCONFIG_LIST结构(里面没有列表)-我的理由是我现在不必初始化该对象,我可以对它进行多次调用NumOfParams = 1而不是NumOfParams> 1且具有dll循环遍历结构数据的dll。

[StructLayout(LayoutKind.Sequential, Pack = 1)]
public unsafe struct SConfig_List
{
    public int NumOfParams;
    public SConfig sconfig;
}

这就是我所说的方法

configData.Parameter = 0x04;
configData.Value = 0x10;
myConfig.NumOfParams = 1;
myConfig.sconfig.Parameter = configData.Parameter;
myConfig.sconfig.Value = configData.Value;

m_status = m_XXXXBox.SetConfig(m_channelId, ref myConfig);

至此,现在已经消除了错误,现在在调用dll的实际方法上,编组仍然存在一些问题,但是这里是:

public XXXXErr SetConfig(int channelId, ref SConfig_List config)
{
    unsafe
    {
        IntPtr output = IntPtr.Zero;
        IntPtr input = Marshal.AllocHGlobal(Marshal.SizeOf(config));
        Marshal.StructureToPtr(config, input, true);

        XXXXErr returnVal = (XXXXErr)m_wrapper.Ioctl(channelId, (int)Ioctl.SET_CONFIG, input, output);
        return returnVal;
    } 
}

这通过了所有初始设置,没有错误,但是当我尝试实际调用dll时,我得到一个错误:尝试读取或写入受保护的内存。这通常表明其他内存已损坏。

我知道这是一个大问题,我真的不知道该问什么,因为我确信这篇文章中存在多个问题,但是有什么想法可以使我走上正确的道路?

在这一点上,我已经尝试了很多东西,我很茫然,我只需要一些指导。我不是在寻找“为我做这件事”类型的答案,而是在寻找一个解释,也许还有一些完成此想法的指示。与所有事物一样,我确信完成任务有多种方式-可能有效,但不是好的形式,而更长的更复杂的方式可能是“更好的实践”

任何和所有建议/评论将不胜感激。如果我排除了帮助解决该难题所需的任何相关数据,请告诉我,我将尽我所能。


我要感谢到目前为止的答复。我一直在尝试各种组合尝试自己解决此问题,但到目前为止我还没有任何运气。我发现了很多无效的方法,但是:-)

我已经尝试过各种“不安全”-“ MarshalAs”,“ StructLayout”和我在网上找到的其他几种组合,现在我求饶。

我已经成功实现了对该非托管dll的其他几个调用,但是它们全部使用简单的整数指针等。我的问题是将指针传递给包含另一个结构数组的Struct。如果您查看我的原始问题的最顶部,则可以查看dll中的文档以及它如何使事物结构化。没有返回值,我只是试图通过此dll将一些配置设置传递给设备。

我将发布整个项目的框架,以便也许可以让某人在这个过程中执着,并希望将来也能帮助其他人尝试解决此类问题。

这是包装器的骨架(并非显示所有功能)

using System;
using System.Runtime.InteropServices;

namespace My_Project
{
    internal static class NativeMethods
    {
        [DllImport("kernel32.dll")]
        public static extern IntPtr LoadLibrary(string dllToLoad);

        [DllImport("kernel32.dll")]
        public static extern IntPtr GetProcAddress(IntPtr hModule, string procedureName);

        [DllImport("kernel32.dll")]
        public static extern bool FreeLibrary(IntPtr hModule);
    }

    internal class APIDllWrapper
    {
        private IntPtr m_pDll;

        [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
        public delegate int APIIoctl(int channelId, int ioctlID, IntPtr input, IntPtr output);
        public APIIoctl Ioctl;
        //extern “C” long WINAPI APIIoctl
        //(
        //unsigned long ChannelID,
        //unsigned long IoctlID,
        //void *pInput,
        //void *pOutput
        //)

        public bool LoadAPILibrary(string path)
        {
            m_pDll = NativeMethods.LoadLibrary(path);

            if (m_pDll == IntPtr.Zero)
                return false;

            pAddressOfFunctionToCall = NativeMethods.GetProcAddress(m_pDll, "APIIoctl");
            if (pAddressOfFunctionToCall != IntPtr.Zero)
                Ioctl = (APIIoctl)Marshal.GetDelegateForFunctionPointer(
                                                                                        pAddressOfFunctionToCall,
                                                                                        typeof(APIIoctl));
            return true;
        }

        public bool FreeLibrary()
        {
            return NativeMethods.FreeLibrary(m_pDll);
        }
    }
}


And Here is the class that defines the hardware I am trying to communicate with
    namespace My_Project
{
    public class APIDevice
    {
        public string Vendor { get; set; }
        public string Name { get; set; }

        public override string ToString()
        {
            return Name;
        }
    }
}


Interface
    using System.Collections.Generic;

namespace My_Project
{
    public interface I_API
    {
        APIErr SetConfig(int channelId, ref SConfig_List config);
    }
}

包含API代码的实际类-这是错误所在,我知道我现在拥有IntPtrs的方法不正确-但这显示了我正在尝试做的事情

using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;

namespace My_Project
{
    public class API : I_API
    {
        private APIDevice m_device;
        private APIDllWrapper m_wrapper;

        public APIErr SetConfig(int channelId, ref SConfig_List config)
        {
            IntPtr output = IntPtr.Zero;
            IntPtr input = Marshal.AllocHGlobal(Marshal.SizeOf(config));
            Marshal.StructureToPtr(config, input, true);

            APIErr returnVal = (APIErr)m_wrapper.Ioctl(channelId, (int)Ioctl.SET_CONFIG, input, output);
            return returnVal;             
        }       
    }
}

这是包含我正在使用的Structs的定义的类

using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;

namespace My_Project
{
    public enum APIErr
    {
        STATUS_NOERROR = 0x00,
        ERR_BUFFER_EMPTY = 0x10,
        ERR_BUFFER_FULL = 0x11,
        ERR_BUFFER_OVERFLOW = 0x12
    }

    public struct SConfig
    {
        public int Parameter;
        public int Value;
    }

    public struct SConfig_List
    {
        public int NumOfParams;
        public SConfig[] sconfig;

        public SConfig_List(List<SConfig> param)
        {
            this.NumOfParams = param.Count;
            this.sconfig = new SConfig[param.Count];
            param.CopyTo(this.sconfig);
        }
    }
}

最后-实际的应用程序通过包装程序调用dll

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using My_Project;

namespace Test_API
{
    public class Comm
    {
        private I_API m_APIBox;
        APIErr m_status;
        int m_channelId;
        bool m_isConnected;

        public Comm(I_API apiInterface)
        {
            m_APIBox = apiInterface;
            m_isConnected = false;
            m_status = APIErr.STATUS_NOERROR;
        }

        public bool ConfigureDevice()
        {
            SConfig tempConfig = new SConfig();

            tempConfig.Parameter = 0x04;
            tempConfig.Value = 0x10;
            SConfig_List setConfig = new SConfig_List(tempConfig);

            m_status = m_APIBox.SetConfig(m_channelId, ref setConfig);
            if (m_status != APIErr.STATUS_NOERROR)
            {
                m_APIBox.Disconnect(m_channelId);
                return false;
            }
            return true;
        }
    }
}
姆莱加

我启动另一个线程是因为由于我的经验不足而提出了错误的问题

工作解决方案在这里

struct和IntPtr的元数据数组

谢谢您的帮助

-李

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

从C#调用非托管代码

来自分类Dev

托管代码与非托管代码

来自分类Dev

将托管引用(this)传递给非托管代码并调用托管回调

来自分类Dev

从 c# 调用非托管代码。使用 IntPtr 获取数据

来自分类Dev

用非托管内存指针交换固定数组的指针

来自分类Dev

使用固定指针将大型数据结构传递给非托管代码

来自分类Dev

混合托管和非托管代码问题

来自分类Dev

从C#调用非托管C DLL函数(具有指向struct的指针和指向bool数组的指针)

来自分类Dev

从托管代码调用本机函数

来自分类Dev

C#非托管代码调用不起作用:AddConsoleAlias

来自分类Dev

从非托管代码传播到托管代码的异常?

来自分类Dev

清理非托管代码中的内存泄漏

来自分类Dev

非托管C ++代码上的NullReferenceException

来自分类Dev

从非托管C ++代码访问WPF

来自分类Dev

托管和非托管代码之间的传递结构

来自分类Dev

从C#调用非托管C ++

来自分类Dev

对静态库的非托管C#调用

来自分类Dev

从C#调用非托管C ++

来自分类Dev

用Emscripten调用函数指针

来自分类Dev

如何使用结构指针导入非托管dll?

来自分类Dev

导出非托管函数指针时访问冲突

来自分类Dev

在C ++ / CLI中包装非托管指针时,堆损坏

来自分类Dev

P /调用-从C#调用非托管DLL

来自分类Dev

用传递函数指针调用_beginthreadx

来自分类常见问题

如何将非托管依赖项导入代码

来自分类Dev

使用DLLImport将数据发送到非托管代码

来自分类Dev

Microsoft Fake框架是否支持C ++ /非托管代码

来自分类Dev

与ASP.net vNext中的非托管代码互操作

来自分类Dev

如何正确处理pinvoke /非托管代码