Guest User

Untitled

a guest
Jun 23rd, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.27 KB | None | 0 0
  1. //function i want to call
  2. public static void GuiDelegate(string message)
  3. {
  4. WriteToWPFGui(message);
  5. }
  6.  
  7. //notice i need to marshall my string from unmanaged <-> managed, my pinvoke sig
  8. public delegate void CppCallback([MarshalAs(UnmanagedType.LPWStr)] string message);
  9.  
  10. public static void GuiWriter(CppCallback c)
  11. {
  12. GuiWriter(c);
  13. }
  14.  
  15. //we need to access C++
  16. [DllImport("C:\projectName.dll", EntryPoint="CSharp_GuiWriter")] via a dll
  17. public static extern void GuiWriter(CppCallback jarg1);
  18.  
  19. //CppCallback is defined above
  20. public static CppCallback gui_functor;
  21.  
  22. delegate void StringDelegate(string message);
  23.  
  24. //delegate assignment
  25. StringDelegate gui_callback = GuiDelegate;
  26. gui_functor = new CppCallback(gui_callback);
  27.  
  28. //this points to pinvoke sig -> pinvoke will step into
  29. projName.GuiWriter(gui_functor);
  30.  
  31. #pragma once
  32. #include <windows.h>
  33. #include <tchar.h>
  34.  
  35. namespace lib2 {
  36.  
  37. typedef void (__stdcall *Callback)(PCWSTR);
  38. static Callback gui;
  39.  
  40. public class Class1
  41. {
  42. void makeCSHarpPrintThisText()
  43. {
  44. (*gui)(_T("make C# print this text"));
  45. }
  46. };
  47.  
  48.  
  49. __declspec(dllexport) void __stdcall CSharp_GuiWriter(void * jarg1)
  50. {
  51. Callback arg1 = (Callback) 0 ;
  52. arg1 = (Callback)jarg1;
  53. gui = arg1;
  54. }
  55. }
Add Comment
Please, Sign In to add comment