Tag: pinvoke

以编程方式刷新系统托盘图标

我有一个有系统托盘图标的应用程序。 在卸载时,如果它正在运行,我将终止该进程。 因此,由于没有优雅地停止应用程序,图标仍保留在系统托盘中,只有当我们将鼠标hover在系统托盘上时才会删除。 我编写了一个代码,可以沿托盘运行光标并将光标放回原始位置。 这就是我所做的: [DllImport(“user32.dll”)] static extern IntPtr FindWindow(string className, string windowName); [DllImport(“user32.dll”)] static extern IntPtr FindWindowEx(IntPtr parent, IntPtr child, string className, string windowName); [DllImport(“user32.dll”)] static extern bool GetWindowRect(HandleRef handle, out RECT rct); [StructLayout(LayoutKind.Sequential)] struct RECT { public int Left; public int Top; public int Right; public int Bottom; } void RefreshTray() { IntPtr […]

从本机C ++反转PInvoke

我目前正在尝试从非托管C ++应用程序中调用C#DLL中的函数。 在网上搜索了几个小时后,我发现我有几个选择。 我可以使用COM, DllExport ,或使用反向PInvoke与DllExport 。 最后听起来对我最有吸引力,所以在搜索之后我就到了这里 。 它声明该文章展示了如何使用反向PInvoke,但看起来C#代码必须先导入C ++ Dll才能使用它。 我需要能够使用C ++来调用我的C#Dll函数,而无需先运行C#应用程序。 也许反向PInvoke不是这样做的方式,但是当谈到低级别的东西时我很缺乏经验,所以关于如何做到这一点的任何指针或技巧都会很棒。 链接中的代码是 C# using System.Runtime.InteropServices; public class foo { public delegate void callback(string str); public static void callee(string str) { System.Console.WriteLine(“Managed: ” +str); } public static int Main() { caller(“Hello World!”, 10, new callback(foo.callee)); return 0; } [DllImport(“nat.dll”,CallingConvention=CallingConvention.StdCall)] public static extern […]

从C ++ DLL中触发C#中的事件

我有一个与Cisco服务器(UCCX)通信的非托管C ++ DLL。 它通过TCP / IP向此服务器发送消息和从该服务器接收消息。 现在它收到一些类型的消息,其中包含一些需要发送到C#GUI的参数,这些参数将在屏幕上显示这些参数。 请告诉我一个有效的方法来从这个DLL中触发C#中的事件。

如何处理受约束执行区域中的分配?

约束执行区域是C#/ .Net的一个特性,它允许开发人员尝试从关键的代码区域中提升“三大”exception–OutOfMemory,StackOverflow和ThreadAbort。 CER通过推迟ThreadAborts,准备调用图中​​的所有方法来实现这一点(因此不会发生JIT,这可能导致分配),并确保有足够的堆栈空间来适应随后的调用堆栈。 典型的不间断区域可能如下所示: public static void GetNativeFlag() { IntPtr nativeResource = new IntPtr(); int flag; // Remember, only the finally block is constrained; try is normal. RuntimeHelpers.PrepareConstrainedRegions(); try { } finally { NativeMethods.GetPackageFlags( ref nativeResource ); if ( nativeResource != IntPtr.Zero ) { flag = Marshal.ReadInt32( nativeResource ); NativeMethods.FreeBuffer( nativeResource ); } } } […]

外部应用程序窗口移动时移动窗口

我有一个总是在顶部的应用程序(基本上是一个状态显示),我想跟随另一个程序,并始终坐在最小化按钮的左侧。 我可以使用以下代码获取表示“目标”过程的Rect ,然后我可以将其与偏移量配对以生成叠加层的初始位置。 获取HWnd IntPtr: private IntPtr HWnd = Process.GetProcessesByName(“targetapplication”)[0].MainWindowHandle; 从user32.dll声明函数: [DllImport(“user32.dll”, SetLastError = true)] [return: MarshalAs(UnmanagedType.Bool)] private static extern bool GetWindowRect(IntPtr hWnd, ref RECT lpRect); 和适当的struct : [StructLayout(LayoutKind.Sequential)] private struct RECT { public int Left; public int Top; public int Right; public int Bottom; } 然后按需调用它。 但是,我想避免不断轮询这个值,所以我想挂钩到目标应用程序并在目标窗口移动时响应。 查看user32.dll文档,我能看到的唯一方法是使用SetWindowsHookEx() 。 但是,我并不完全确定如何从这里拦截一个事件。 我相信目标应用程序是基于WinForms构建的,但我无法确定。 因此,让我响应目标的Move事件或直接响应某些Windows消息的解决方案都很有用。 关于如何进行的任何想法?

从C#托管代码调用win32 CreateProfile()

快速问题(希望如此),如何从C#(托管代码)正确调用win32函数CreateProfile()? 我试图自己设计一个解决方案但没有用。 CreateProfile()的语法是: HRESULT WINAPI CreateProfile( __in LPCWSTR pszUserSid, __in LPCWSTR pszUserName, __out LPWSTR pszProfilePath, __in DWORD cchProfilePath ); 支持文档可以在MSDN库中找到。 我到目前为止的代码发布在下面。 DLL导入: [DllImport(“userenv.dll”, CharSet = CharSet.Auto, SetLastError = true)] private static extern int CreateProfile( [MarshalAs(UnmanagedType.LPWStr)] string pszUserSid, [MarshalAs(UnmanagedType.LPWStr)] string pszUserName, [Out][MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszProfilePath, uint cchProfilePath); 调用函数: /* Assume that a user has been created using: […]

如何解决私有字节(本机内存)泄漏?

我正在开发一个似乎有漏洞的C#应用​​程序。 我用过内存分析器,发现我的 私有字节不断增加,但所有堆中的 字节数不会增加,这意味着它可能是本机内存泄漏 现在我卡住了,如何在本机代码中发现内存泄漏?

委托实例和方法指针之间有什么区别?

我认为委托实例可以与函数实例互换。 请使用以下代码: delegate int AddDelegate(int a, int b); AddDelegate DelegateInstance; public void DoStuff() { //I can call this without a delegate “instance”: MethodThatTakesAdd(Add); //I can also call it WITH a delegate “instance” DelegateInstance = Add; MethodThatTakesAdd(DelegateInstance); } public int Add(int a, int b) { return a + b; } public void MethodThatTakesAdd(AddDelegate addFunction) { Console.WriteLine(addFunction(1, […]

来自user32.dll的FindWindowEx使用dllimport返回零句柄和错误代码127

我需要以编程方式处理另一个Windows应用程序,搜索谷歌我找到了一个使用DLLImport属性处理Windows计算器的示例,并在C#中将user32.dll函数导入托管应用程序。 应用程序正在运行,我正在获取主窗口的句柄,即计算器本身,但后续代码不起作用。 FindWindowEx方法不返回计算器子项的句柄,如按钮和文本框。 我尝试在DLLImport上使用SetLastError = True,发现我收到错误代码127,即“找不到过程”。 这是我从中获取示例应用程序的链接: http://www.codeproject.com/script/Articles/ArticleVersion.aspx?aid=14519&av=34503 如果有人知道如何解决它,请帮忙。 更新:DLLImport是: [DllImport(“user32.dll”, SetLastError = true)] public static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string className, string windowTitle); 不起作用的守则是: hwnd=FindWindow(null,”Calculator”); // This is working, I am getting handle of Calculator // The following is not working, I am getting hwndChild=0 and err = 127 hwndChild = FindWindowEx((IntPtr)hwnd,IntPtr.Zero,”Button”,”1″); […]

如何使用pinvoke使用cdecl回调

我有一个具有cdecl回调的ac库。 我如何从c#中使用这些。 一切似乎都说他们必须是stdcall回调 要清楚: delegate int del(); [dllimport(“mylib.dll”,CallingConvention=CallingConvention.Cdecl)] public static extern int funcwithcallback(del foo); 其中del必须被称为cdecl-wise