Calling C++ dll function from c#

2019-08-02 07:07发布

问题:

I have a solution that has a c++ dll project(MsgHook.cpp) with a function:-

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;
}

I am trying to call this function from my c# web services project in the following way:-

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

        }
    }

But in the compiler, I am getting an error that the name f_closeSEB does not exit in current context. Is this not the correct way of calling the function defined in DLL file via C#?

回答1:

You have to make sure of two things:

  1. Write the DLLImport correctly like in the examples shown below

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

  2. Make sure you compile the DLL "MsgHook.dll" for x86 or x64 according to the C# program needs. .NET supports any cpu but C++ does not, so make sure of the required type.

for more information, please consider the MSDN article on Calling Win32 DLLs in C# with P/Invoke.



回答2:

Try declaring

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

in MsgHook.cpp file. This way the f_closeSEB goes unmangled into the .dll file.

You may also check the resulting file using some tool (like CFF Explorer) to see if the export section of the .dll contains the f_closeSEB.

The code in MsgHook.cpp seems like a C code, so you might just rename the '.cpp' file to '.c' and compile it as C source.



标签: c# c++ dll