【问题标题】:Calling C++ dll function from c#从 C# 调用 C++ dll 函数
【发布时间】:2012-09-17 04:22:12
【问题描述】:

我有一个解决方案,它有一个带有函数的 c++ dll 项目(MsgHook.cpp):-

BOOL f_closeSEB()
{
    logg(fp, "\n\n");
    //TerminateProcess(hPiProcess->hProcess,0);
    SendMessage(hWndCaller,WM_DESTROY,NULL,NULL);
    logg(fp, "   SEB exit sequence, destroy window\n");
    //logg(fp, "Leave LLKeyboardHook() and return -1\n\n");
    return -1;
}

我正在尝试通过以下方式从我的 c# Web 服务项目中调用此函数:-

using System.Runtime.InteropServices;

    namespace closeSEB
    {
        partial class closeSEBService
        {
            /// <summary> 
            /// Required designer variable.
            /// </summary>
            private System.ComponentModel.IContainer components = null;

            public enum commands
            {
                CloseIt=255
            }

            /// <summary>
            /// Clean up any resources being used.
            /// </summary>
            /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
            protected override void Dispose(bool disposing)
            {
                if (disposing && (components != null))
                {
                    components.Dispose();
                }
                base.Dispose(disposing);
            }

            [DllImport("MsgHook.dll", SetLastError = true)]
            public static extern void runRTS(string serviceName);

             protected override void OnCustomCommand(int command)
            {
              base.OnCustomCommand(command);
              if (command == (int)commands.CloseIt)
              {
                //Code to call msghook closeSEB function
                  runRTS("closeSEBService");
                  f_closeSEB(); 
              }
            }
            #region Component Designer generated code

            /// <summary> 
            /// Required method for Designer support - do not modify 
            /// the contents of this method with the code editor.
            /// </summary>
            private void InitializeComponent()
            {
                // 
                // closeSEBService
                // 
                this.ServiceName = "Service1";

            }

            #endregion

        }
    }

但是在编译器中,我收到一个错误,即名称 f_closeSEB 在当前上下文中没有退出。这不是通过C#调用DLL文件中定义的函数的正确方式吗?

【问题讨论】:

  • 我在这方面的经验不是很丰富,但你不应该是[DllImport]ing f_closeSeb吗?
  • 我以为我们导入了 dll 文件。可以直接导入dll文件中定义的函数吗?
  • 我记得在 C# 中摆弄了一些本机 Win32 的东西,你必须为你使用的每个函数都这样做。
  • 好的。当我将其更改为“[DllImport("MsgHook.dll", SetLastError = true)] public static extern void f_closeSEB();”时,错误消失了。我会尝试测试它是否也可以在运行时工作。谢谢。

标签: c# c++ dll


【解决方案1】:

你必须确保两件事:

  1. 如以下示例所示正确编写 DLLImport

    [DllImport("MsgHook.dll", SetLastError = true)] public static extern bool f_closeSEB();

  2. 确保根据 C# 程序的需要为 x86 或 x64 编译 DLL“MsgHook.dll”。 .NET 支持任何 cpu,但 C++ 不支持,因此请确保所需的类型。

有关更多信息,请参阅 Calling Win32 DLLs in C# with P/Invoke 上的 MSDN 文章。

【讨论】:

    【解决方案2】:

    尝试声明

    extern "C" BOOL f_closeSEB()
    // function code goes here
    

    在 MsgHook.cpp 文件中。这样f_closeSEB 就可以解构到 .dll 文件中了。

    您还可以使用某些工具(如 CFF Explorer)检查生成的文件,以查看 .dll 的导出部分是否包含 f_closeSEB

    MsgHook.cpp 中的代码看起来像 C 代码,因此您可以将“.cpp”文件重命名为“.c”并将其编译为 C 源代码。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-06-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-06
      相关资源
      最近更新 更多