标签: pinvoke

如何在C#中确定当前关注的进程名称和版本

例如,如果我正在使用Visual Studio 2008,我想要值devenv和2008或9.

版本号非常重要......

c# pinvoke process user32

1
推荐指数
1
解决办法
2596
查看次数

如何在c#中使用C dll头文件库

我是C#的新手,我正在尝试为我的项目使用帮助包.包是用c编写的,有1)/ bin /几个.dll文件2)/ include /有一个头文件3)/ lib/msvc/.lib文件我的问题是如何在我的C#WPF项目中使用这些文件?我知道C#中没有"#include",并且无法通过添加到项目的引用来导入.dll.那我怎么能在C#中做到这一点?

谢谢

.net c# wpf pinvoke interop

1
推荐指数
1
解决办法
5192
查看次数

在窗口标题中更改光标

我有一个WinForm,现在我需要在窗口标题部分更改光标.我有一些代码工作,它有两个问题:

  1. 它还会在边缘处更改光标(应显示正常的调整大小光标).我发现了,我需要这样的事情WM_NCHITTESTHTTOP,但我怎么结合呢?
  2. 移动鼠标时有一些闪烁.

我也尝试将代码放在下面base.WndProc(ref m);.

这是我已有的代码:

if ((m.Msg == Win32.WM.NCMOUSEMOVE
    || m.Msg == Win32.WM.NCLBUTTONDOWN || m.Msg == Win32.WM.NCLBUTTONUP
    || m.Msg == Win32.WM.NCRBUTTONDOWN || m.Msg == Win32.WM.NCRBUTTONUP)
)
{
    if (m.WParam.ToInt32() != Win32.HT.TOP && m.WParam.ToInt32() != Win32.HT.RIGHT && m.WParam.ToInt32() != Win32.HT.BOTTOM && m.WParam.ToInt32() != Win32.HT.LEFT)
    {
        Cursor = Cursors.Hand;
    }
}
Run Code Online (Sandbox Code Playgroud)

编辑:
我没有在Spy ++中正确记录消息.找到窗口边缘的解决方案(请参阅更新的代码).

Thnx,J

c# pinvoke wndproc winforms

1
推荐指数
1
解决办法
882
查看次数

访问Delphi DLL抛出ocasional异常

当我调用Dll方法时,它有时会引发异常,有时则不会.

我这样称呼它:

public class DllTest
{

    [DllImport(@"MyDll.dll")]
    public extern static string MyMethod(string someStringParam);
}


class Program
{       

    static void Main(string[] args)
    {
        DllTest.MyMethod("SomeString");
    }
}
Run Code Online (Sandbox Code Playgroud)

有时得到的例外是:

AccessViolationException

Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

AccessViolationException

Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

AccessViolationException

Attempted to read or write protected memory. This is often an indication that other memory …

c# delphi pinvoke interop delphi-7

1
推荐指数
1
解决办法
2210
查看次数

拦截ESC而不从缓冲区中删除其他按键

我有一个控制台应用程序,提示用户多个输入.我希望用户能够在任何提示取消操作后按下escape.

就像是:

if (Console.ReadKey().Key != ConsoleKey.Escape) {
  string input = Console.ReadLine();
  ...
}
Run Code Online (Sandbox Code Playgroud)

但问题是,如果按下除了escape之外的键,它将不是输入的一部分(从中返回ReadLine).

有没有办法"偷看"下一个键,否则这样做?

.net c# console pinvoke

1
推荐指数
1
解决办法
1662
查看次数

是否有可能将UInt16制作成枚举?

我有一个字节数组需要编组到以下结构中:

[StructLayout(LayoutKind.Sequential, Pack = 4)]
internal struct ACSEventHeader_t
{
    public UInt32 acsHandle;
    public EventClasses eventClass;
    public UInt16 eventType;
};
Run Code Online (Sandbox Code Playgroud)

EventClasses枚举定义为:

internal enum EventClasses
{
    Request = 0,
    Unsolicited = 1,
    ConnectionConfirmation = 2,
    CommandConfirmation = 5
}
Run Code Online (Sandbox Code Playgroud)

我用它做的代码看起来像这样(eventBuf.Data的类型为byte []):

