Tag: pinvoke

来自C ++的C#USB驱动程序:SetupDiGetDeviceInterfaceDetail

我试图从C#调用SetupDiGetDeviceInterfaceDetail时遇到问题。 它始终返回1784错误代码(“提供的用户缓冲区对请求的操作无效”)。 这是我的C#代码: Guid GUID_DEVINTERFACE_DFU = new Guid(0x3fe809ab, 0xfb91, 0x4cb5, 0xa6, 0x43, 0x69, 0x67, 0x0d, 0x52,0x36,0x6e); Guid classGuid = GUID_DEVINTERFACE_DFU; IntPtr hDevInfo = Win32.SetupDiGetClassDevs(ref classGuid, IntPtr.Zero, IntPtr.Zero, Win32.DIGCF_DEVICEINTERFACE | Win32.DIGCF_PRESENT); if (hDevInfo.ToInt32() == Win32.INVALID_HANDLE_VALUE) { Console.WriteLine(“read hardware information error”); } else { SP_DEVINFO_DATA devInfoData = new SP_DEVINFO_DATA(); devInfoData.cbSize = (uint)Marshal.SizeOf(typeof(SP_DEVINFO_DATA)); devInfoData.classGuid = Guid.Empty; devInfoData.devInst = […]

在P / Invoke调用上固定char

我有char缓冲区的对象池,并在P / Invoke调用上传递此缓冲区。 我是否需要在通话前固定缓冲区? 第一种方法: [DllImport(“Name”, CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)] private static extern void SomeMeth(char[] text, int size); public static string CallSomeMeth() { char[] buffer = CharBufferPool.Allocate(); SomeMeth(buffer, 4095); string result = new string(buffer, 0, Array.IndexOf(buffer, ‘\0’)); CharBufferPool.Free(buffer); return result; } 第二种方法: [DllImport(“Name”, CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)] private static unsafe extern […]

编组C Struct,其中包含结构数组

我在这个论坛中提到了类似的问题,但没有得到我的问题的解决方案。 我一直在努力解决编组问题。 我有一个包含另一个结构数组的结构,该平台是Win CE 。 我使用的是Visual Studio 2008和.NET CF 3.5。 代码: C结构: struct dot11Rate { unsigned int rate; unsigned char mode; }; typedef struct my_supported_rates { unsigned short n_rates; struct dot11Rate srates[36]; unsigned char isSet; unsigned char no_of_HTStreams; }MY_SUPPORTED_DATA_RATES; 结构MY_SUPPORTED_DATA_RATES的大小在C中是296个字节 这是我尝试将其转换为C#结构: C#转换: [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] public struct dot11Rate { public uint rate; public byte […]

如何以另一个用户la / netonly启动Internet Explorer进程?

我正在尝试从我们的WPF应用程序启动Internet Explorer作为另一个用户,这样当我们的用户访问(内部)网站时,他们通过集成Windows身份validation进行静默身份validation。 我们不希望将iexplore.exe作为其他用户启动,因为在计算机上首次启动进程时会出现奇怪的部署/环境问题,并且它首次尝试设置IE7 / 8。 但是,如果你有一个如何在每台机器上静音每个IE安装程序的解决方案,我很乐意听到它。 回到我想要的问题。 我可以使用runas从命令提示符中获取我想要的IE模拟行为*(感谢https://serverfault.com/questions/70376/runas-domain-account-still-asks-for-password ): c:\> runas /noprofile /netonly /user:MyDomain\MyUser iexplore.exe *注意:由于很多原因,我不能在我们的WPF应用程序中使用runas ,但最终结果是我想要的。 无论如何,我想要运行runas /noprofile /netonly iexplore.exe的C#等效代码。 我在CreateProcessWithLogonW P / Invoke中途。 这就是我所拥有的: uint LOGON_NETCREDENTIALS_ONLY = 2; var lpStartupInfo = new CreateProcessWithLogonW_PInvoke.STARTUPINFO(); CreateProcessWithLogonW_PInvoke.PROCESS_INFORMATION processInformation; CreateProcessWithLogonW_PInvoke.CreateProcessWithLogonW( userName, domain, pw, LOGON_NETCREDENTIALS_ONLY, null, commandLine, 0, null, null, ref lpStartupInfo, out processInformation); 这成功启动了Internet Explorer,但似乎根本没有模仿用户。 我能够通过runas命令模拟用户,所以我98%确定validation失败不是IE /区域/密码/ […]

调试winform崩溃 – C#

System.AccessViolationException was unhandled Message=Attempted to read or write protected memory. This is often an indication that other memory is corrupt. Source=System.Windows.Forms StackTrace: at System.Windows.Forms.UnsafeNativeMethods.PeekMessage(MSG& msg, HandleRef hwnd, Int32 msgMin, Int32 msgMax, Int32 remove) at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData) at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context) at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context) at System.Windows.Forms.Application.Run(Form mainForm) at ABC.Program.Main() […]

如何使用C#中的STARTUPINFOEX调用CreateProcess()并重新生成子项

