Advertisement
Guest User

Untitled

a guest
May 4th, 2015
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.06 KB | None | 0 0
  1. library Project1;
  2.  
  3. uses
  4. System.SysUtils,
  5. System.Classes;
  6.  
  7. type
  8.  
  9. IStringFunctions = interface
  10. ['{240B567B-E619-48E4-8CDA-F6A722F44A71}']
  11. function GetMethodValueAsString():PAnsiChar; stdcall;
  12. end;
  13.  
  14. TStringFunctions = class(TInterfacedObject, IStringFunctions)
  15. public
  16. function GetMethodValueAsString():PAnsiChar; stdcall;
  17. end;
  18.  
  19. {$R *.res}
  20.  
  21. function TStringFunctions.GetMethodValueAsString():PAnsiChar; stdcall;
  22. begin
  23. Result := 'test';
  24. end;
  25.  
  26.  
  27. procedure GetImplementation(out instance:IStringFunctions); stdcall; export;
  28. begin
  29. instance := TStringFunctions.Create;
  30. end;
  31.  
  32. exports GetImplementation;
  33.  
  34. begin
  35. end.
  36.  
  37. using System;
  38. using System.Runtime.CompilerServices;
  39. using System.Runtime.InteropServices;
  40.  
  41. namespace ConsoleApplication1
  42. {
  43. [ComVisible(true)]
  44. [ComImport, InterfaceType(ComInterfaceType.InterfaceIsIUnknown), Guid("240B567B-E619-48E4-8CDA-F6A722F44A71")]
  45. public interface IStringFunctions
  46. {
  47. [MethodImplAttribute(MethodImplOptions.PreserveSig)]
  48. [return: MarshalAs(UnmanagedType.AnsiBStr)]
  49. string GetMethodValueAsString();
  50. }
  51.  
  52. class Program
  53. {
  54. [DllImport("kernel32.dll", EntryPoint = "LoadLibrary", CallingConvention = CallingConvention.StdCall)]
  55. static extern int LoadLibrary([MarshalAs(UnmanagedType.LPStr)] string lpLibFileName);
  56.  
  57. [DllImport("kernel32.dll", EntryPoint = "GetProcAddress", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
  58. static extern IntPtr GetProcAddress(int hModule, [MarshalAs(UnmanagedType.LPStr)] string lpProcName);
  59.  
  60. [DllImport("kernel32.dll", EntryPoint = "FreeLibrary", CallingConvention = CallingConvention.StdCall)]
  61. static extern bool FreeLibrary(int hModule);
  62.  
  63. [UnmanagedFunctionPointer(CallingConvention.StdCall, CharSet = CharSet.Ansi)]
  64. delegate void GetImplementation([MarshalAs(UnmanagedType.Interface)] out IStringFunctions instance);
  65.  
  66. static void Main(string[] args)
  67. {
  68. const string dllName = "Project1.dll";
  69. const string functionName = "GetImplementation";
  70.  
  71. int libHandle = LoadLibrary(dllName);
  72. if (libHandle == 0) throw new Exception(string.Format("Could not load library "{0}"", dllName));
  73.  
  74. var delphiFunctionAddress = GetProcAddress(libHandle, functionName);
  75. if (delphiFunctionAddress == IntPtr.Zero) throw new Exception(string.Format("Can't find function "{0}" in library "{1}"", functionName, dllName));
  76.  
  77. GetImplementation getImplementation = (GetImplementation)Marshal.GetDelegateForFunctionPointer(delphiFunctionAddress, typeof(GetImplementation));
  78.  
  79. if (getImplementation != null)
  80. {
  81. IStringFunctions instance = null;
  82. getImplementation(out instance);
  83.  
  84. if (instance != null)
  85. {
  86. //!!! don't return value !!!!
  87. String result = instance.GetMethodValueAsString();
  88. Console.WriteLine(result);
  89. }
  90. }
  91. Console.ReadLine();
  92. }
  93. }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement