Tag: pinvoke

通过p / invoke使用c#中的XGBoost DLL

我正在尝试使用XGBoost的 dll(libxgboost.dll)来创建一个DMatrix(就像一个2D数组)并获得它有多少列。 它运行正常,直到它在下面的代码中的int cols = …行抛出System.AccessViolationException : using System; using System.Runtime.InteropServices; namespace basicXgboost { class Program { [DllImport(“../../libs/libxgboost.dll”, CharSet = CharSet.Auto)] public static extern int XGDMatrixCreateFromFile([MarshalAs(UnmanagedType.LPStr)] string file, int silent, IntPtr outputPtr); [DllImport(“../../libs/libxgboost.dll”, CharSet = CharSet.Auto)] public static extern int XGDMatrixNumCol(IntPtr dmatrixPtr, IntPtr dmatrixColumnsPtr); static void Main(string[] args) { IntPtr dmatrixPtr = Marshal.AllocHGlobal(1000000); IntPtr dmatrixColumnsPtr […]

使用PInvoke声明实现代码示例时遇到问题

我在这里引用了以下线程来解答我的问题: 从系统托盘中的图标获取工具提示文本 我基本上想要做与OP相同的事情,但与响应此线程的其他一些用户不同,我在添加缺少的代码示例中未明确定义的PInvoke声明后,无法使代码工作。 我组装了一个类文件,试图将所有内容放在一起,我引用了PInvoke.net的声明(我不确定这是用户michalczerwinski所做的,或者他是否正在使用某种PInvoke库)。 这是我到目前为止所拥有的: using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Runtime.ConstrainedExecution; using System.Runtime.InteropServices; using System.Security; using System.Text; using System.Threading.Tasks; namespace Example { public static class TrayTooltip { [Flags] public enum ProcessAccessFlags : uint { All = 0x001F0FFF, Terminate = 0x00000001, CreateThread = 0x00000002, VirtualMemoryOperation = 0x00000008, VirtualMemoryRead = 0x00000010, VirtualMemoryWrite = […]

当从针对任何CPU的C#项目调用时,为什么此代码会抛出System.AccessViolationException?

我的ATL项目中有这个IDL: [ object, uuid(61B0BFF7-E9DF-4D7E-AFE6-49CC67245257), dual, nonextensible, pointer_default(unique) ] interface ICrappyCOMService : IDispatch { typedef [ uuid(C65F8DE6-EDEF-479C-BD3B-17EC3F9E4A3E), version(1.0) ] struct CrapStructure { INT ErrorCode; BSTR ErrorMessage; } CrapStructure; [id(1)] HRESULT TestCrap([in] INT errorCode, [in] BSTR errorMessage, [in, out] CrapStructure *crapStructure); }; [ uuid(763B8CA0-16DD-48C8-BB31-3ECD9B9DE441), version(1.0), ] library CrappyCOMLib { importlib(“stdole2.tlb”); [ uuid(F7375DA4-2C1E-400D-88F3-FF816BB21177) ] coclass CrappyCOMService { [default] […]

向TreeView添加文件夹结构需要数小时

为什么这段代码需要数小时才能完成: public void SetRoot(string path) { Tag = path; BeginUpdate(); AddFolderRecursive(Nodes, path); EndUpdate(); } private void AddFolderRecursive(TreeNodeCollection nodes, string path) { try { var dirs = Directory.EnumerateDirectories(path).OrderBy(d => d).Select(d => d.Split(Path.DirectorySeparatorChar).Last()); TreeNode node; ShellFileGetInfo.FolderIcons fi; foreach (var d in dirs) { node = nodes.Add(Path.Combine(path, d), d, ImageList.Images.Count); node.Tag = Path.Combine(path, d); node.SelectedImageIndex = ImageList.Images.Count + […]

C#p / Invoke如何使用SendInput为DirectX游戏模拟keyPRESS事件

我经常为各种机器人或其他GUI自动化程序模拟键盘按键事件而苦苦挣扎。 我设法使用以下方法模拟keydown事件: INPUT[] kInput = new INPUT[1]; kInput[j].type = SendInputEventType.InputKeyboard; kInput[j].mkhi.ki.wVk = 0; kInput[j].mkhi.ki.wScan = (ushort) MapVirtualKey((uint) Keys.D5, 0); kInput[j].mkhi.ki.dwFlags = KeyboardEventFlags.SCANCODE; kInput[j].mkhi.ki.time = 0; kInput[j].mkhi.ki.dwExtraInfo = IntPtr.Zero; SendInput(1, kInput, Marshal.SizeOf(typeof(INPUT))); 要么 INPUT[] kInput = new INPUT[1]; kInput[1].type = SendInputEventType.InputKeyboard; kInput[1].mkhi.ki.wVk = ‘5’; kInput[1].mkhi.ki.wScan = 0; kInput[1].mkhi.ki.dwFlags = 0; kInput[1].mkhi.ki.time = 0; kInput[1].mkhi.ki.dwExtraInfo = IntPtr.Zero; […]

emxArray_real_T到C#struct加上初始化

我正在尝试为此C#结构创建一个“构造函数”(包括初始尝试): [StructLayout(LayoutKind.Sequential)] public struct emxArray_real_T { public IntPtr data; public IntPtr size; public int allocatedSize; public int numDimensions; [MarshalAs(UnmanagedType.U1)] public bool canFreeData; public emxArray_real_T(double[] cSharpData) { var arraySize = Marshal.SizeOf(cSharpData[0]) * cSharpData.Length; this.data = Marshal.AllocHGlobal(arraySize); // ???? numDimensions = 1; canFreeData = false; } } C对应的C结构如下所示: typedef struct emxArray_real_T { real_T *data; int32_T *size; int32_T […]

增加整个桌面上的光标大小

首先,请放下“您的申请不应该这样做”的概念。 这正是购买此软件的人们所期待的。 我如何在系统范围内增加鼠标光标的大小? 我也必须增加所有鼠标光标,所以我不认为SetCursor可以做到这一点,至少不是以任何漂亮,干净的方式。 我不能使用Form的光标大小,因为这只会影响光标在窗体上的活动状态。 我看到Windows的Ease-of-Access Center中有“超大”鼠标光标,所以必须有办法…… 有任何想法吗?

如何在c#中存储从C ++函数返回的uint8_t *变量?

我从我的C#程序中调用了一个C ++ DLL。 DLL由几个函数组成,除了这个函数之外,我能够调用其中的大多数函数。 C ++函数如下所示: __declspec(dllexport) uint8_t* myHash(const char *filename) { uint8_t *hash = (unsigned char*)malloc(72*sizeof(uint8_t)); //some processing on hash return hash; } 从上面的代码中可以看出,散列函数存储了一个字符数组。 我想在我的C#程序中收到值,但我无法做到。 我的C#代码如下所示: [DllImport(“myHash.dll”, CharSet = CharSet.Ansi)] public static extern IntPtr myHash(string filename); IntPtr ptr = myHash(fileA); char[] result = new char[72]; Marshal.Copy(ptr, result, 0, 72);

C#P \调用DLL没有进入C ++的入口点?

我有一个C ++ Dll“TheFoo.dll”,方法“Foo()” 我只需调用以下命令即可访问使用此方法的其他C ++代码: Foo(); 我相信该方法确实有: __declspec( dllexport ) 所以,随着我对P / Invoke所做的阅读,我认为我应该能够简单地从我的C#代码中调用相同的方法: namespace PInvokeExample1 { /// /// Interaction logic for MainWindow.xaml /// public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } [DllImport(@”C:\MyFolder\TheFoo.dll”)] public static extern void Foo(); private void button1_Click(object sender, RoutedEventArgs e) { Foo(); } } } 当我运行它时,我收到一个错误: Unable to […]

使用指针参数(WCT)从C#调用C ++方法

我不熟悉从C#调用C ++方法的概念。 假设我想从C#调用C ++函数GetThreadWaitChain : https://msdn.microsoft.com/en-us/library/windows/desktop/ms679364(v=vs.85).aspx https://msdn.microsoft.com/en-us/library/windows/desktop/ ms681623(v = vs.85)的.aspx 我已经构建了一个调用所依赖的其他类型的模型: [DllImport(“Advapi32.dll”)] public static extern void CloseThreadWaitChainSession(IntPtr WctHandle); [DllImport(“Advapi32.dll”)] public static extern HANDLE OpenThreadWaitChainSession(UInt32 Flags, UInt32 callback); [DllImport(“Advapi32.dll”)] public static extern BOOL GetThreadWaitChain( IntPtr WctHandle, UInt32 Context, UInt32 flags, UInt32 ThreadId, WAITCHAIN_NODE_INFO NodeInfoArray, UInt32 IsCycle ); [StructLayout(LayoutKind.Sequential)] public struct WAITCHAIN_NODE_INFO { public UInt32 ObjectType; public […]