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 802801
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T23:39:37+00:00 2026-05-14T23:39:37+00:00

I am doing an MxRecordLookup. I am getting an error when calling the DnsRecordListFree

  • 0

I am doing an MxRecordLookup. I am getting an error when calling the DnsRecordListFree in the .NET Framework 4.0. I am using Windows 7. How do I get around it? Here is the error:

System.MethodAccessException: Attempt by security transparent method to call native code through method.

Here is my code:

    [DllImport("dnsapi", EntryPoint = "DnsQuery_W", CharSet = CharSet.Unicode, SetLastError = true, ExactSpelling = true)]
    private static extern int DnsQuery([MarshalAs(UnmanagedType.VBByRefStr)]ref string pszName, QueryTypes wType, QueryOptions options, int aipServers, ref IntPtr ppQueryResults, int pReserved);

    [DllImport("dnsapi", CharSet = CharSet.Auto, SetLastError = true)]
    private static extern void DnsRecordListFree(IntPtr pRecordList, int FreeType);

    public List<string> GetMXRecords(string domain)
    {
        List<string> records = new List<string>();
        IntPtr ptr1 = IntPtr.Zero;
        IntPtr ptr2 = IntPtr.Zero;
        MXRecord recMx;

        try
        {

            int result = DnsQuery(ref domain, QueryTypes.DNS_TYPE_MX, QueryOptions.DNS_QUERY_BYPASS_CACHE, 0, ref ptr1, 0);
            if (result != 0)
            {
                if (result == 9003)
                {
                    //No Record Exists
                }
                else
                {
                    //Some other error
                }
            }

            for (ptr2 = ptr1; !ptr2.Equals(IntPtr.Zero); ptr2 = recMx.pNext)
            {
                recMx = (MXRecord)Marshal.PtrToStructure(ptr2, typeof(MXRecord));
                if (recMx.wType == 15)
                {
                    records.Add(Marshal.PtrToStringAuto(recMx.pNameExchange));
                }
            }
        }
        finally
        {
            DnsRecordListFree(ptr1, 0);
        }

        return records;
    }
  • 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-14T23:39:37+00:00Added an answer on May 14, 2026 at 11:39 pm

    Read Security Changes in the .NET Framework 4 to understand what transparent and critical code is. By default, in .NET 4, any code in an assembly marked with AllowPartiallyTrustedCallersAttribute is transparent, which means it cannot call critical code (code marked with SecurityCriticalAttribute). Only security safe critical (marked with SecuritySafeCriticalAttribute) or critical code can call critical code. And security safe critical can be called by transparent called.

    In short:

    Transparent can call Transparent or Security Safe Critical
    Security Safe Critical can call Security Safe Critical or Critical
    Critical can call Critical

    Enable Code Analysis with the Microsoft Security rules set to see warnings about bad security calls. Note that you can revert back to how security worked in .NET Framework 2.0 (all code is critical) by applying [assembly: SecurityRules(SecurityRuleSet.Level1)] or removing AllowPartiallyTrustedCallersAttribute. See Security-Transparent Code, Level 2 for more information.

    To comply with the new rules, GetMXRecords should become SecuritySafeCritical and the DLL import calls must be marked with SecurityCritical.

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

Sidebar

Related Questions

When doing a cvs update , you get a nice summary of the state
When doing a simple performance measurement, I was astonished to see that calling String.IndexOf(char)
Doing odd/even styling with jQuery is pretty easy: $(function() { $(.oddeven tbody tr:odd).addClass(odd); $(.oddeven
When doing an INSERT with a lot of data, ie: INSERT INTO table (mediumtext_field)
When doing small icons, header graphics and the like for websites, is it better
When doing case-insensitive comparisons, is it more efficient to convert the string to upper
When doing TDD , how to tell that's enough tests for this class /
I doing a function in Javascript like the VisualBasic DateDiff. You give two dates
When doing thread synchronization in C# should I also lock an object when I
When doing an ALTER TABLE statement in MySQL, the whole table is read-locked (allowing

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.