如何在使用DllImpot属性时更改托管代码中的函数名称?

时间:2021-03-02 16:01:20

I want to call an external function like this.

我想调用这样的外部函数。

[DllImport("user32.dll")]
static extern IntPtr WindowFromPoint(POINT Point);

However I want to change function name from WindowFromPoint to MyFunc. So in my managed code I would be able to do :

但是我想将函数名称从WindowFromPoint更改为MyFunc。所以在我的托管代码中,我能够做到:

MyFunc(new POINT());

Because this is not the only function and there is over 100 functions I'm looking for a easy one line solution.

因为这不是唯一的功能,并且有超过100个功能,我正在寻找一个简单的单行解决方案。

1 个解决方案

#1


4  

[DllImport("user32.dll", CharSet = CharSet.Unicode, EntryPoint = "WindowFromPoint")]
static extern IntPtr MyFunc(POINT Point);

I believe this will work.

我相信这会奏效。

Or, if, for whatever reason, you need to keep the name you can try this:

或者,如果出于某种原因,您需要保留名称,您可以尝试这样做:

[MethodImplAttribute(MethodImplOptions.AggressiveInlining)] 
public static IntPtr MyFunc(POINT Point)
{
    return WindowFromPoint(Point);
}

#1


4  

[DllImport("user32.dll", CharSet = CharSet.Unicode, EntryPoint = "WindowFromPoint")]
static extern IntPtr MyFunc(POINT Point);

I believe this will work.

我相信这会奏效。

Or, if, for whatever reason, you need to keep the name you can try this:

或者,如果出于某种原因,您需要保留名称,您可以尝试这样做:

[MethodImplAttribute(MethodImplOptions.AggressiveInlining)] 
public static IntPtr MyFunc(POINT Point)
{
    return WindowFromPoint(Point);
}