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

  • Home
  • SEARCH
  • 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 6827741
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:18:03+00:00 2026-05-26T22:18:03+00:00

I need to use a native DLL from C#. The DLL exposes several methods

  • 0

I need to use a native DLL from C#. The DLL exposes several methods which I can access via P/Invoke, and several types. All this code is in a standard NativeMethods class. To keep things simple it looks like this:

internal static class NativeMethods
{
    [DllImport("Foo.dll", SetLastError = true)]
    internal static extern ErrorCode Bar(ref Baz baz);

    internal enum ErrorCode { None, Foo, Baz,... } 

    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
    internal struct Baz
    {
        public int Foo;
        public string Bar;
    }
}

To achieve loose coupling I need to extract an interface and implement it using calls to NativeMethods:

public interface IFoo
{
    void Bar(ref Baz baz);
}

public class Foo : IFoo
{
    public void Bar(ref Baz baz)
    {
         var errorCode = NativeMethods.Bar(baz);
         if (errorCode != ErrorCode.None) throw new FooException(errorCode);
    }         
}

Now I can just use IFoo as a dependency in my code and mock it in tests:

public class Component : IComponent
{
     public Component(IFoo foo, IService service) { ... }
}

Something seems wrong here. NativeMethods must be internal according to FxCop. It makes sense then for IFoo (which was extracted from NativeMethods) to be internal too. But I can’t just make it internal b/c it is used in a public ctor (which should stay public). So: in order to achieve loose coupling I have to change the visibility of a component which otherwise would have been internal. What do you guys think about this?

Another problem: Component has method public void DoSomehing(Bar bar) which uses the Bar defined in NativeMethods.cs. I have to make that public too for this to work. This, or make a new Bar class which wraps NativeMethods+Bar. If I go the public way then NativeMethods becomes public too and FxCop complains that “Nested types should not be visible”. If I go the wrapper way… well, I feel it’s too much of an effort to do it for all the “native types”. Oh, there is the 3rd way: move the types away from NativeMethods and make them public. Then FxCop gets to analyze them and finds all the errors which were otherwise hidden when they were nested in NativeMethods. I really don’t know what’s the best way here…

  • 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-05-26T22:18:03+00:00Added an answer on May 26, 2026 at 10:18 pm

    It’s possible that a public abstract class is your friend here, instead of an interface.

    That can include internal abstract methods (referring to internal types), which actually makes it impossible to subclass in the normal way from outside the assembly (but InternalsVisibleTo will let you create a fake for testing).

    Basically interfaces haven’t really been designed as well as they could have been from a component aspect.

    This is exactly what I did in Noda Time for CalendarSystem – its API uses internal types, but I wanted to make it an interface or abstract class. I wrote about the oddities of access in a blog post which you may find interesting.

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

Sidebar

Related Questions

I need to use NSImage which appears need to be imported from <AppKit/AppKit.h> .
In C++/CLI , you can use native types in a managed class by it
I'm attempting to call native methods within a 3rd party DLL, which has a
I have a C++ dll that contains methods that I need to use in
Question: I use System.Data.OracleClient. System.Data.OracleClient requires OracleInstantClient, which are native dll's. So in order
Need to use own imaged markers instead built-in pins. I have several questions. 1.
I need to use sed to convert all occurences of ##XXX## to ${XXX} .
I need to use sendmail from Macs in an office. At the moment, I
I need to use a datetime.strptime on the text which looks like follows. Some
Sorry for the verbose introduction that follows. I need insight from someone knowing P/Invoke

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.