Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

158
Visualizações
Loading native dll's at runtime

Let’s say I have an extern method that is used to calculate the square root of a number. Like:

using System;
public static class Program 
{
    Public static extern double Sqrt(double x);

    public static void Main(string[] args) 
    {
        Console.WriteLine(Sqrt(64));
    }
}

For the extern method to work, I will have to load a DLL which is usually done by using DllImport. But let’s say I want to load the DLL at runtime by calling a method, like in Java:

System.load(“Path to DLL”);

Can this be done in C#?

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

You can import the same LoadLibrary that you're accustomed to using.

[DllImport("kernel32.dll")]
public static extern IntPtr LoadLibrary(string lib);
[DllImport("kernel32.dll")]
public static extern void FreeLibrary(IntPtr module);
[DllImport("kernel32.dll")]
public static extern IntPtr GetProcAddress(IntPtr module, string proc);

Example use (you should add error handling):

delegate int MyDelegate(int n, [MarshalAs(UnmanagedType.LPStr)] string s);

var module = LoadLibrary("Foo.dll");
var method = GetProcAddress(module, "doSomething");
var doSomething = (MyDelegate)Marshal.GetDelegateForFunctionPointer(method, typeof(MyDelegate));
var result = doSomething(2, "Hello world!"); 
FreeLibrary(module); 

See this article

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda