Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8763731
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T15:51:05+00:00 2026-06-13T15:51:05+00:00

Currently I’m trying to use a C++ library under C# using DLL importation. Library

  • 0

Currently I’m trying to use a C++ library under C# using DLL importation. Library is called Interception.
The problem is that I don’t know how to translate #define entries and typedef declaration of the header file:

https://github.com/oblitum/Interception/blob/master/include/interception.h

I tried to use “using” directive, but with no success (I can’t access to the void definition).
Moreover, I didn’t understood the role of __declspec(dllimport) in this header. In my c# project, I just ignored it? Is it good to do that?

This is the code I want to use in c# (it’s a sample of the library)

https://github.com/oblitum/Interception/blob/master/samples/hardwareid/main.cpp

EDIT:

What I’ve tried: basic importation:

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

I don’t know ho to convert InterceptionPredicate. According the header file, InterceptionFilter is a ushort, and InterceptionContext is a void pointer (void*).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T15:51:06+00:00Added an answer on June 13, 2026 at 3:51 pm

    First, it looks like you’re trying to implement a global keyboard/mouse hook .. if that’s the case, I’d recommend googling ‘C# low level keyboard and mouse hook’.

    Now for your question, first is the __declspec(dllimport) issue: this would be if you were actually using the header in a C++ application, that is the C++ equivilent of the C# DllImport .. so in effect you didn’t ignore it, you implemented it. In C++ it just tells the linker that the function declared as such will be imported from a specific DLL instead of it being a local function (pretty similar to what the C# DllImport directive does)

    Next is for function pointer issue (InterceptionPredicate). In the header it is defined as such:

    typedef int (*InterceptionPredicate)(InterceptionDevice device);
    

    And InterceptionDevice is just an ‘int’. So the InterceptionPredicate is just a function pointer type (or Delegate in C#), so your delegate definition for InterceptionPredicate would look like this:

    // [UnmanagedFunctionPointer(CallingConvention.Winapi)]
    public delegate int InterceptionPredicate (int device);
    

    A note about the UnmanagedFunctionPointer calling convention descriptor: IF you know what kind of calling convention (stdcall, fastcall, cdecl) the exported function might be using, you could specify here so that the .NET marshaler will know how to pass the data between the managed/unmanaged code, but if you don’t know it or it’s not specified typically you can just leave that off.

    Also, as others have mentioned, unless you have the ‘unsafe’ flag specified in your C# properties, a void* type should always be an IntPtr in C#.

    Also, be sure to mark the dll function in your C# code as public static extern, see example below.

    So to make an example of the function you’ve specified, here’s what could be done:

    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
            }
        }
    }
    

    Hope that gets you on the right track.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Currently I am trying to use a bunch of custom perl modules, test.pm as
Currently I know of only two ways to cache data (I use PHP but
Currently, I am writing a MiddleWare application that synchronizes information between and accounting application
Currently I am using HTML files for parts of my user interface. I display
currently, I`m implementing a map App with Mono4Droid and there I`m using a WebView
Currently working with converting SQLException error messages into messages that are more useful for
Currently I'm using a gridview with an objectdatasource to display a table of product
Currently using SASS on a website build. It is my first project using it,
Currently, I have a loop in my program that has the format: #include<stdio.h> int
Currently I am facing the following problem, which I'm working in Stata to solve.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.