unmanaged

Passing c# string to unmanaged c++ DLL

£可爱£侵袭症+ 提交于 2019-12-03 16:05:15
I have a simple application that loads an unmanaged dll and passes a few string values to it from C#. But in the C++ dll application, I receive an exception :: Tried to access a read/write protected memory. My DLL Import looks like this: [DllImport("X.dll", CallingConvention = CallingConvention.Cdecl) ] public static extern int DumpToDBLogFile([MarshalAs(UnmanagedType.I4)]int loggingLevel, [MarshalAs(UnmanagedType.I4)]int jobId, int threadId, [MarshalAs(UnmanagedType.LPStr)]string procName, [MarshalAs(UnmanagedType.LPStr)]string message); and the C++ Declaration is like extern "C" __declspec

convert struct handle from managed into unmanaged C++/CLI

丶灬走出姿态 提交于 2019-12-03 13:57:04
问题 In C#, I defined a struct: [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] public struct MyObject { [MarshalAs(UnmanagedType.LPWStr)] public string var1; [MarshalAs(UnmanagedType.LPWStr)] public string var2; }; I have this struct in C++: public value struct MyObject { LPWSTR var1; LPWSTR var2; }; And in the method of C++ which is a public class to be called from C#: TestingObject(MyObject^ configObject) { // convert configObject from managed to unmanaged. } The object is

What's the equivalent of WORD in C#?

与世无争的帅哥 提交于 2019-12-03 11:01:46
I'm trying to access an unmanaged library and am lucky to have access to a comprehensive guide to the API. Unfortunately, I've no idea what the C# equivalent of C++'s WORD type is. Similarly, I've no idea what DWORD would be. yan You're probably looking for ushort for WORD and uint for DWORD. 来源: https://stackoverflow.com/questions/5490428/whats-the-equivalent-of-word-in-c

Pinning an updateble struct before passing to unmanaged code?

醉酒当歌 提交于 2019-12-03 09:34:48
问题 I using some old API and need to pass the a pointer of a struct to unmanaged code that runs asynchronous. In other words, after i passing the struct pointer to the unmanaged code, the unmanaged code copies the pointer and returns immediately. The unmanaged code can access that struct in background, in another thread. I have no control over the unmanaged code that runs in another thread nor the thread itself. The fixed { } statement can't be used for pinning because it not designed for async

How can I embed an unmanaged C++ form into a .NET application?

怎甘沉沦 提交于 2019-12-03 07:56:15
I have been able to successfully wrap my unmanaged Borland C++ dll, and launch it's forms from a C# .NET 4.0 application. Is it possible to embed a form from the dll directly into a .NET application? To clarify, the original form is being used as an embedded control in a Borland C++ project already. It essentially looks like a custom control, sitting on a panel within the application. When I say 'embed' I mean place INTO a form in the same way you drop buttons, panels, etc on to a form. I'm not looking to just make a child form. If this is not possible, then perhaps a better question would be

What is the difference between a delegate instance and a method pointer?

坚强是说给别人听的谎言 提交于 2019-12-03 06:42:37
I thought that a delegate instance was interchangeable with a function instance. Take the following code: delegate int AddDelegate(int a, int b); AddDelegate DelegateInstance; public void DoStuff() { //I can call this without a delegate "instance": MethodThatTakesAdd(Add); //I can also call it WITH a delegate "instance" DelegateInstance = Add; MethodThatTakesAdd(DelegateInstance); } public int Add(int a, int b) { return a + b; } public void MethodThatTakesAdd(AddDelegate addFunction) { Console.WriteLine(addFunction(1, 2).ToString()); } Both ways of calling it APPEAR to be equivalent, and if

P/Invoke to dynamically loaded library on Mono

試著忘記壹切 提交于 2019-12-03 06:04:42
问题 I'm writing a cross-platform .NET library that uses some unmanaged code. In the static constructor of my class, the platform is detected and the appropriate unmanaged library is extracted from an embedded resource and saved to a temp directory, similar to the code given in another stackoverflow answer. So that the library can be found when it isn't in the PATH, I explicitly load it after it is saved to the temp file. On windows, this works fine with LoadLibrary from kernel32.dll. I'm trying

Where to place dlls for unmanaged libraries?

最后都变了- 提交于 2019-12-03 04:38:08
I am trying to create a Nuget package for a library that depends on ghostscript and therefore references gsdll32.dll - an unmanaged library. I can't just included that a standard dll reference. Where do I put this in the nuget directory structure? Add a build folder to the package and, if the package for example has the id MyPackage , add a MSBuild target file called MyPackage.targets to this folder. It is important that the .targets file has the same name as the .nuspec file. In the .nuspec file you must have a section like this: <files> <file src="lib\*.*" target="lib" /> <file src="build

What exactly happens during a “managed-to-native transition”?

為{幸葍}努か 提交于 2019-12-03 04:17:42
问题 I understand that the CLR needs to do marshaling in some cases, but let's say I have: using System.Runtime.InteropServices; using System.Security; [SuppressUnmanagedCodeSecurity] static class Program { [DllImport("kernel32.dll", SetLastError = false)] static extern int GetVersion(); static void Main() { for (; ; ) GetVersion(); } } When I break into this program with a debugger, I always see: Given that there is no marshaling that needs to be done (right?), could someone please explain what's

convert struct handle from managed into unmanaged C++/CLI

邮差的信 提交于 2019-12-03 03:47:09
In C#, I defined a struct: [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] public struct MyObject { [MarshalAs(UnmanagedType.LPWStr)] public string var1; [MarshalAs(UnmanagedType.LPWStr)] public string var2; }; I have this struct in C++: public value struct MyObject { LPWSTR var1; LPWSTR var2; }; And in the method of C++ which is a public class to be called from C#: TestingObject(MyObject^ configObject) { // convert configObject from managed to unmanaged. } The object is debugged correctly that I can see two strings var1 and var2. However, the problem now is that I need to