本文介绍了从c#调用C ++ dll函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我正在尝试从我的c#web services项目中调用此函数方式: -

 使用System.Runtime.InteropServices; 

命名空间closeSEB
{
partial class closeSEBService
{
///< summary>
///必需的设计器变量。
///< / summary>
private System.ComponentModel.IContainer components = null;

public枚举命令
{
CloseIt = 255
}

///< summary>
///清理正在使用的资源。
///< / summary>
///< param name =disposal>如果管理资源应该处理,则为true;否则为false。< / param>
protected override void Dispose(bool disposal)
{
if(disposal&&(components!= null))
{
components.Dispose();
}
base.Dispose(处置);
}

[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)
{
//代码调用msghook closeSEB函数
runRTS(closeSEBService);
f_closeSEB();
}
}
#region组件设计器生成代码

///< summary>
/// Designer支持的必需方法 - 不要使用代码编辑器修改
///该方法的内容。
///< / summary>
private void InitializeComponent()
{
//
// closeSEBService
//
this.ServiceName =Service1;

}

#endregion

}
}

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

解决方案

你必须确保两件事:


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



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


  2. 确保编译根据C#程序需要的DLLMsgHook.dll用于x86或x64。 .NET支持任何cpu ,但是C ++没有,所以请确保所需的类型。


了解更多信息,请考虑关于。


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#?

解决方案

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.

这篇关于从c#调用C ++ dll函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 09:42