Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

157
Views
Cargando dll nativos en tiempo de ejecución

Digamos que tengo un método externo que se usa para calcular la raíz cuadrada de un número. Me gusta:

 using System; public static class Program { Public static extern double Sqrt(double x); public static void Main(string[] args) { Console.WriteLine(Sqrt(64)); } }

Para que funcione el método externo, tendré que cargar una DLL que generalmente se realiza mediante DllImport . Pero digamos que quiero cargar la DLL en tiempo de ejecución llamando a un método, como en Java:

 System.load(“Path to DLL”);

¿Se puede hacer esto en C#?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Puede importar la misma LoadLibrary que está acostumbrado a usar.

 [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);

Ejemplo de uso (debe agregar manejo de errores):

 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);

ver este artículo

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!