unmanaged

Uniformly handling error codes in an unmanaged API

故事扮演 提交于 2019-12-10 17:19:35
问题 I'm writing a wrapper around a fairly large unmanaged API. Almost every imported method returns a common error code when it fails. For now, I'm doing this: ErrorCode result = Api.Method(); if (result != ErrorCode.SUCCESS) { throw Helper.ErrorToException(result); } This works fine. The problem is, I have so many unmanaged method calls that this gets extremely frustrating and repetitive. So, I tried switching to this: public static void ApiCall(Func<ErrorCode> apiMethod) { ErrorCode result =

Calling a C++ function from C# - unbalanced stack

这一生的挚爱 提交于 2019-12-10 16:54:31
问题 I have a unmanaged C++ function with the following signature: int function(char* param, int ret) I am trying to call it from C#: unsafe delegate int MyFunc(char* param, int ret); ... int Module = LoadLibrary("fullpathToUnamanagedDll"); IntPtr pProc = GetProcAddress(Module, "functionName"); MyFunc func = (MyFunc)System.Runtime.InteropServices.Marshal.GetDelegateForFunctionPointer(pProc, typeof(MyFunc)); unsafe { char* param = null; int ret = 0; int result = func(param, ret); } As far as I can

What .NET UnmanagedType is Unicode (UTF-16)?

大憨熊 提交于 2019-12-10 16:36:01
问题 I am packing bytes into a struct, and some of them correspond to a Unicode string. The following works fine for an ASCII string: [StructLayout(LayoutKind.Sequential)] private struct PacketBytes { [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 64)] public string MyString; } I assumed that I could do [StructLayout(LayoutKind.Sequential)] private struct PacketBytes { [MarshalAs(UnmanagedType.LPWStr, SizeConst = 32)] public string MyString; } to make it Unicode, but that didn't work (the field

How can I in C# stream.Read into unmanaged memory stream?

我们两清 提交于 2019-12-10 16:08:55
问题 I can read unmanaged memory in C# using UnmanagedMemoryStream, but how can I do the reverse? I want to read from a managed stream directly into unmanaged memory, instead of first reading into a byte[] and then copying. I'm doing async stream reading on a large number of requests, so the added memory is significant (not to mention the additional copy). 回答1: I think that it is not really possible. When you talk about a managed stream, I suppose you are refering to an instance of System.IO

C# Unmanaged exports when multiple projects involved (Robert Giesecke)

我的未来我决定 提交于 2019-12-10 15:18:15
问题 I have this situation where I need to create an unmanaged DLL in .Net that can be invoked from a delphi program. I've been doing some research and I found Robert Giesecke's library (RGiesecke.DllExport). I started with a pretty simple DLL that displays a windows form with a textbox, something like this: [ComVisible(true)] [DllExport("PlaceOrder", CallingConvention = CallingConvention.StdCall)] public static IntPtr PlaceOrder(IntPtr lnpInXml) { string inputXml = Marshal.PtrToStringAnsi

Find out what a random number generator was seeded with in C++

浪尽此生 提交于 2019-12-10 14:44:57
问题 I've got an unmanaged c++ console application in which I'm using srand() and rand(). I don't need this to solve a particular problem, but was curious: is the original seed passed to srand() stored somewhere in memory that I can query? Is there any way to figure out what the seed was? 回答1: The seed is not required to be stored, only the last random number returned is. Here's the example from the manpage: static unsigned long next = 1; /* RAND_MAX assumed to be 32767 */ int myrand(void) { next

Allocation and deallocation of memory in unmanaged code using platform Invoke (C#)

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-10 13:25:24
问题 I want to allocate and deallocate memory in unmanaged code (C++) and we call them functions from managed code (C#). Iam not sure whether the following code is fine without memory leaks or not? C# code: [DllImport("SampleDLL.dll")] public extern void getString([MarshalAs(UnmanagedType.LPStr)] out String strbuilder); [DllImport("SampleDLL.dll")] public extern void freeMemory([MarshalAs(UnmanagedType.LPStr)] out String strBuilder); .... //call to unmanaged code getString(out str); Console

How to send a string by reference to an unmanaged C library that modifies that string?

僤鯓⒐⒋嵵緔 提交于 2019-12-10 12:38:31
问题 I am new to the world of interacting with unmanaged libraries. I have an unmanaged C function that modifies a string by reference within the function. I'm having trouble passing a string from C# and getting it modified by the C function. Here's the C function: __declspec(dllexport) void __stdcall Test(char* name) { *name = "Bar"; } This is the C# DLL import code: [DllImport(@"C:/blah/mylibrary.dll")] public extern static string Test(string name); This is the code I'm using to call the

Changing the string to which an IntPtr is pointing

≡放荡痞女 提交于 2019-12-10 12:15:57
问题 In my C# application I have a variable lpData of type IntPtr (received from a call to unmanaged code), and it points to a string. I have to replace this string with another value. I tried: int RegQueryValueExW_Hooked( IntPtr hKey, string lpValueName, int lpReserved, ref Microsoft.Win32.RegistryValueKind lpType, IntPtr lpData, ref int lpcbData) { lpData = Marshal.StringToHGlobalUni("new string"); ... } but this doesn't seem to replace the actual string. Can someone point me in the right

Qt xmlWriter/xmlReader

不打扰是莪最后的温柔 提交于 2019-12-10 10:56:26
问题 I want to write and read an xml file using Qt. Is there a simple example code, which generates an XML file dynamically? I've found some Qt xml classes, but does anybody know what they are used to and is there an easy example which uses these classes? QtXml Module (http://doc.qt.nokia.com/latest/qtxml.html) QXmlStreamReader (http://doc.qt.nokia.com/4.7/qxmlstreamreader.html) QXmlStreamWriter (http://doc.qt.nokia.com/4.7/qxmlstreamreader.html) I've written a managed C++ code, but my problem is