Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include "test.h"
- extern "C" __declspec(dllexport) int malloctest();
- int malloctest(){
- int check = test();
- return check;
- }
- #include <stdlib.h>
- #ifdef __cplusplus
- extern "C" {
- #endif
- int test();
- #ifdef __cplusplus
- }
- #endif
- #include "test.h"
- int test(){
- int * array = malloc(42 * sizeof(int));
- free(array);
- return 42;
- }
- gcc -c malloctest.cpp test.c
- gcc -shared -o malloctest.dll malloctest.o test.o
- class Program
- {
- [DllImport("kernel32", CharSet = CharSet.Ansi, ExactSpelling = true, SetLastError = true)]
- static extern IntPtr GetProcAddress(IntPtr hModule, string procName);
- [DllImport("kernel32", SetLastError = true)]
- static extern IntPtr LoadLibrary(string lpFileName);
- public delegate int test();
- static void Main(string[] args)
- {
- IntPtr pcygwin = LoadLibrary("cygwin1.dll");
- IntPtr pcyginit = GetProcAddress(pcygwin, "cygwin_dll_init");
- Action init = (Action)Marshal.GetDelegateForFunctionPointer(pcyginit, typeof(Action));
- init();
- IntPtr libptr = LoadLibrary("malloctest.dll");
- IntPtr funcptr = GetProcAddress(libptr, "malloctest");
- test malloctest = (test)Marshal.GetDelegateForFunctionPointer(funcptr, typeof(test));
- int check = malloctest(); //hold
- Console.WriteLine(check);
- }
- }
Add Comment
Please, Sign In to add comment