Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/157.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 导入:使用C+的语法翻译+;C语言下的图书馆# 目前我尝试使用C++语言在C++中使用DLL导入。这个库称为拦截。 问题是我不知道如何翻译#定义头文件的条目和typedef声明:_C#_C++_Dll - Fatal编程技术网

C# 导入:使用C+的语法翻译+;C语言下的图书馆# 目前我尝试使用C++语言在C++中使用DLL导入。这个库称为拦截。 问题是我不知道如何翻译#定义头文件的条目和typedef声明:

C# 导入:使用C+的语法翻译+;C语言下的图书馆# 目前我尝试使用C++语言在C++中使用DLL导入。这个库称为拦截。 问题是我不知道如何翻译#定义头文件的条目和typedef声明:,c#,c++,dll,C#,C++,Dll,我尝试使用“using”指令,但没有成功(我无法访问void定义)。 此外,我不明白uu declspec(dllimport)在这个标题中的作用。在我的c#项目中,我只是忽略了它?这样做好吗 这是我想在c#中使用的代码(这是库的一个示例) 编辑: 我尝试过的:基本输入: [DllImport("interception.dll", CharSet = CharSet.Auto, SetLastError = true)] void interception_set_filter(vo

我尝试使用“using”指令,但没有成功(我无法访问void定义)。 此外,我不明白uu declspec(dllimport)在这个标题中的作用。在我的c#项目中,我只是忽略了它?这样做好吗

这是我想在c#中使用的代码(这是库的一个示例)

编辑:

我尝试过的:基本输入:

[DllImport("interception.dll", CharSet = CharSet.Auto, SetLastError = true)]
    void interception_set_filter(void* context, InterceptionPredicate predicate, ushort filter);

我不知道如何转换拦截谓词。根据头文件,拦截过滤器是一个惯例,而拦截上下文是一个空指针(Vault*)。

< P> C++库应该编译为.dll文件。此.DLL文件应具有导出的函数。您可以使用该工具检查从.DLL导出的内容。NET代码可以调用所谓的“平台调用”的C++导出函数。 现在,我强烈建议你深入研究这一点,这将指导你


注:
void*
应以c#作为IntPtr声明<代码>枚举应重新声明为枚举。函数应该声明为用“代码> > dLimPult属性标记的静态外部方法。

< P> C++库应编译为.dll文件。此.DLL文件应具有导出的函数。您可以使用该工具检查从.DLL导出的内容。NET代码可以调用所谓的“平台调用”的C++导出函数。 现在,我强烈建议你深入研究这一点,这将指导你


注:
void*
应以c#作为IntPtr声明<代码>枚举应重新声明为枚举。函数应该声明为带有
DllImport
属性的静态外部方法。

首先,看起来您正在尝试实现全局键盘/鼠标挂钩。。如果是这样,我建议谷歌搜索“C#low level keyboard and mouse hook”

