Guest User

Untitled

a guest
Jul 16th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.33 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include "test.h"
  3. extern "C" __declspec(dllexport) int malloctest();
  4.  
  5. int malloctest(){
  6. int check = test();
  7. return check;
  8. }
  9.  
  10. #include <stdlib.h>
  11. #ifdef __cplusplus
  12. extern "C" {
  13. #endif
  14. int test();
  15. #ifdef __cplusplus
  16. }
  17. #endif
  18.  
  19. #include "test.h"
  20. int test(){
  21. int * array = malloc(42 * sizeof(int));
  22. free(array);
  23. return 42;
  24. }
  25.  
  26. gcc -c malloctest.cpp test.c
  27. gcc -shared -o malloctest.dll malloctest.o test.o
  28.  
  29. class Program
  30. {
  31.  
  32. [DllImport("kernel32", CharSet = CharSet.Ansi, ExactSpelling = true, SetLastError = true)]
  33. static extern IntPtr GetProcAddress(IntPtr hModule, string procName);
  34.  
  35. [DllImport("kernel32", SetLastError = true)]
  36. static extern IntPtr LoadLibrary(string lpFileName);
  37.  
  38.  
  39. public delegate int test();
  40. static void Main(string[] args)
  41. {
  42. IntPtr pcygwin = LoadLibrary("cygwin1.dll");
  43. IntPtr pcyginit = GetProcAddress(pcygwin, "cygwin_dll_init");
  44. Action init = (Action)Marshal.GetDelegateForFunctionPointer(pcyginit, typeof(Action));
  45. init();
  46.  
  47. IntPtr libptr = LoadLibrary("malloctest.dll");
  48. IntPtr funcptr = GetProcAddress(libptr, "malloctest");
  49. test malloctest = (test)Marshal.GetDelegateForFunctionPointer(funcptr, typeof(test));
  50. int check = malloctest(); //hold
  51. Console.WriteLine(check);
  52. }
  53. }
Add Comment
Please, Sign In to add comment