ACSEventHeader_t h = new ACSEventHeader_t();
IntPtr pt1 = Marshal.AllocHGlobal(eventBuf.Data.Length);
Marshal.Copy(eventBuf.Data, 0, pt1, eventBuf.Data.Length);
h = (ACSEventHeader_t)Marshal.PtrToStructure(pt1, typeof(ACSEventHeader_t));
Marshal.FreeHGlobal(pt1);
Run Code Online (Sandbox Code Playgroud)

在代码中执行此操作将无异常地工作,但ACSEventHeader_t结构的eventClass属性具有错误的值.将类型更改为UInt16会获得正确的值,但之后我没有枚举.

我试图将[MarshalAs(UnmanagedType.U2)]添加到eventClass属性,但是会产生以下异常:

Cannot marshal field 'eventClass' of type 'ACSEventHeader_t': 
enter code here`Invalid managed/unmanaged type combination (Int32/UInt32 must be
paired with I4, U4, or …
Run Code Online (Sandbox Code Playgroud)

c# pinvoke

1
推荐指数
1
解决办法
2115
查看次数

如何从编组指针读取uint?

我有一个本机方法,需要一个指针来写出一个dword(uint).

现在我需要从(Int)指针获取实际的uint值,但Marshal类只有读取(签名)整数的方便方法.

如何从指针获取uint值?

我搜索了问题(和谷歌),但找不到我需要的东西.

示例(不工作)代码:

IntPtr pdwSetting = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(uint)));

        try
        {
            // I'm trying to read the screen contrast here
            NativeMethods.JidaVgaGetContrast(_handleJida, pdwSetting);
            // this is not what I want, but close
            var contrast = Marshal.ReadInt32(pdwSetting);
        }
        finally
        {
            Marshal.FreeHGlobal(pdwSetting);
        }
Run Code Online (Sandbox Code Playgroud)

来自本机函数的返回值是0到255之间的双字,其中255是完全对比.

c# pinvoke marshalling

1
推荐指数
2
解决办法
5136
查看次数

如何将窗口所有者设置为非托管窗口

我想将所有者表单设置为无人窗口的表单.我有非托管窗口的句柄.如何将此非托管窗口设置为托管表单的所有者窗口?

IntPtr hWnd = GetUnmanagedWindow();//assume the handle is returned correctly
Form form = new Form();
form.Show(ConvertToManaged(hWnd));//Need an implementation for ConvertOrSomething()
Run Code Online (Sandbox Code Playgroud)

c# pinvoke winforms

1
推荐指数
1
解决办法
2743
查看次数

我不能用C#禁用Windows Key

我使用的是Windows 8.我尝试使用c#禁用Windows Key,例如http://tamas.io/c-disable-ctrl-alt-del-alt-tab-alt-f4-start-menu-and-so-on/.它没有错误,但Windows键未被禁用.如何用C#禁用Windows Key?

这是我的代码.

namespace TestDisable
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        [DllImport("user32", EntryPoint = "SetWindowsHookExA", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
        public static extern int SetWindowsHookEx(int idHook, LowLevelKeyboardProcDelegate lpfn, int hMod, int dwThreadId);
        [DllImport("user32", EntryPoint = "UnhookWindowsHookEx", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
        public static extern int UnhookWindowsHookEx(int hHook);
        public delegate int LowLevelKeyboardProcDelegate(int nCode, int wParam, ref KBDLLHOOKSTRUCT lParam);
        [DllImport("user32", EntryPoint = "CallNextHookEx", …
Run Code Online (Sandbox Code Playgroud)

c# pinvoke winapi keyboard-hook

1
推荐指数
1
解决办法
2314
查看次数

编组指向数组P / Invoke的指针

我包装共享对象库(FFTW).NET使用的核心P/InvokeFFTW需要分配可能在特定边界上对齐的内存,因此我需要使用其内存分配例程。理想情况下,我想避免在托管数组中创建单独的内存块,并避免每次使用时复制数据。理想情况下,创建数组以指向已分配的内存。这是可能的,还是我应该放弃并承担副本的性能问题?

c# arrays pinvoke marshalling .net-core

1
推荐指数
1
解决办法
155
查看次数