现在,首先,是代码< > O.DEXSPECU/DCOD>问题:如果你在C++应用中实际使用了头,那就是C++代码> DLLimPoto<代码>的C++等价物。所以实际上你并没有忽略它,而是实现了它。在C++中,它只告诉链接器,声明的函数将从特定的DLL导入而不是本地函数(与C语言<代码> dLimPurth 接下来是函数指针问题(InterceptionPredicate)。在标题中,其定义如下:

typedef int (*InterceptionPredicate)(InterceptionDevice device);
InterceptionDevice
只是一个“int”。因此,InterceptionPredicate只是一种函数指针类型(或C#中的委托),因此InterceptionPredicate的委托定义如下所示:

// [UnmanagedFunctionPointer(CallingConvention.Winapi)]
public delegate int InterceptionPredicate (int device);
关于UnmanagedFunctionPointer调用约定描述符的注意事项:如果您知道导出函数可能使用哪种类型的调用约定(stdcall、fastcall、cdecl),可以在此处指定,以便.NET封送处理程序知道如何在托管/非托管代码之间传递数据,但是如果你不知道或者没有特别说明,你可以把它关掉

另外,正如其他人所提到的,除非在C#属性中指定了“不安全”标志,
void*
类型在C#中应该始终是
IntPtr

另外,请确保将C#代码中的dll函数标记为
public static extern
,请参见下面的示例

因此,要为您指定的函数制作一个示例,可以执行以下操作:

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

namespace InterceptorTest
{
    public class Interceptor : IDisposable
    {
        #region DllImports

        [DllImport("interception.dll", CharSet = CharSet.Auto, SetLastError = true)]
        public static extern IntPtr interception_create_context();

        [DllImport("interception.dll", CharSet = CharSet.Auto, SetLastError = true)]
        public static extern void interception_destroy_context(IntPtr context);

        [DllImport("interception.dll", CharSet = CharSet.Auto, SetLastError = true)]
        public static extern void interception_set_filter(IntPtr context, InterceptionPredicate predicate, ushort filter);

        // The function pointer type as defined in interception.h that needs to be defined as a delegate here
        public delegate int InterceptionPredicate(int device);

        #endregion

        #region private members

        private InterceptionPredicate m_PredicateDelegate { get; set; }
        private IntPtr m_Context { get; set; }

        #endregion

        #region methods

        public Interceptor(ushort filter)
        {
            // be sure to initialize the context
            this.m_PredicateDelegate = new InterceptionPredicate(this.DoSomethingWithInterceptionPredicate);
            this.m_Context = interception_create_context();
            interception_set_filter(this.m_Context, this.m_PredicateDelegate, filter);
        }

        private void Cleanup()
        {
            interception_destroy_context(this.m_Context);
            // the next line is not really needed but since we are dealing with
            // managed to unmanaged code it's typically best to set to 0
            this.m_Context = IntPtr.Zero;
        }

        public void Dispose()
        {
            this.Cleanup();
            GC.SuppressFinalize(this);
        }

        protected virtual void Dispose(bool disposing)
        {
            if (disposing) { this.Cleanup(); }
        }

        public int DoSomethingWithInterceptionPredicate(int device)
        {
            // this function is something you would define that would do something with
            // the device code (or whatever other paramaters your 'DllImport' function might have
            // and return whatever interception_set_filter is expecting
            return device;
        }

        #endregion
    }

    static class Program
    {
        [STAThread]
        private static void Main(string[] argv)
        {
            Interceptor icp = new Interceptor(10);
            // do something with the Interceptor object
        }
    }
}

希望这能让你走上正轨。

首先,看起来你正在尝试实现一个全局键盘/鼠标挂钩。。如果是这样,我建议谷歌搜索“C#low level keyboard and mouse hook”

现在,首先,是代码< > O.DEXSPECU/DCOD>问题:如果你在C++应用中实际使用了头,那就是C++代码> DLLimPoto<代码>的C++等价物。所以实际上你并没有忽略它,而是实现了它。在C++中,它只告诉链接器,声明的函数将从特定的DLL导入而不是本地函数(与C语言<代码> dLimPurth 接下来是函数指针问题(InterceptionPredicate)。在标题中,其定义如下:

typedef int (*InterceptionPredicate)(InterceptionDevice device);
InterceptionDevice
只是一个“int”。因此,InterceptionPredicate只是一种函数指针类型(或C#中的委托),因此InterceptionPredicate的委托定义如下所示:

// [UnmanagedFunctionPointer(CallingConvention.Winapi)]
public delegate int InterceptionPredicate (int device);
关于UnmanagedFunctionPointer调用约定描述符的注意事项:如果您知道导出函数可能使用哪种类型的调用约定(stdcall、fastcall、cdecl),可以在此处指定,以便.NET封送处理程序知道如何在托管/非托管代码之间传递数据,但是如果你不知道或者没有特别说明,你可以把它关掉

另外,正如其他人所提到的,除非在C#属性中指定了“不安全”标志,
void*
类型在C#中应该始终是
IntPtr

另外,请确保将C#代码中的dll函数标记为
public static extern
,请参见下面的示例

因此,要为您指定的函数制作一个示例,可以执行以下操作:

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

namespace InterceptorTest
{
    public class Interceptor : IDisposable
    {
        #region DllImports

        [DllImport("interception.dll", CharSet = CharSet.Auto, SetLastError = true)]
        public static extern IntPtr interception_create_context();

        [DllImport("interception.dll", CharSet = CharSet.Auto, SetLastError = true)]
        public static extern void interception_destroy_context(IntPtr context);

        [DllImport("interception.dll", CharSet = CharSet.Auto, SetLastError = true)]
        public static extern void interception_set_filter(IntPtr context, InterceptionPredicate predicate, ushort filter);

        // The function pointer type as defined in interception.h that needs to be defined as a delegate here
        public delegate int InterceptionPredicate(int device);

        #endregion

        #region private members

        private InterceptionPredicate m_PredicateDelegate { get; set; }
        private IntPtr m_Context { get; set; }

        #endregion

        #region methods

        public Interceptor(ushort filter)
        {
            // be sure to initialize the context
            this.m_PredicateDelegate = new InterceptionPredicate(this.DoSomethingWithInterceptionPredicate);
            this.m_Context = interception_create_context();
            interception_set_filter(this.m_Context, this.m_PredicateDelegate, filter);
        }

        private void Cleanup()
        {
            interception_destroy_context(this.m_Context);
            // the next line is not really needed but since we are dealing with
            // managed to unmanaged code it's typically best to set to 0
            this.m_Context = IntPtr.Zero;
        }

        public void Dispose()
        {
            this.Cleanup();
            GC.SuppressFinalize(this);
        }

        protected virtual void Dispose(bool disposing)
        {
            if (disposing) { this.Cleanup(); }
        }

        public int DoSomethingWithInterceptionPredicate(int device)
        {
            // this function is something you would define that would do something with
            // the device code (or whatever other paramaters your 'DllImport' function might have
            // and return whatever interception_set_filter is expecting
            return device;
        }

        #endregion
    }

    static class Program
    {
        [STAThread]
        private static void Main(string[] argv)
        {
            Interceptor icp = new Interceptor(10);
            // do something with the Interceptor object
        }
    }
}

希望这能让您走上正轨。

好的,我找到了一个隐藏在GIT上的示例代码。谢谢谷歌


通过一个工作示例,它对DLL导入的所有函数进行了精确计算。

好的,我找到了一个隐藏在GIT上的示例代码。谢谢谷歌


它从DLL导入中提炼出所有函数,并有一个工作示例。

为什么不直接调用C++库,而不是试图将它转换成C?