pinvoke

How to pass C# object references to and from C++

♀尐吖头ヾ 提交于 2020-08-24 12:08:29
问题 Is there any way to pass C# object references (class types, not structs) into and out of C++ via native interop? Here's my use case: I'm trying to write a game engine in C# but want to use a native (C++) physics library. The physics library maintains its own internal state of all the physical bodies and lets the user associate a small amount of data (a pointer) with each body. After a physics simulation tick, the library supplies a list of all physical bodies that moved. I want to iterate

How to pass C# object references to and from C++

早过忘川 提交于 2020-08-24 12:07:20
问题 Is there any way to pass C# object references (class types, not structs) into and out of C++ via native interop? Here's my use case: I'm trying to write a game engine in C# but want to use a native (C++) physics library. The physics library maintains its own internal state of all the physical bodies and lets the user associate a small amount of data (a pointer) with each body. After a physics simulation tick, the library supplies a list of all physical bodies that moved. I want to iterate

How to pass C# object references to and from C++

旧城冷巷雨未停 提交于 2020-08-24 12:03:17
问题 Is there any way to pass C# object references (class types, not structs) into and out of C++ via native interop? Here's my use case: I'm trying to write a game engine in C# but want to use a native (C++) physics library. The physics library maintains its own internal state of all the physical bodies and lets the user associate a small amount of data (a pointer) with each body. After a physics simulation tick, the library supplies a list of all physical bodies that moved. I want to iterate

How to fix “Access Violation Exception” when accessing ImageHlp.MapFileAndCheckSumA? [duplicate]

a 夏天 提交于 2020-06-29 03:54:47
问题 This question already has answers here : Attempted to read or write protected memory. This is often an indication that other memory is corrupt (26 answers) Proper IntPtr use in C# (4 answers) Closed 18 days ago . I declare it as: [System.Runtime.InteropServices.DllImport("imagehlp.dll")] public static extern UInt32 MapFileAndCheckSumA(string fileName, IntPtr HeaderSum, IntPtr CheckSum); Then I try to call MapFileAndCheckSumA IntPtr HeaderSum = new IntPtr(0); IntPtr CheckSum = new IntPtr(0);

Passing multiple parameters using CreateRemoteThread in C#

瘦欲@ 提交于 2020-06-11 05:54:16
问题 My goal is to call a function in a remote process using P/Invoke in C# (CreateRemoteThread). The problem is that the function takes more than one parameter. Is there a way to pass multiple parameters to the function? 回答1: [DllImport("kernel32.dll", SetLastError = true, ExactSpelling = true)] static extern IntPtr OpenProcess(int dwDesiredAccess, bool bInheritHandle, int dwProcessId); [DllImport("kernel32.dll", SetLastError = true, ExactSpelling = true)] static extern IntPtr VirtualAllocEx

Is there generally a noticeable performance hit when calling PInvoke on Win32 / COM methods?

狂风中的少年 提交于 2020-05-24 10:12:20
问题 I'm wondering whether anyone has a decent explanation or overview on the negative aspects of using DLLImport / PInvoke on Win32 methods from managed .Net code? I plan to make use of various Win32 methods and would like to have a greater understanding on the negative implications of doing so. Thanks, Brian. 回答1: According to MSDN - Calling Native Functions from Managed Code PInvoke has an overhead of between 10 and 30 x86 instructions per call. In addition to this fixed cost, marshaling

Compute total width of title bar buttons for 3rd party window on windows 10

牧云@^-^@ 提交于 2020-05-15 08:12:48
问题 My initial approach to this was using GetSystemMetrics with SystemMetric.SM_CXSIZE and some simple math based on which buttons are available (times 3, or times 1), via WindowStyle . [DllImport("user32.dll")] private static extern int GetSystemMetrics(SystemMetric smIndex); This has an issue on Windows 10, where the calculated width is approximately 70% of actual. So the width covers just two buttons - maximize and close. Windows 7 and 8.1 are fine, same DPI setting, where it covers all

AccessViolationException when PInvoking C++ DLL (cdecl calling convention problem?) [closed]

…衆ロ難τιáo~ 提交于 2020-05-14 18:17:12
问题 Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 2 years ago . I've spent all day researching this, and I'm none the wiser: I have a C# DLL which PInvokes a method in a C++ DLL. I have had no problems doing this when compiling in Debug mode, but when compiling in Release mode I get an AccessViolationException. Googling this problem tells me