Tag: pinvoke

“对PInvoke函数的调用使堆栈失衡”

我在visual c#中创建了一个Form应用程序,它使用一个函数来生成鼠标单击,但是我收到以下错误消息: A call to PInvoke function ‘…Form1::mouse_event’ has unbalanced the stack. This is likely because the managed PInvoke signature does not match the unmanaged target signature. Check that the calling convention and parameters of the PInvoke signature match the target unmanaged signature. 我的代码: [DllImport(“user32.dll”, CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)] public static extern void […]

以编程方式检查MSI的ProductVersion

昨天我一直在搜索高低,寻找如何快速获得msi数据库的ProductVersion。 大多数情况下,我发现使用WindowsInstaller COM包装器,虽然这完成了工作,我想通过pinvoke使用msi.dll实现相同的结果。

C#PInvoke out字符串声明

在C#PInvoke中,如何传递一个字符串缓冲区,以便C DLL填充它并返回? 什么是PInvoke宣言? C函数声明是 int GetData(char* data, int buflength); 在C#中,我已将其声明为 [DllImport(DllName)] static extern Int32 GetData([MarshalAs(UnmanagedType.LPStr)]StringBuilder receiveddata, Int32 buflen); 这是对的吗? 我像这样传递StringBuilder变量 int bufferLength = 32; StringBuilder data = new StringBuilder(bufferLength); int result = GetData(data, bufferLength); 我想知道它是否正确? 谢谢

枚举大小以字节为单位

以字节为单位的枚举大小是多少? public enum MMTPCnxNckRsn { MMTPCnxNckRsnNoAnswer = -2, MMTPCnxNckRsnSendError = -1, MMTPCnxNckRsnOk = 0, MMTPCnxNckRsnInvalidMember = 1, MMTPCnxNckRsnHubNotReady = 2, MMTPCnxNckRsnUnknownMember = 3, MMTPCnxNckRsnLastCnxTooRecent = 4, MMTPCnxNckRsnInvalidVersion = 5, MMTPCnxNckRsnInvalidOptions = 6, MMTPCnxNckRsnTooManyCnx = 7 }; 我已经使用下面的代码找到它,但我认为它还不够……它是一个包含10个元素的字符串数组。我应该计算每个元素中的字符数,假设每个字符1个字节并添加所有字节的元素? 那么无符号数呢? var size = Enum.GetNames(typeof(MMTPCnxNckRsn)).Length; var arr = Enum.GetNames(typeof (MMTPCnxNckRsn)); 我不确定是否重要的​​是提到我正在将本地win32代码marshaling到c#中,并且找到用于管理内存中地址的枚举大小是很重要的。

不平衡堆栈!

我写了一个VC ++ DLL。 dll中某个方法的声明如下: extern “C” _declspec(dllexport) void startIt(int number) { capture = cvCaptureFromCAM(number); } 我在使用P / Invoke的C#代码中使用此dll。 我将声明作为: [DllImport(“Tracking.dll”, EntryPoint = “startIt”)] public extern static void startIt(int number); 我将代码中的函数调用为: startIt(0); 现在,当遇到这一行时,编译器会抛出这个错误: A call to PInvoke function ‘UsingTracking!UsingTracking.Form1::startIt’ has unbalanced the stack. This is likely because the managed PInvoke signature does not match the unmanaged […]

如何编组指向结构数组指针的指针?

我的C声明如下: int myData(uint myHandle, tchar *dataName, long *Time, uint *maxData, DATASTRUCT **data); typedef struct { byte Rel; __int64 Time; char Validated; unsigned char Data[1]; } DATASTRUCT ; 我的C#声明如下: [DllImport(“myData.dll”, EntryPoint = “myData”)] public static extern int myData(uint myHandle, [MarshalAs(UnmanagedType.LPTStr)] string dataName, out long Time, out uint maxData, ref DATASTRUCT[] data); [StructLayout(LayoutKind.Sequential, Pack = 1)] public […]

使用RegisterHotKey检测Ctrl + V但不截取它

我需要检测用户何时按下Ctrl + V (无论窗口焦点如何 – 我的应用程序可能会被最小化)但我不能停止实际的粘贴操作。 我尝试了一些事情:(我成功绑定了RegisterHotKey的键击) 我有: protected override void WndProc(ref Message m) { if (m.Msg == 0x312) hotKey(); base.WndProc(ref m); } 我尝试过以下方法: void hotKey() { SendKeys.SendWait(“^v”); //just puts ‘v’ instead of clipboard contents } 和 void hotKey() { SendKeys.SendWait(ClipBoard.GetText()); /* This works, but since Ctrl is still down, it triggers * all the […]

需要激活一个窗口

我有这种情况。 我有一个应用程序的窗口句柄。 我需要激活它。 我尝试了所有这些function,但总是没有工作。(大多数时候,它不能在第一次工作,我将不得不手动点击它来激活它。第二次尝试以后它工作正常)我之所以如此我这样做是因为我有代码写在Form.Activate事件的forms,我需要执行。 应用程序是单实例应用程序。 创建新实例时,它首先检查是否存在任何其他进程,如果找到,则将旧进程的句柄传递给这些函数,以便用户可以处理旧表单。 从不同的C应用程序调用应用程序。 [DllImport(“user32.dll”)] public static extern int ShowWindow(IntPtr hWnd,int nCmdShow); [DllImport(“user32.dll”)] public static extern int SetForegroundWindow(IntPtr hWnd); [DllImport(“user32”)] public static extern bool PostMessage(IntPtr hwnd, int msg, IntPtr wparam, IntPtr lparam);

如何将const char *从C#传递给C函数?

我尝试从我的C#-application中调用来自外部DLL的普通C函数。 此function定义为 void set_param(const char *data) 现在我使用这个函数有一些问题: 如何在C#代码中指定此“const”? public static extern void set_param(sbyte *data)似乎错过了“const”部分。 调用此函数时,如何移交普通的8位C字符串? 调用set_param(“127.0.0.1”)导致错误消息“无法从’string’转换为’sbyte ‘”*。

P / Invoke教程?

有谁知道在System.Runtime.InteropServices名称空间中有深入的好教程/电子书? 我刚刚阅读了本教程,了解如何使UAC屏蔽显示在一个按钮中,现在我真的想学习这个。 PS如何在Windows系统文件中查看是否存在在按钮中显示UAC屏蔽的方法?