pinvoke

Reverse PInvoke and create a full unmanaged C# program

隐身守侯 提交于 2019-12-23 19:51:16
问题 I know this is a strange question but the idea is simple: I prefer C# syntax rather than C++: -Setters and getters directly inside a property -interfaces -foreach statement -possibility to declare an implicit cast operator other small things... What I really don't know is if is possible to import a c++ dll (expecially std libraries) in C# if I don't use any namespace (even System) The idea is just to write a program using everything that you will normally use in C++ (nothing from CLR so),

DllImport - ANSI vs. Unicode

六眼飞鱼酱① 提交于 2019-12-23 16:21:42
问题 I have some questions about the possible answers for the test question bellow: Question: You write the following code segment to call a function from the Win32 Application Programming Interface (API) by using platform invoke. string personName = "N?el"; string msg = "Welcome" + personName + "to club"!"; bool rc = User32API.MessageBox(0, msg, personName, 0); You need to define a method prototype that can best marshal the string data. Which code segment should you use? // A. [DllImport("user32"

Indomitable beast: a 2d char array, inside a structure, in the belly of an unmanaged dll

落花浮王杯 提交于 2019-12-23 15:18:57
问题 Having gird my loins and ventured into Legacy Land, having hacked, p-invoked and marshaled every type of wild beast, I now stand before a creature so fierce that, as far as I can tell from an exhausting survey of my brethern-in-arms, not a single code warrior has been left standing. Here are the details. I am attempting to pass a 2d char array (in c#), inside a structure, to a C dll (no source code), which must be able to make changes to the 2d array. The C structure: typedef struct s_beast {

How to marshall c++ char* to C# string using P/INVOKE

假装没事ソ 提交于 2019-12-23 14:54:18
问题 I'm new to C++. I'm calling a C++ function from C# using a PINVOKE and wanting to get a string back as an out parameter. However I just get an empty string back. The int out parameter works fine. Importing: [DllImport ( @"UnamanagedAssembly.dll", CharSet = CharSet.Ansi)] public static extern int Activate(ref int numActivated, StringBuilder eventsActivated); extern "C" __declspec(dllexport) int Activate(int *p_NumActivated, char *p_EventsActivated) {return Activation::GetInstance()->Activate(p

Managed to unmanaged code call causes access violation… sometimes

非 Y 不嫁゛ 提交于 2019-12-23 14:02:04
问题 This code causes the following exception, sometimes : "Attempted to read or write protected memory. This is often an indication that other memory is corrupt" private static TOKEN_GROUPS GetTokenGroups(IntPtr tokenHandle) { var groups = new TOKEN_GROUPS(); uint tokenInfoLength = 0; uint returnLength; var res = GetTokenInformation(tokenHandle, TOKEN_INFORMATION_CLASS.TokenGroups, IntPtr.Zero, tokenInfoLength, out returnLength); if (!res && returnLength > 0) { tokenInfoLength = returnLength; var

Changes via SetEnvironmentVariable do not take effect in library that uses getenv

社会主义新天地 提交于 2019-12-23 12:56:35
问题 I have a simple c# application that is binding to a library compile with mingnu compiler toolset. I can easily call the functions in the library without issue. However the library calls getenv to set itself up this environment variable needs to be set for the library to work correctly so I am using Environment.SetEnvironmentVariable however the library cannot retrieve the value I have set. 回答1: getenv makes a copy of the environment variable block of the process on startup. Any subsequent

Changes via SetEnvironmentVariable do not take effect in library that uses getenv

半世苍凉 提交于 2019-12-23 12:55:11
问题 I have a simple c# application that is binding to a library compile with mingnu compiler toolset. I can easily call the functions in the library without issue. However the library calls getenv to set itself up this environment variable needs to be set for the library to work correctly so I am using Environment.SetEnvironmentVariable however the library cannot retrieve the value I have set. 回答1: getenv makes a copy of the environment variable block of the process on startup. Any subsequent

platform pinvoke tutorial msdn

痴心易碎 提交于 2019-12-23 12:47:18
问题 The following is a tutorial from msdn. The ouput of _flushall is "Test" in the tutorial but I got "2" by displaying output using console.write(). Can somebody explain please? using System; using System.Runtime.InteropServices; class PlatformInvokeTest { [DllImport("msvcrt.dll")] public static extern int puts(string c); [DllImport("msvcrt.dll")] internal static extern int _flushall(); public static void Main() { puts("Test"); _flushall(); } } 回答1: That code doesn't work anymore on modern

Why does String.Equals(Object obj) check to see if this == null? [duplicate]

我们两清 提交于 2019-12-23 09:18:10
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Why check this != null? // Determines whether two strings match. [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)] public override bool Equals(Object obj) { //this is necessary to guard against reverse-pinvokes and //other callers who do not use the callvirt instruction if (this == null) throw new NullReferenceException(); String str = obj as String; if (str == null) return false; if (Object

Properly declare SP_DEVICE_INTERFACE_DETAIL_DATA for PInvoke

柔情痞子 提交于 2019-12-23 07:29:09
问题 The SP_DEVICE_INTERFACE_DETAIL_DATA structure: typedef struct _SP_DEVICE_INTERFACE_DETAIL_DATA { DWORD cbSize; TCHAR DevicePath[ANYSIZE_ARRAY]; } SP_DEVICE_INTERFACE_DETAIL_DATA, *PSP_DEVICE_INTERFACE_DETAIL_DATA; How do I declare it in C# to get Marshal.SizeOf work properly? I don't have a problem with allocating dynamic buffer. I only want to calculate cbSize in a proper, non-hardcoded manner. The definition at PInvoke.net is wrong. The explanation at PInvoke.net is also wrong: SP_DEVICE