欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費電子書(shū)等14項超值服

開(kāi)通VIP
c#進(jìn)程外Com服務(wù)(exe)編寫(xiě)調用

網(wǎng)上有些進(jìn)程外的一些資料,但有些簡(jiǎn)單,研究了兩天寫(xiě)了demo,可利用這種方式解決64位的程序調用32位的dll等問(wèn)題,但注意方法參數不能含有IntPtr,因為指針跨進(jìn)程是無(wú)效的,每個(gè)進(jìn)程都有自己的內存區域

 一.編寫(xiě)外部Com服務(wù)exe

    1.首先新建一個(gè)winform的應用程序,并設置com程序集可見(jiàn)


2.編寫(xiě)com類(lèi) 

    編寫(xiě)com接口,guid可利用vs的工具生成,代碼設置com接口的可視,實(shí)現接口后,編寫(xiě)com工廠(chǎng)啟用com


  

  1. internal static class ComHelperClass
  2. {
  3. public const string s_IID_ITestComVisible = "C66C0654-49AE-4f2e-8EDA-BD01C8259C20";
  4. public const string s_CLSID_TestComVisibleClass = "12D783BB-33BF-4973-B38B-2A8F0BA926E4";
  5. public static readonly Guid IID_ITestComVisible = new Guid(s_IID_ITestComVisible);
  6. public static readonly Guid CLSID_TestComVisibleClass = new Guid(s_CLSID_TestComVisibleClass);

  7. public const string s_IID_IClassFactory = "00000001-0000-0000-C000-000000000046";
  8. public static readonly Guid IID_IClassFactory = new Guid("00000001-0000-0000-C000-000000000046");
  9. public static readonly Guid IID_IUnknown = new Guid("00000000-0000-0000-C000-000000000046");

  10. [DllImport("ole32.dll")]
  11. public static extern int CoRegisterClassObject(
  12. [MarshalAs(UnmanagedType.LPStruct)] Guid rclsid,
  13. [MarshalAs(UnmanagedType.IUnknown)] object pUnk,
  14. uint dwClsContext,
  15. uint flags,
  16. out uint lpdwRegister);

  17. [DllImport("ole32.dll")]
  18. public static extern int CoRevokeClassObject(uint dwRegister);

  19. [DllImport("ole32.dll")]
  20. public static extern int CoInitializeSecurity(
  21. IntPtr securityDescriptor,
  22. Int32 cAuth,
  23. IntPtr asAuthSvc,
  24. IntPtr reserved,
  25. UInt32 AuthLevel,
  26. UInt32 ImpLevel,
  27. IntPtr pAuthList,
  28. UInt32 Capabilities,
  29. IntPtr reserved3);

  30. public const int RPC_C_AUTHN_LEVEL_PKT_PRIVACY = 6; // Encrypted DCOM communication
  31. public const int RPC_C_IMP_LEVEL_IDENTIFY = 2; // No impersonation really required
  32. public const int CLSCTX_LOCAL_SERVER = 4;
  33. public const int REGCLS_MULTIPLEUSE = 1;
  34. public const int EOAC_DISABLE_AAA = 0x1000; // Disable Activate-as-activator
  35. public const int EOAC_NO_CUSTOM_MARSHAL = 0x2000; // Disable custom marshalling
  36. public const int EOAC_SECURE_REFS = 0x2; // Enable secure DCOM references
  37. public const int CLASS_E_NOAGGREGATION = unchecked((int)0x80040110);
  38. public const int E_NOINTERFACE = unchecked((int)0x80004002);
  39. }

  40. [ComVisible(true)]
  41. [Guid(ComHelperClass.s_IID_ITestComVisible)]
  42. public interface ITestComVisible
  43. {
  44. [DispId(1)]
  45. string TestProperty { get; set; }

  46. [DispId(2)]

  47. void TestMethod();

  48. //可擴展相應的方法接口,并在TestComVisibleClass 實(shí)現

  49. }
  50. [ComVisible(true)]
  51. [Guid(ComHelperClass.s_CLSID_TestComVisibleClass)]
  52. public class TestComVisibleClass : ITestComVisible
  53. {
  54. public string TestProperty { get; set; }

  55. public void TestMethod()
  56. {
  57. MessageBox.Show("我是32");
  58. }
  59. }
  60. // 類(lèi)廠(chǎng)
  61. [
  62. ComImport,
  63. InterfaceType(ComInterfaceType.InterfaceIsIUnknown),
  64. Guid(ComHelperClass.s_IID_IClassFactory)
  65. ]
  66. internal interface IClassFactory
  67. {
  68. [PreserveSig]
  69. int CreateInstance(IntPtr pUnkOuter, ref Guid riid, out IntPtr ppvObject);
  70. [PreserveSig]
  71. int LockServer(bool fLock);
  72. }
  73. internal class ComClassFactory : IClassFactory
  74. {
  75. #region IClassFactory Members

  76. public int CreateInstance(IntPtr pUnkOuter, ref Guid riid, out IntPtr ppvObject)
  77. {
  78. ppvObject = IntPtr.Zero;
  79. if (pUnkOuter != IntPtr.Zero)
  80. Marshal.ThrowExceptionForHR(ComHelperClass.CLASS_E_NOAGGREGATION);
  81. if (riid == ComHelperClass.IID_ITestComVisible ||
  82. riid == ComHelperClass.IID_IUnknown)
  83. {
  84. ppvObject = Marshal.GetComInterfaceForObject(
  85. new TestComVisibleClass(), typeof(ITestComVisible));
  86. }
  87. else
  88. Marshal.ThrowExceptionForHR(ComHelperClass.E_NOINTERFACE);
  89. return 0; // S_OK
  90. }
  91. public int LockServer(bool fLock)
  92. {
  93. return 0; // S_OK
  94. }
  95. #endregion
  96. }


