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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T06:12:08+00:00 2026-06-18T06:12:08+00:00

So I created the following test project: [DllImportAttribute(TestMFCDLL.dll, CallingConvention = CallingConvention.Cdecl)] internal static extern

  • 0

So I created the following test project:

[DllImportAttribute("TestMFCDLL.dll", CallingConvention = CallingConvention.Cdecl)]
internal static extern int test(int number);

private void button1_Click(object sender, EventArgs e)
{
    int x = test(5);
}

Which works fine for my MFC dll that has the function test defined, however what I actually have is many MFC dlls that all share a common entry function and run differently based on my inputs. So basically I have tons of dlls that I cannot know at compile time what the name is, I just know that they have a function similar to how this program is setup, is there a way to import a dll based on run-time knowledge? Simply doing this returns an error:

static string myDLLName = "TestMFCDLL.dll";
[DllImportAttribute(myDLLName, CallingConvention = CallingConvention.Cdecl)]

An attribute argument must be a constant expression, typeof expression
or array creation expression of an attribute parameter type

  • 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-18T06:12:10+00:00Added an answer on June 18, 2026 at 6:12 am

    If you want to dynamically load a DLL and use the functions in the DLL, then you’ll need to do a little bit more. First you need to load the DLL dynamically. You can use LoadLibrary and FreeLibrary for that.

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

    Second you need to get the address of the function in the DLL and call it.

    [DllImport("kernel32.dll")]
    public static extern IntPtr GetProcAddress(IntPtr hModule, string functionName);
    
    [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
    private delegate int Test(int number);
    

    Putting that all together:

    IntPtr pLib = LoadLibrary(@"PathToYourDll.DLL");
    IntPtr pAddress = GetProcAddress(pLib, "test");
    Test test = (Test)Marshal.GetDelegateForFunctionPointer(pAddress, typeof(Test));
    int iRresult = test(0);
    bool bResult = FreeLibrary(pLib);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I created a Test project that I want to delete, using the following command:
I created web App Test Project using the following Maven command: mvn archetype:generate -DgroupId=com.vanilla.cache
I'm following this CodeFramework example and created a test project after installed Code Framework
Okay, so I created a JavaScript file called test.js which contains the following code:
I have the following test-code: CREATE TABLE #Foo (Foo int) INSERT INTO #Foo SELECT
I created a following class within a namespace Global namespace Global { public static
Ok, so I created a test project just to verify that jQuery AJAX works
To get started with Selenium2 and the HtmlUnitDriver I created a test project. As
I have installed the Vaadin plugin for Eclipse. I created a test sample project,
I created a WPF test project, single Window with a single button in it.

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.