Bonjour,
Est-il possible d'appeler des fonctions d'une dll développée en C# en utilisant les APIs LoadLibrary et GetProcAddress?
J'ai le code suivant :
-------------------Code-------------------------------
-------------------------------------------------------
Code c# : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50 namespace mcClientWithoutAddReference { static class NativeMethods { [DllImport("kernel32.dll")] public static extern IntPtr LoadLibrary(string dllToLoad); [DllImport("kernel32.dll")] public static extern IntPtr GetProcAddress(IntPtr hModule, string procedureName); [DllImport("kernel32.dll")] public static extern bool FreeLibrary(IntPtr hModule); } class Program { [UnmanagedFunctionPointer(CallingConvention.Cdecl)] private delegate int mcTestMethod(int d); static void Main(string[] args) { IntPtr pDll = NativeMethods.LoadLibrary(@"C:\\mcMath.dll"); //oh dear, error handling here if (pDll == IntPtr.Zero) { Console.WriteLine("Pointer DLL null"); } IntPtr pAddressOfFunctionToCall = NativeMethods.GetProcAddress(pDll, "mcTestMethod"); //oh dear, error handling here if (pAddressOfFunctionToCall == IntPtr.Zero) { Console.WriteLine("Adress null"); } mcTestMethod addResult = (mcTestMethod)Marshal.GetDelegateForFunctionPointer(pAddressOfFunctionToCall, typeof(mcTestMethod)); int theResult = addResult(10); bool result = NativeMethods.FreeLibrary(pDll); Console.WriteLine(theResult.ToString()); int f = 0; } } }
Le problème est :
1- L'appel à GetProcAddress retourne toujours 0 malgré que l'appel à LoadLibrary réussit et que le nom de la fonction à appeler "mcTestMethod" existe correctement dans la dll.
2- J'ai développé une autre dll identique à celle développée en C# mais en C++. Dans ce cas, l'appel à GetProcAddress réussit et la méthode "mcTestMethod" est exécutée correctement.
Des explications svp.
Partager