3.編寫(xiě)代碼啟動(dòng)com工廠(chǎng),調用;并編譯生成程序

  1. static void Main()
  2. {
  3. Application.EnableVisualStyles();
  4. Application.SetCompatibleTextRenderingDefault(false);
  5. RegisterDcomServer();
  6. Application.ApplicationExit += new EventHandler(Application_ApplicationExit);
  7. Application.Run(new Form1());
  8. }


  9. static void Application_ApplicationExit(object sender, EventArgs e)
  10. {
  11. RevokeDcomServer();
  12. }


  13. private static void RegisterDcomServer()
  14. {
  15. // 做一些安全檢查,確保只有一些有權限的人才能調用你的C# Dcom組件
  16. // 如果你對安全性不關(guān)心的話(huà),可以刪除下面的語(yǔ)句
  17. //int hr = ComHelperClass.CoInitializeSecurity(
  18. // IntPtr.Zero, // 這里要輸入你的安全描述符
  19. // -1,
  20. // IntPtr.Zero,
  21. // IntPtr.Zero,
  22. // ComHelperClass.RPC_C_AUTHN_LEVEL_PKT_PRIVACY,
  23. // ComHelperClass.RPC_C_IMP_LEVEL_IDENTIFY,
  24. // IntPtr.Zero,
  25. // ComHelperClass.EOAC_DISABLE_AAA | ComHelperClass.EOAC_SECURE_REFS | ComHelperClass.EOAC_NO_CUSTOM_MARSHAL,
  26. // IntPtr.Zero);
  27. //if (hr != 0)
  28. // Marshal.ThrowExceptionForHR(hr);


  29. int hr = ComHelperClass.CoRegisterClassObject(
  30. ComHelperClass.CLSID_TestComVisibleClass,
  31. new ComClassFactory(),
  32. ComHelperClass.CLSCTX_LOCAL_SERVER,
  33. ComHelperClass.REGCLS_MULTIPLEUSE,
  34. out m_ComCookie);
  35. if (hr != 0)
  36. Marshal.ThrowExceptionForHR(hr);
  37. }


  38. private static void RevokeDcomServer()
  39. {
  40. if (m_ComCookie != 0)
  41. ComHelperClass.CoRevokeClassObject(m_ComCookie);

  42. }

     4.在本機注冊com服務(wù)程序(管理身份運行 regasm)生成tlb文件,并修改添加注冊表為本地服務(wù)(LocalServer32),刪除自動(dòng)生成的服務(wù)(inprocServer32)



查看系統注冊表(建議使用RegWorkshop查看,檢索guid )



vs使用的話(huà)到此就可以了,但如果c++調用的話(huà)還要在注冊表里聲明下tlb的信息

tlb信息可以用oleview進(jìn)行查看,并在注冊表添加信息



二、外部對com服務(wù)進(jìn)行調用

    新建一個(gè)winform程序 ,編寫(xiě)調用代碼,即可

        

  1. System.Type t = Type.GetTypeFromProgID("TestComServer.TestComVisibleClass");
  2. dynamic o = Activator.CreateInstance(t);

  3. o.TestMethod();

 至此我們的進(jìn)程外com服務(wù)的編寫(xiě)和測試程序全部完成

完成的程序Demo 

注意下載Demo后,要現在本地進(jìn)行com注冊和相應注冊表修改,如果感覺(jué)注冊表操作麻煩,可以自己寫(xiě)個(gè)腳本

參考資料:

http://blog.csdn.net/zxdu721/article/details/7785277

https://www.cnblogs.com/killmyday/articles/1395432.html

https://www.codeproject.com/KB/COM/simplecomserver.aspx?display=Print

本站僅提供存儲服務(wù),所有內容均由用戶(hù)發(fā)布,如發(fā)現有害或侵權內容,請點(diǎn)擊舉報。
打開(kāi)APP,閱讀全文并永久保存 查看更多類(lèi)似文章
猜你喜歡
類(lèi)似文章
C#截取 當前桌面的活動(dòng)窗體圖像
常用Win32API封裝Win32apifor.net
C#實(shí)現重啟、關(guān)機、開(kāi)關(guān)顯示器的方法
C#訪(fǎng)問(wèn)遠程主機資源的方法
共享內存操作類(lèi)(C#源碼)
【zt】獲得QQ聊天輸入框中的內容
更多類(lèi)似文章 >>
生活服務(wù)
分享 收藏 導長(cháng)圖 關(guān)注 下載文章
綁定賬號成功
后續可登錄賬號暢享VIP特權!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服

欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久