pinvoke

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

泄露秘密 提交于 2020-05-14 18:15:24
问题 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

How can I ignore a field when marshalling a structure with P/Invoke

£可爱£侵袭症+ 提交于 2020-05-12 11:30:20
问题 I want to marshal a structure for use with P/Invoke, but this struct contains a field that is only relevant to my managed code, so I don't want it to be marshaled since it doesn't belong in the native structure. Is it even possible ? I was looking for an attribute similar to NonSerialized for serialization, but it doesn't seem to exist... struct MyStructure { int foo; int bar; [NotMarshaled] // This attribute doesn't exist, but that's the kind of thing I'm looking for... int ignored; } Any

Passing a char array from c# to c++ dll

风格不统一 提交于 2020-02-04 05:06:11
问题 I have a dll of the LZ4 c implementation and I want to call the LZ4_compress_default(const char* source,char* dest,int sourceLength,int maxdestLength); function from a c# code. The function compresses the source array into the dest array. How to do this? My C# code: DllImport(@"CXX.dll", CharSet = CharSet.Ansi, SetLastError = true, CallingConvention = CallingConvention.Cdecl)] internal static extern int LZ4_compress_default( [MarshalAs(UnmanagedType.LPArray)] char[] source, out byte[] dest,

Change cursor in window caption

耗尽温柔 提交于 2020-02-02 13:11:49
问题 I have a WinForm and now I need to change the cursor when it's in the windows caption part. I have some code working, it has 2 problems: It also changes the cursor when on the edges (normal resize cursor should be shown). I found out the I need something like this WM_NCHITTEST & HTTOP , but how do I combine that? There's some flicker when moving the mouse. I also tried placing the code below the base.WndProc(ref m); . This is the code I already have: if ((m.Msg == Win32.WM.NCMOUSEMOVE || m

How to correctly call P/Invoke methods in a class library?

☆樱花仙子☆ 提交于 2020-02-01 08:37:04
问题 I have multiple projects in a Visual Studio 2015 solution. Several of these projects do P/Invokes like: [DllImport("IpHlpApi.dll")] [return: MarshalAs(UnmanagedType.U4)] public static extern int GetIpNetTable(IntPtr pIpNetTable, [MarshalAs(UnmanagedType.U4)] ref int pdwSize, bool bOrder); So I moved all my P/Invokes to a separate class library and defined the single class as: namespace NativeMethods { [ SuppressUnmanagedCodeSecurityAttribute(), ComVisible(false) ] public static class

Move P/Invokes to NativeMethods class Because it is a P/Invoke method message

烂漫一生 提交于 2020-01-29 14:23:19
问题 Can someone suggest what do I have to do with this message? CA1060 Move P/Invokes to NativeMethods class Because it is a P/Invoke method, 'UControl.InternetGetConnectedState(out int, int)' should be defined in a class named NativeMethods, SafeNativeMethods, or UnsafeNativeMethods. Mega. UControl.xaml.cs 33 Code: namespace Mega { /// <summary> /// Interaction logic for UserControl1.xaml /// </summary> public partial class UControl { [DllImport("wininet.dll")] private extern static bool

Move P/Invokes to NativeMethods class Because it is a P/Invoke method message

南楼画角 提交于 2020-01-29 14:21:52
问题 Can someone suggest what do I have to do with this message? CA1060 Move P/Invokes to NativeMethods class Because it is a P/Invoke method, 'UControl.InternetGetConnectedState(out int, int)' should be defined in a class named NativeMethods, SafeNativeMethods, or UnsafeNativeMethods. Mega. UControl.xaml.cs 33 Code: namespace Mega { /// <summary> /// Interaction logic for UserControl1.xaml /// </summary> public partial class UControl { [DllImport("wininet.dll")] private extern static bool

c++ Get raw pixel data from hbitmap

て烟熏妆下的殇ゞ 提交于 2020-01-29 05:12:25
问题 I am fairly new to using p/invoke calls and am wondering if someone can guide me on how to retrieve the raw pixel data (unsigned char*) from an hbitmap. This is my scenario: I am loading a .NET Bitmap object on the C# side and sending it's IntPtr to my unmanaged c++ method. Once I receive the hbitmap ptr on the C++ side, I would like to access the Bitmaps' pixel data. I already made a method that accepts an unsigned char* which represents the raw pixel data from c# however I found extracting

PInvoke for C function that returns char *

廉价感情. 提交于 2020-01-26 08:45:06
问题 I'm trying to write some C# code that calls a method from an unmanaged DLL. The prototype for the function in the dll is: extern "C" __declspec(dllexport) char *foo(void); In C#, I first used: [DllImport(_dllLocation)] public static extern string foo(); It seems to work on the surface, but I'm getting memory corruption errors during runtime. I think I'm pointing to memory that happens to be correct, but has already been freed. I tried using a PInvoke code gen utility called "P/Invoke Interop

Fill struct from IntPtr received in lParam property of Window Message going across process boundaries in C#

柔情痞子 提交于 2020-01-25 10:35:08
问题 I posted this question a few days ago, and I have some follow up doubts about marshaling an IntPtr to a struct. The thing goes like this: As stated in the question I am referencing, I make calls to asynchronous methods on a native Dll. These methods communicate their completion with Windows Messages. I receive the Windows Message correctly now and, within it, an lParam property (of type IntPrt). According to the documentation I am following, this lParam points to the struct that has the