我需要创建一个新进程,但是它是另一个进程的“子”而不是当前进程,例如重新父进程。 以下几乎让我有.NET:如何使用C#和.NET中的 STARTUPINFOEX调用CreateProcessAsUser() :如何PInvoke UpdateProcThreadAttribute和http://winprogger.com/launching-a-non-child-process/ using System; using System.Diagnostics; using System.IO; using System.Runtime.InteropServices; public class ProcessCreator { [DllImport(“kernel32.dll”)] [return: MarshalAs(UnmanagedType.Bool)] static extern bool CreateProcess( string lpApplicationName, string lpCommandLine, ref SECURITY_ATTRIBUTES lpProcessAttributes, ref SECURITY_ATTRIBUTES lpThreadAttributes, bool bInheritHandles, uint dwCreationFlags, IntPtr lpEnvironment, string lpCurrentDirectory, [In] ref STARTUPINFOEX lpStartupInfo, out PROCESS_INFORMATION lpProcessInformation); [DllImport(“kernel32.dll”, SetLastError = true)] [return: MarshalAs(UnmanagedType.Bool)] […]

将结构数组从c#传递给delphi

我在VS2010中使用Robert Giesecke Unmanaged Exports 1.2.6 ,我的目标是将一系列结构从c#(.NET 3.5)传递给delphi(D7) 。 我不得不承认,我对delphi并不熟悉。 我已经阅读过这篇文章 ,但建议的答案对我不起作用:在delphi中调用func时,CPU调试窗口打开,如果我继续应用程序退出,无exception且没有所需的结果。 这是我试过的代码: C#平台x86 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Runtime.InteropServices; using RGiesecke.DllExport; namespace ArrayTest { public class Class1 { public struct Sample { [MarshalAs(UnmanagedType.BStr)] public string Name; } [DllExport] public static int func( [Out, MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 1)] Sample[] samples, ref int […]

强制在ListView中显示滚动条?

背景知识 :我们大多数人都知道SysListView32公共控件和.NET Framework提供的等效包装器ListView类。 它内部的一点深度表明,它为滚动内容提供的滚动条不是控件本身,而是由SysListView32控件管理。 目标 :总是绘制滚动条,即使它没有要显示的ListViewItems ,或者只有很少的滚动条,无论如何都不需要滚动条; 有点像模仿RichTextBox类,其ScrollBars属性设置为ForcedBoth 。 或者有点像这个ListBox : 问题 : 对于ListView滚动条, .NET完全没有糖。 Win32文档未说明何时显示/隐藏和/或启用/禁用滚动条。 我的解决方法 : override派生类中的WndProc并按照步骤2和3处理其WM_HSCROLL和WM_VSCROLL消息。 调用base.WndProc来执行滚动function的实际所需处理。 创建一个类似WmScroll的方法,并after base.WndProc返回after立即对其after处理。 这包括对GetScrollInfo的ap / invoke调用。 确定是否确实需要滚动条。 如果不是,则调用ShowScrollBar和EnableScrollBar ,并使用所需的值绘制明显禁用的滚动条。 解决方法的问题 : 它几乎没有用。 滚动条显示和禁用,但与Windows经典主题下的滚动条类似。 它隐藏了每个ListViewGroup的折叠按钮,使它们无用! 描述性图片: 期待已久的实际问题 : 如何强制滚动条 在 ListView 始终 Visible , 而不管 ListView 的数量如何, 如果不需要它们,则禁用它们,同时避免大小错误计算 (显示 ListViewGroup 的 折叠按钮 ) 和主题恶化? 没有代码的答案,以及C# , VB.NET和C […]

如何使用P / Invoke在C#中返回一个列表?

我正在开发一个小项目,我使用P / Invoke并希望在C#中返回以下内容: public: class std::list<int,class std::allocator > const * __thiscall TransactionsModule_t::GetTransactionList(void)const 而现在这是我困惑的地方: [DllImport(“TransactionManager.dll”, EntryPoint = “…”, CallingConvention = CallingConvention.ThisCall)] public static extern ??? GetTransactionList( IntPtr interfacePtr); 我甚至不知道从哪里开始看,因为我无法直接看到返回类型是什么,显然它是一种列表。 我明白了,但这是一个嵌套列表吗? 字典Dictionary<int,List> ?

修复了带锯齿数组的语句

我有锯齿状的数组,我需要传递给外部方法。 [DllImport(…)] private static extern int NativeMethod(IntPtr[] ptrArray); … fixed (ulong* ptr = array[0]) { for (int i = 0; i < array.Length; i++) { fixed (ulong* p = &array[i][0]) { ptrArray[i] = new IntPtr(p); } } NativeMethod(ptrArray); } 问题是ptr未使用,因编译而被删除。 根据它的固定声明也被删除。 所以数组被GC移动,使得ptrArray元素变为无效。 将锯齿状数组作为指向本机方法的指针的一维数组传递的最佳方法是什么? 更新: 这是NativeMethod的C ++代码: NativeClass::NativeMethod(const int* array)